Fish-Net: Networking Evolved
  • Overview
    • What is FishNet?
      • Features
        • Unity Compatibility
        • Performance
          • Benchmark Setup
          • Fish-Networking Vs Mirror
      • Pro, Projects, and Support
      • Business Support
      • Development
        • Changelog
        • Roadmap
      • Branding
      • Legal Restrictions
    • Showcase
      • Upcoming Releases
    • Asset Integrations
      • Fish-Network-Discovery
    • Community Resources
  • Guides
    • Getting Started
      • Installing Fish-Networking
      • Getting Connected
      • Preparing Your Player
      • Moving Your Player Around
      • Spawning and Despawning Items
      • Using SyncVars to Sync Colors
      • Connecting to Remote Devices
      • Beyond the Basics
    • High-Level Overview
      • Fundamentals
      • Networking Models
      • Terminology
        • Server, Client, Host
        • Communicating
        • Miscellaneous
      • Transports
    • Features
      • Server and Client Identification
        • Executing on Server or Client
        • NetworkConnections
      • Networked GameObjects and Scripts
        • NetworkObjects
        • NetworkBehaviour
        • Spawning and Despawning
          • Predicted Spawning
          • Nested NetworkObjects
          • Object Pooling
      • Network State Events
      • Network Communication
        • Remote Procedure Calls
        • SyncTypes
          • Customizing Behavior
          • SyncVar
          • SyncList
          • SyncHashSet
          • SyncDictionary
          • SyncTimer
          • SyncStopwatch
          • Custom SyncType
        • Broadcasts
      • Data Serialization
        • Custom Serializers
          • Interface Serializers
          • Inheritance Serializers
      • Ownership
        • Using Ownership To Read Values
      • Area of Interest (Observer System)
        • Modifying Conditions
        • Custom Conditions
      • Scene Management
        • Scene Events
        • Scene Data
          • SceneLookupData
          • SceneLoadData
          • SceneUnloadData
        • Loading Scenes
          • Automatic Online and Offline Scenes
        • Unloading Scenes
        • Scene Stacking
        • Scene Caching
        • Scene Visibility
        • Persisting NetworkObjects
        • Custom Scene Processors
          • Addressables
      • InstanceFinder
      • Addressables
      • Transports
        • Multipass
      • Prediction
        • What Is Client-Side Prediction
        • Configuring PredictionManager
        • Configuring TimeManager
        • Configuring NetworkObject
        • Offline Rigidbodies
        • Interpolations
        • Creating Code
          • Controlling An Object
          • Non-Controlled Object
          • Understanding ReplicateState
            • Using States In Code
            • Predicting States In Code
          • Advanced Controls
        • Custom Comparers
        • PredictionRigidbody
        • Using NetworkColliders
      • Lag Compensation
        • States
        • Raycast
        • Projectiles
    • Upgrading API
    • Server Hosting
      • Edgegap - Official Partner
        • Getting Started with Edgegap
      • Hathora
        • Getting Started with Hathora
      • Amazon Web Services (AWS)
        • Getting Started with AWS
    • Upgrading To Fish-Networking
    • Troubleshooting
      • Technical Limitations
      • Creating Bug Reports
        • Report Example
      • FAQ
  • FishNet Building Blocks
    • Components
      • Managers
        • NetworkManager
        • TimeManager
        • PredictionManager
        • ServerManager
        • ClientManager
        • SceneManager
        • TransportManager
          • IntermediateLayer
        • StatisticsManager
        • ObserverManager
          • HashGrid
        • RollbackManager (Pro Feature)
      • Prediction
        • Network Collider
          • NetworkCollision
          • NetworkCollision2D
          • NetworkTrigger
          • NetworkTrigger2D
        • OfflineRigidbody
        • PredictedOwner
        • PredictedSpawn
      • Utilities
        • PingDisplay
        • BandwidthDisplay
        • Tick Smoothers
          • NetworkTickSmoother
          • OfflineTickSmoother
          • MonoTickSmoother [Obsolete]
          • DetachableNetworkTickSmoother [Obsolete]
      • PlayerSpawner
      • DefaultScene
      • ServerSpawner
      • Authenticator
      • ColliderRollback
      • NetworkAnimator
      • NetworkBehaviour
      • NetworkTransform
      • NetworkObject
      • NetworkObserver
    • Prefabs
      • NetworkManager
      • NetworkHudCanvas
    • ScriptableObjects
      • ObserverConditions
        • DistanceCondition
        • GridCondition
        • HostOnlyCondition
        • MatchCondition
        • OwnerOnlyCondition
        • SceneCondition
      • SpawnablePrefabs
        • DefaultPrefabObjects
        • SinglePrefabObjects
        • DualPrefabObjects
      • LevelLoggingConfiguration
    • Transports
      • Tugboat
      • Multipass
      • Yak (Pro Feature)
      • Bayou
      • FishyWebRTC
      • FishyUnityTransport
      • FishySteamworks (Steam)
      • FishyEOS (Epic Online Services)
      • FishyFacepunch (Steam)
      • FishyRealtime (Photon)
  • API Documentation
    • API Reference
