When does codes marked as [Server] gets called?

Hi,

If I mark some methods as [Server], like Awake, when they actually get called?

When my object gets generated, I believe Awake will get once. So how this can be done on the server? I’m really confused.

Thanks

‘[Server]’ on an Awake or on any function would just mean that only the Server will run it and the client would not.
As for when they are called that’s down to you to call them when needed inside the code.

Edit:
Tested it on the Start function and seems to work so it’ll work on Awake too, Although it throws back a warning;
“[Server] function ‘System.Void ClassName::Start()’ called on client”

So I would not recommend using it on Start or Awake and rather make a new function and call it within Awake and have the [Server] on the called function you wish only to run on the server.

So is there a case that Awake would be called on the client? What is the code path of NetworkBehaviour’s Awake and Start? Will they run both on the server and client? I think not. I believe they run only on the server.

I’m converting my single player game into multiplayer and I believe every code runs on the server. It’s so confusing.

NetworkBehaviour’s Awake and Start are directly from MonoBehaviour’s Start and Awake as NetworkBehaviour derives from MonoBehaviour thus start and awake run both on Server and Client unless told otherwise.

Example of Syncing Color on Server object with clients only once, wouldn’t work well if the color keeps changing:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class SyncColor : NetworkBehaviour {

   [SyncVar] private Color color;
   [SerializeField] private Renderer rend;

   // Runs both on Client and on Server
   void Start () {
     if (isServer) { //Check if the Network Identity is the server
       color = rend.material.GetColor ("_Color");
     } else { // If isServer fails then most lily is the client
       setColor();
     }
   }

   // Runs Only on the Client when called, and if the server calls this function it does nothing.
   [Client]
   private void setColor() {
     rend.material.SetColor("_Color", color);
   }
}

Honestly I should be using a hook in the SyncVars for the client but my point here is awake runs on both the server and the client.

Or with Two Functions one for server and one for client

// Runs both on Client and on Server
   void Start () {
     if (isServer) { //Check if the Network Identity is the server
       getColor();
     } else { // If isServer fails then most lily is the client
       setColor();
     }
   }

   // Runs Only on the Client when called, and if the server calls this function it does nothing.
   [Client]
   private void setColor() {
     rend.material.SetColor("_Color", color);
   }
   // Runs Only on the Server when called, and if the client calls this function it does nothing.
   [Server]
   private void getColor() {
     color = rend.material.GetColor ("_Color");
   }

Are you sure about this? So every code I have in my NetworkBehaviour derived classes run twice?! Because if it was the case, then every code from every sample that I’ve seen should’ve these attributes on them since if not, they would run twice.

Make sure you’re using [ClientCallback] and [ServerCallback] attributes instead of [Client] and [Server] on the methods called by engine, like Awake, Update and other messages.
It will keep your console free from the warnings like this:

Thanks Dimitry, but my question is something else here.

So each NetworkBehaviour is called twice, once by local player and once by server, am I right?

Yep, “same” NetworkBehaviour may exist both on the server and all the connected clients.
As it derives from the standard MonoBehaviour, you’ll have all the standard messages called (Awake, Update, etc.) on both server and client sides.

But NetworkBehaviour has own, network-specific messages.
Like PreStartClient, OnStartLocalPlayer, etc.
And those messages will be called not for every instances.
For example, first one will run only on the clients, and second one will run only for the local player (which may control that NetworkBehaviour).

Note, you also may have a host (server and client in the same application).

What happens when there is no attribute set? Is it called on Server and Client? If method has an attribute [Server] and other one [Client] and we are the host then both of them will be called for us?

Attribute just restricts execution, it can’t turn server method into the client one and vice versa.
If method is intend to run both on client and server - it will run on both of them if you’ll leave it without attributes.

For example, if you have NetworkBehaviour with Update() callback, that callback will be executed both on the server and clients by default.

But if you don’t wish it to run on all clients, you may decorate it with [ServerCallback] attribute to let Unity know it should be executed on the server only.

1 Like

It clears a lot, thanks. I’ve got one more question. If I hook the method into SyncVar what Attribute should I use? Or it just doesn’t matter because it’s redundant?

SyncVars are intend to be sent from the server to the clients. So the hooking method will run on the client side only.
Thus you don’t need to set the [Client] attribute for that method.

2 Likes