Sending and receiving bytes via MultiplayerPeer.PutPacket() in C# Godot 4.2.2 (data transfer without RPCs)

Godot Version

Godot 4.2.2

Question

I want to send data between server and client without using RPCs and while I got the connection running, I struggle with sending raw bytes.

I have set up the server and client connection via ENetMultiplayerPeer.
From a german tutorial video (https://youtu.be/Yp1IxZyuP90?si=0MdQ64Qb2j1iGubE&t=1274) I got to the option of sending and receiving raw bytes via these methods for sending (broadcast to every connected peer):

byte[ ] bytesMessage = System.Text.Encoding.UTF8.GetBytes(message);
Multiplayer.MultiplayerPeer.SetTargetPeer((int)MultiplayerPeer.TargetPeerBroadcast);
Multiplayer.MultiplayerPeer.PutPacket(bytesMessage);

And a method for receiving:

int senderID = Multiplayer.MultiplayerPeer.GetPacketPeer();
byte[] receivedBytes = Multiplayer.MultiplayerPeer.GetPacket();
string stringMessage = System.Text.Encoding.UTF8.GetString(receivedBytes);

Unfortunately, this was not further explored and demonstrated/tested in the video.

I have the impression, that I must connect these functions to an event handler and for an example in GDScript (Any ENet example without rpc? (send packets between clients)), I got this line for receiving-callbacks:

multiplayer.peer_packet.connect(received_message_callback)

However, I cannot find a “Multiplayer.PeerPacket” or similar in C#.
More explicitly, I have these remaining questions:

  1. Is “Multiplayer.MultiplayerPeer.PutPacket(bytesMessage);” even sending the bytes array to all peers?

  2. After calling my code for sending messages, how can I check if a message was actually send on the server side?
    Currently, I call “GD.Print(Multiplayer.MultiplayerPeer.GetAvailablePacketCount());” right after “Multiplayer.MultiplayerPeer.PutPacket(bytesMessage);”, but I get “0” printed.

  3. My methods are called by my own code right after establishing a connection, but I want also to have a continuous communication. To which event handler have I to connect the receiving function, so that client and server are listening for requests and answers?

  4. I’m looking for alternatives to RPCs since I’m running a dedicated headless server as stand alone project + a client project receriving updates to world. The game runs a procedurally generated world and hands only relevant chunks of the world to each client. Thus, the number and types of objects vastly differs between all peers and therefore RPCs are not really useful here, correct?

yes the cpp code uses id 0 which is to all.

It will be asynchronous, meaning it will take a non-zero amount of time to transmit, and you should check continuously. (I’m not sure if packets have a life span)

Or… read below

check out SceneMultiplayer signal peer_packet

the complete opposite. RPCs will probably only get away if you spread them out onto your game entities. I understand your goal is to have complete control. You can use RPCs in a “manager” node fashion that each client will have an instance of. these manager nodes will talk to eachother via RPC’d functions. This will be almost the same as what you are attempting, but without rpc’s you will have to parse the raw packets on your own. An RPC’d function’s syntax allows you to define how the data will be packaged and parsed.

2 Likes

also if you want to even disable implicit packet peer polling behavior, you should set SceneTree.multiplayer_poll to false. Then you can manually poll the packet_peer

1 Like

Thank you very much!

Maybe I should still check RPCs out and use them to send strings, bytes, and number values. My main concern was that my architecture is not really compatible with RPCs, since I’m using completly different back ends for server and client (client basically only receives the data, visualises it, and sends object updates to server, which in turn updates the world model and sends updates to all peers).
A “manager node” would not be that different from what I have right now.

Another concern was that due to their High Level implementation, RPCs are not parsing and transmitting data efficiently. But knowing that one can adjust these settings I should give it a try

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.