Network Signals
Last updated
Last updated
are used to communicate between the client and server. They are a critical piece of building a multiplayer game.
Imagine a scenario where a player would like to use a health potion. We first need to tell the server that a player would like to use a potion and have it apply the potion's effects.
Ability Use
Notifying the server when a player uses a special ability
Trade Request
Letting the server know two players would like to trade
Item Drop
Telling the server a player has dropped an item
Network Signals can be created inside of any script. NetworkSignal
takes a single generic parameter that is the type of data passed through the signal.
Firing the usePotion
Network Signal on the client
Listening for usePotion
Network Signals on the server
Once the server has applied the potion, we want to tell every player about it so they can play particle effects, sounds, update UI, etc.
Server to client Network Signals can be sent to any number of players
Telling all players a potion has been consumed
Listening for potionUsed
Network Signals on the client
Primitive data types and their composite forms (arrays, objects) can be sent over the network when using NetworkSignals
and NetworkFunctions
. The following Unity types are also supported
If a network signal receives an event, but does not have any existing connection handlers, the event will be cached until the first connection is created. At this point, all events will be passed along to the connection handler.
This is typically useful when the server sends a client a network signal event right after the client has joined, but before the client has connected to the signal. Thus, the event is not missed by the client, as it is successfully handed to the first connection to the signal on the client.
The cache is limited in size (currently hard-coded to 10,000), and will throw errors if exceeded.
The first argument passed to OnClientEvent
is always the who fired the Network Signal.
A common requirement is sending Characters and Players over the network. Send and ids and utilize the and APIs to fetch their respective objects. This ensures that only permitted data types are sent over the network, while the actual player and character objects are retrieved locally.