Advertisement
Meridius_IX

ModAPI: Network Message Example

May 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.  
  2. //Put this in your scriptInit
  3. //Replace 4 digit number with a unique number
  4. //Replace MethodName with the name of a void Method (example below)
  5. MyAPIGateway.Multiplayer.RegisterMessageHandler(4123, MethodName);
  6.  
  7. ///////////////////////////////////////////////////////////////
  8.  
  9. void MethodName(byte[] data){
  10.            
  11.     string receivedData = MyAPIGateway.Utilities.SerializeFromBinary<string>(data);
  12.     //Do Stuff For Client or Server (split string like you would customdata if needed)
  13.    
  14. }
  15.  
  16. ///////////////////////////////////////////////////////////////
  17.  
  18. //This serializes the string data you want to send
  19. var sendData = MyAPIGateway.Utilities.SerializeToBinary<string>("Some String You Want To Send");
  20.  
  21. //This sends the data to a specific player using their SteamId
  22. bool sendPlayer = MyAPIGateway.Multiplayer.SendMessageTo(4123, sendData, factionPlayer.SteamUserId);
  23.  
  24. //This sends to the server only
  25. bool sendServer = MyAPIGateway.Multiplayer.SendMessageToServer(4123, sendData);
  26.  
  27. ///////////////////////////////////////////////////////////////
  28.  
  29. //Must unregister your network message handler, otherwise they will stack between game loads
  30. protected override void UnloadData(){
  31.            
  32.     MyAPIGateway.Multiplayer.UnregisterMessageHandler(4123, MethodName);
  33.    
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement