When I choose not Host(Server+Client), but Server Only for one instance of the program I see one client from another instance, but colour and title is not updated.
In the MyNetworkPlayer script, add the statements for setting up the name and colour from our hook methods on the server side as well.
[Server]
public void SetDisplayName(string newDisplayName)
{
displayName = newDisplayName;
if(IsServerOnly())
{
displayNameText.text = displayName;
}
}
[Server]
public void SetDisplayColour(Color newDisplayColour)
{
displayColour = newDisplayColour;
if(IsServerOnly())
{
displayColourRenderer.material.SetColor("_BaseColor", displayColour);
}
}
Can you explain why this is, are we always going to need these IsServerOnly checks. It feels like we do the same thing at 4 different places.
The game for this course was built assuming that no Server only build would be used (since hosting on a server is not part of this course). In this case I just added some IsServerOnly() checks so that we could add this functionality to the server, if there was an independent server.
We do a lot of these checks to prevent several different errors. The main one is to prevent code from being called twice. Often we are trying to prevent the server and the client from calling the same code in the Server + Client version of the game. This could result in bugs in the game or errors from trying to run code that is not valid for that Server or Client.
The other reason is to provide some checks to prevent tampering from the client side. We are making sure code that should run on the server (like moving units and taking damage) only runs on the server to prevent clients from cheating.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.