Powered by GitBook
On this page
  • Overview
  • Checking a Boolean
  • Method Attributes
  • Client Method Attribute
  • Server Method Attribute
  1. Guides
  2. Features
  3. Server and Client Identification

Executing on Server or Client

How you can run certain code on only the server, or only a client, or only on a host.

PreviousServer and Client IdentificationNextNetworkConnections

Last updated 1 day ago

Overview

Very often your code will need to know if it's running on a server or a client, or both at the same time. 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 and classes have easy to use properties for this exact reason. They are also accessible from the 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.

If you are in a NetworkBehaviour then using IsServerStarted is easiest, but it also checks whether the NetworkObject has been spawned on the network. You can easily use the to access the NetworkManager in order to check from a MonoBehaviour or non-spawned NetworkBehaviour though.

Simple example showing how a NetworkBehaviour could prevent code from running on the server:

Player.cs
using FishNet.Object;

public class Player : NetworkBehaviour
{
    public void OnStruck()
    {
        if (!IsClientStarted)
            return;

        // Play visual effect and sounds only on a client.
    }
}

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 optionally additionally the NetworkObject is initialized). There are additional properties which may be added to the attribute for additional functionality.

Simple example showing the Client attribute to prevent the method from running when not a client.

[Client]
void ShowUI()
{
    // This code will only run on a client, otherwise it will print a warning.
}

And following is an example of the options you can use with it: If this method were called with no client active a warning would be printed and the method would not run. You can change the logging type using the Logging property to change whether it should log a warning at all or not. The default value is LoggingType.Warning.

It's also possible to allow only the owner of the object to call the method by setting RequireOwnership to true; this value is false by default.

Finally, you can use the UseIsStarted property to cause this to ignore the initialized state of the NetworkObject and check if the client alone is started.

// The server does not need to play VFX; only play VFX if the client is active.
[Client(Logging = LoggingType.Off, RequireOwnership = true, UseIsStarted = true)]
void PlayVFX() 
{ 
    // Play VFX here...
}

Server Method Attribute

The Server attribute provides similar features of the Client variant, except will not allow the method to run if the server is not active (and optionally additionally the NetworkObject is initialized).

Here is an example of the Server attribute being used to prevent code from running anywhere except the server.

Like the Client attribute, a warning will be thrown if this is called while the server is not active. The warning can be changed or disabled by adjusting the Logging property.

You can use the UseIsStarted property to cause this to ignore the initialized state of the NetworkObject and only check if the server is started.

// The server would validate hit results from a client.
[Server(Logging = LoggingType.Off, UseIsStarted = false)]
private void ValidateHit() 
{
    // Hit validation code here that will only run on the server.
}
NetworkBehaviour
NetworkObject
NetworkManager
InstanceFinder