Steamworks and unity–p2p Multiplayer

Source: Internet
Author: User
Tags message queue

before we discussed "how to integrate steamworks.net and unity," This is a good start, and now we're going to go deeper and talk about multiplayer games on Steam. This is not a tutorial, but it can guide you how to use Steamworks in your game. We will use Steamworks. NET Library, Steam_api.dll. Note that your game runs and needs to have a steam app Id. You can apply for one here (greenlit), or the game passed directly by valve company also has an ID. This is a guide if you just want to know if steam is a convenient platform to read on ...

multi-player game
Steamworks's greatest feature is its matching mechanism and peer-to network communication, you don't have to worry about server settings, everything is ready. you may not be familiar with the use of peers to build a multiplayer game, and now the popular way is to build separate entities for clients and server vendors. In this case, the client is the game itself, and the server is a service-side logic that connects all the players and protects the player's data from being spoofed by the app. Client, server This mode may be better for large-scale games, for some small, non-competitive games, consider abandoning the servers section, two client direct conversations may be better. It's really simple, isn't it?  Steamworks make it as simple as possible. You don't have to worry about the link, you just need a steamid (Steamid is the user's only identity, it is a large number encapsulated in the Csteamid object, you can get the csteamid of the user interacting with you, such as the pass-through Hall (lobby). When you have steamid, you need to do the following: [C #]Plain Text view copy code ?
1 // class SteamNetworkingpublicstatic bool SendP2PPacket(CSteamID steamIDRemote, byte[] pubData, uint cubData, EP2PSend eP2PSendType, intnChannel = 0)

Pubdata is the data we want to send, Cubdata is the byte size of the data sent, Ep2psendtype is the way to transmit, nchannal default value is empty, it is not used, not discussed. here's how to send "hello!" Examples of strings: [C #]Plain Text view copy code ?
1234567 CSteamID receiver = ...;string hello = "Hello!"; // allocate new bytes array and copy string characters as bytesbyte[] bytes = new byte[hello.Length * sizeof(char)];System.Buffer.BlockCopy(hello.ToCharArray(), 0, bytes, 0, bytes.Length);SteamNetworking.SendP2PPacket(receiver, bytes, (uint) bytes.Length, EP2PSend.k_EP2PSendReliable);


Here are four ways to send:
    • k_ep2psendunreliable – Packet, can be lost, do not need to be sent sequentially, but fast
    • K_ep2psendunreliablenodelay – the same as above, but does not do a link check, because it may be lost, but this is the quickest way to send.
    • k_ep2psendreliable – Reliable information, large packets, and send and receive in turn.
    • k_ep2psendreliablewithbuffering – the same as above, but buffers the data before sending it, and if you send a lot of small packets, it won't be that timely. (may be delayed by 200ms)


What do you do on the other side?

If one person sends the data, the other receives the data in some way. Of course, they all have security measures in secret. You will not send data to other Steamworks clients outside of your range. Before a client can receive your data, he or she has accepted your request and set up a peer session.

Peer-to-peer requests occur when you first send data to a steamwork client. When you send any data, the process repeats automatically (usually a few minutes), and you should only accept the connection you want, such as other players in your lobby (lobby).

How do I accept a session request? Very simple! You can write code like this:

[C #]Plain Text view copy code ?
01020304050607080910111213141516171819 // create a callback field. Having a field will make sure that the callback// handle won‘t be eaten by garbage collector.private Callback<P2PSessionRequest_t> _p2PSessionRequestCallback;void Start(){    // setup the callback method    _p2PSessionRequestCallback = Callback<P2PSessionRequest_t>.Create(OnP2PSessionRequest);}void OnP2PSessionRequest(P2PSessionRequest_t request){    CSteamID clientId = request.m_steamIDRemote;    if (ExpectingClient(clientId))    {        SteamNetworking.AcceptP2PSessionWithUser(clientId);    } else {        Debug.LogWarning("Unexpected session request from " + clientId);    }}



this one-to-peer conversation will be accepted and you can begin ...  Read message

All messages are present in the Steamwork message queue. You can read it when you are ready to take it. These are typically handled in the update () function, and your app can check to see if there are any new messages as soon as possible.

[C #]Plain Text view copy code ?
010203040506070809101112131415161718192021222324 void Update() {    uint size;    // repeat while there‘s a P2P message available    // will write its size to size variable    while (SteamNetworking.IsP2PPacketAvailable(out size))    {        // allocate buffer and needed variables        var buffer = new byte[size];        uint bytesRead;        CSteamID remoteId;        // read the message into the buffer        if (SteamNetworking.ReadP2PPacket(buffer, size, out bytesRead, out remoteId))        {            // convert to string            char[] chars = new char[bytesRead / sizeof(char)];            Buffer.BlockCopy(buffer, 0, chars, 0, length);            string message = new string(chars, 0, chars.Length);            Debug.Log("Received a message: " + message);        }    }}

 That's all!

Summary

This guide does not cover the cleanup section (this is not required because unused sessions are automatically cleaned up) and exception handling. You can read them official Steamworks documentation in official documents. Remember that you need to be a partner in steam to get it, and if you are not, I hope you can consider becoming one of them after you finish reading this article.

Original link: http://blog.theknightsofunity.com/steamworks-and-unity-p2p-multiplayer/

Steamworks and unity–p2p Multiplayer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.