Peer-to-peer Networking in Leadwerks Game Engine 4.6
I'm wrapping up the new multiplayer capabilities for Leadwerks 4.6, and I am very excited about what this offers developers.
We saw previously how the lobby system is used to create or join games, and how to retrieve Steam IDs for messaging. Once we have joined a game we can start sending messages with the P2P::Send command, which includes a few overloads:
static bool P2P::Send(uint64 steamid, const int messageid, const int channel = 0, const int flags = 0); static bool P2P::Send(uint64 steamid, const int messageid, std::string& data, const int channel = 0, const int flags = 0); static bool P2P::Send(uint64 steamid, const int messageid, Bank* data, const int channel = 0, const int flags = 0); static bool P2P::Send(uint64 steamid, const int messageid, Stream* data, const int channel = 0, const int flags = 0); static bool P2P::Send(uint64 steamid, const int messageid, const void* data, const int size, const int channel = 0, const int flags = 0);
Each message has an ID and can be followed by additional data in the form of a string, bank, or stream. Voice chat is handled automatically, but the rest is up to you to decide what data the messages should contain. I provide examples for text chat, joining and leaving a game, and movement.
Receiving messages from other players is extremely simple:
static Message* P2P::Receive(const int channel = 0);
The message object has information contained within it:
class Message { public: int id; Stream* stream; uint64 userid };
We can evaluate messages based on their ID. For example, here is a text chat message being received:
auto message = P2P::Receive() if (message) { if (message->id == MESSAGE_CHAT && message->stream != nullptr) { Print(message->stream->ReadString()); } }
A new multiplayer game template will be included, with out-of-the-box support for text and voice chat, public servers, and player movement.
You can download the test app and try it out here:
Thanks to everyone who has helped me test this!
- 1
- 1
5 Comments
Recommended Comments