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:
-
Is “Multiplayer.MultiplayerPeer.PutPacket(bytesMessage);” even sending the bytes array to all peers?
-
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. -
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?
-
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?