Executing on Server or Client
How you can run certain code on only the server, or only a client, or only on a host.
Overview
Very often your code will need to know if it's running on a server or a client, or . There are several ways of knowing this, for example, code executed directly from client or server only events or methods, checking compiler directives to see the type of game build, marking methods to only allow running on a specific instance, or simply checking a Boolean value.
Checking a Boolean
You can easily check if the current code is running on the server with a Boolean check. FishNet's NetworkBehaviour and NetworkObject classes have easy to use properties for this exact reason. They are also accessible from the NetworkManager directly.
You can use IsClientStarted
to detect if the instance running the code is a client (it could also be a server).
IsClientOnlyStarted
can be used to detect if the instance running the code is a client only and not a server.
IsServerStarted
detects if the instance running the code is the server (It could also be a client).
And finally, IsServerOnlyStarted
returns whether the instance running the code is a server only and not a client.
When checking from a NetworkBehaviour or on a NetworkObject you will often want to know if the code is running on the client or server side AND the NetworkObject has already been initialized on the network and can be safely used for network functionality, such as RPCs or network properties. You can do that using the IsServerInitialized
, IsServerOnlyInitialized
, IsClientInitialized
, and IsClientOnlyInitialized
properties.
Instead of using something like IsClientInitialized && IsServerInitialized
each time you want to check if the code is running on the host, you can simply check IsHostInitialized
. There are Started, and Only variations of this as well.
Method Attributes
There are a variety of attributes which can be used to ensure a method only runs on the appropriate game instance.
These methods are also used for Fish-Networking Pro's automatic code stripping. Importantly, methods marked with the Server
attribute will have their method body stripped from non server builds.
Client Method Attribute
Placing a Client
attribute above a method ensures that the method cannot be called unless the local client is active and the NetworkObject is initialized (the latter of which can be disabled by enabling the UseIsStarted
option). There are additional properties which may be added to the attribute for additional functionality, such as for disabling the warning that would be logged if you try to call the method with this and it doesn't pass the check.
Server Method Attribute
The Server
attribute provides similar features of the Client
variant, except it will not allow the method to run if the server is not active and the NetworkObject is initialized (the latter of which can be disabled by enabling the UseIsStarted
option). There are additional properties which may be added to the attribute for additional functionality, such as for disabling the warning that would be logged if you try to call the method with this and it doesn't pass the check.
Last updated