Changes

Jump to navigation Jump to search
Line 212: Line 212:  
To send data to players in multiplayer you first need to call '''ParentMod.RegisterNetworkID'''(id) in a ModBehaviour. The id should be a value between 1-255, it will be used to identify messages to and from your mod, so you should pick a unique number that is unlikely to clash with other mods. When your mod is deactivated, the id will automatically be de-registered and will have to be re-registered to work. Please note that if players don't enable code mods when they host a game, all code mods will be deactivated immediately.
 
To send data to players in multiplayer you first need to call '''ParentMod.RegisterNetworkID'''(id) in a ModBehaviour. The id should be a value between 1-255, it will be used to identify messages to and from your mod, so you should pick a unique number that is unlikely to clash with other mods. When your mod is deactivated, the id will automatically be de-registered and will have to be re-registered to work. Please note that if players don't enable code mods when they host a game, all code mods will be deactivated immediately.
   −
To send messages you have to call '''ParentMod.SendNetworkMessage'''(byte[] data, ModBehaviour.MessageTarget target, byte targetID) in your ModBehaviour, where target can be '''everyone'''(default), '''everyone but me'''(so avoid sending message to self), '''everyone except'''(everyone except player with targetID), '''specifically''' (only player with targetID) and '''host'''. For performance reasons, I recommend instantiating a '''MemoryStream''' when your mod first loads and when you want to send data call MemoryStream.SetLength(0) to reset its data and then use the extension methods that Software Inc. comes with to write arbitrary data to the stream. Finally call MemoryStream.ToArray() to send the data. Please be careful about how much data you send, you should keep the packages as small as possible.
+
To send messages you have to call '''ParentMod.SendNetworkMessage'''(byte[] data, ModBehaviour.MessageTarget target, byte targetID) in your ModBehaviour, where target can be '''everyone'''(default), '''everyone but me'''(so avoid sending message to self), '''everyone except'''(everyone except player with targetID), '''specifically''' (only player with targetID) and '''host'''. For performance reasons, I recommend instantiating a '''MemoryStream''' when your mod first loads and when you want to send data call '''MemoryStream.SetLength'''(0) to reset its data and then use the extension methods that Software Inc. comes with to write arbitrary data to the stream. Finally call '''MemoryStream.ToArray'''() to send the data. Please be careful about how much data you send, you should keep the packages as small as possible.
    
When you want to receive data, override '''ModMeta.ReceiveNetworkMessage'''(SINetworking.NetworkPlayer player, MemoryStream stream), and just use the Stream extension methods to turn the bytes in the MemoryStream back into data you use, in the same order you sent it.
 
When you want to receive data, override '''ModMeta.ReceiveNetworkMessage'''(SINetworking.NetworkPlayer player, MemoryStream stream), and just use the Stream extension methods to turn the bytes in the MemoryStream back into data you use, in the same order you sent it.

Navigation menu