Spawn Payloads
Spawn Payloads allow the sending additional information along with an object's Spawn call.
NetworkBehaviours have support for writing and reading extra data and sending it along with the spawn payloads for the network object. This can be quite useful for syncing an initial state of the object.
To use this, you need to override the NetworkBehaviour's WritePayload and ReadPayload methods.
When writing and reading, the order and amount of data written and read is important and MUST be the same.
Write Payload
The first parameter of the WritePayload method needs to be a NetworkConnection, this represents which client the spawn is going to happen for and is provided by FishNet when it calls the method.
The second parameter should be of the type Writer, this will represent a reference to the Writer being used and you will use it to add data to the payload.
For example you can do the following:
public override void WritePayload(NetworkConnection connection, Writer writer)
{
// Writes the string stored in the class variable playerName to the spawn payload.
writer.WriteString(playerName);
// Writes the integer 42 to the payload.
writer.WriteInt32(42);
}You can view the API for this method here.
Read Payload
The first parameter of the ReadPayload method needs to be a NetworkConnection, this represents which client the spawn is happening for and is provided by FishNet when it calls the method.
The second parameter should be of the type Reader, this will represent a reference to the Reader being used and you will use it to get data out of the payload.
For example you can do the following:
You can view the API for this method here.
More advanced example
Last updated