User Tools

Site Tools


besiege:modding:networking

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
besiege:modding:networking [2018/07/24 15:47]
spaar
besiege:modding:networking [2018/07/24 16:02] (current)
spaar [Receiving Messages]
Line 19: Line 19:
 Example of creating and storing a message type: Example of creating and storing a message type:
  
-<​code ​cs>+<​code ​csharp>
 using Modding; using Modding;
 +using Modding.Blocks;​
  
 public static class Messages { public static class Messages {
Line 65: Line 66:
   * ''​SendTo(Player,​ Message)''​ sends the given message only to the given player.   * ''​SendTo(Player,​ Message)''​ sends the given message only to the given player.
   * ''​SendInSimulation(Message)''​ sends the message to all instance participating in the current simulation. This may mean no player (not in simulation, in local simulation) or all players that are currently not in local simulation (in global simulation).   * ''​SendInSimulation(Message)''​ sends the message to all instance participating in the current simulation. This may mean no player (not in simulation, in local simulation) or all players that are currently not in local simulation (in global simulation).
 +
 +Continuing the example from above:
 +
 +<code csharp>
 +float x = <compute some float>;
 +Block b = <get some block>;
 +
 +Message message = Messages.Example.CreateMessage(x,​ b);
 +ModNetworking.SendToAll(message);​
 +</​code>​
  
 ===== Receiving Messages ===== ===== Receiving Messages =====
Line 70: Line 81:
 To receive messages, a mod must register a callback to at least one of two events: ''​ModNetworking.MessageReceived''​ or ''​ModNetworking.Callbacks[MessageType]''​. Both events require callbacks that take exactly one argument: a ''​Message''​ object. To receive messages, a mod must register a callback to at least one of two events: ''​ModNetworking.MessageReceived''​ or ''​ModNetworking.Callbacks[MessageType]''​. Both events require callbacks that take exactly one argument: a ''​Message''​ object.
  
-The first event is fired for every message sent by the same mod.+The first event is fired for every message sent by the mod.
  
 Using the second form, it is possible to register different callbacks for different message types. If ''​exampleType''​ was a ''​MessageType''​ instance, callbacks registered to ''​ModNetworking.Callbacks[exampleType]''​ will be called for every message with the given type that is received. (Registering callbacks works the same way it does for normal events, i.e. ''​ModNetworking.Callbacks[exampleType] += MyCallbackMethod;''​ or ''​+= (msg) => {%%<​code>​%%};''​) Using the second form, it is possible to register different callbacks for different message types. If ''​exampleType''​ was a ''​MessageType''​ instance, callbacks registered to ''​ModNetworking.Callbacks[exampleType]''​ will be called for every message with the given type that is received. (Registering callbacks works the same way it does for normal events, i.e. ''​ModNetworking.Callbacks[exampleType] += MyCallbackMethod;''​ or ''​+= (msg) => {%%<​code>​%%};''​)
  
-Either way, a callback receives a ''​Message''​ object. The ''​Message''​ object exposes who sent the message with the ''​Sender''​ property (of type ''​Player''​) and the data attached to it with the ''​GetData(int)''​ method. ''​GetData'' ​takes an index as argument, ​the first object passed ​''​CreateMessage'' ​has index 0the second object has index 1 and so on. The objects are returned as simple ​''​object''​s but can simply be cast to their original types.+Either way, a callback receives a ''​Message''​ object. 
 + 
 +The ''​Message''​ object exposes who sent the message with the ''​Sender''​ property (of type ''​Player''​) and the data attached to it with the ''​GetData(int)''​ method. 
 + 
 +Indices passed to ''​GetData'' ​correspond to the position of the objects in the ''​CreateMessage'' ​(or, equivalently, ''​CreateMessageType''​) call. 
 + 
 +Concluding the example: 
 + 
 +<code csharp>​ 
 +public override void OnLoad() { 
 +    Messages.Example = ...; 
 +    ModNetworking.Callbacks[Messages.Example] = msg => { 
 +        float x = (float)msg.GetData(0);​ 
 +        Block b = (Block)msg.GetData(1);​ 
 +        Debug.Log("​Received a message from " + msg.Sender.Name 
 +            + ", x = " + x 
 +            + ", b = " + b.Guid + "​!"​);​ 
 +    }; 
 +
 +</​code>​
besiege/modding/networking.1532440062.txt.gz · Last modified: 2018/07/24 15:47 by spaar