I’m trying to make a multiplayer game, and have stumbled upon a problem I just can’t solve…
I have a player input his name in main menu scene, then that name gets saved to PlayerPrefs and player connects to the host, the player has child Canvas with child Text on it.
On Start() I pull the name from playerPrefs and set it to Text.text, and it should sync with other players, the name is displayed above the players head…
As far as I’ve understood, It should look something like this:
Whoa, thank you, it works!
It doesn’t show the server name but I won’t have localPlayer anyways…
Could you just tell me how it works?
As far as I figured it out it works something like this:
Client sends the command to server to execute RPC and RPC basically means that server should send the change to all other clients right?
But does the name need to be a SyncVar then as it’s not gonna change during the gameplay?
oups sorry I forgot the SyncVar.
If you don’t change dynamically the playerName during the gameplay you don’t need the SyncVar.
As you mentioned the rpc is called on each client and update the player name.
The command is only executed on the server that why the player name was not updated on the local client.
If you don’t have a client on your server it seems the rpc is not called (I never tested).
I just tested it and it doesn’t work, damn…
If I run it on dedicated server as local client+Host, and dont disconnect the local client, it should work all fine, right?
As Rpc calls don’t seem to be buffered for clients joining late I wanted to sync the player names with the above method. Still have to further debug if this is really the case or if I have some hard to find timing / calling order issue hidden somewhere in my code
Whenever I try using SyncVar however the whole thing blows up…
PlayerIdentity is my NetworkBehaviour where I declare the above string.
If I remove the SyncVar attribute everything is alright.
Even initializing on declaration with =“”; does not fix this.
It turns out some custom de/serialization script sitting on the same gameobject but using a different (unreliable) NetworkChannel was destroying any SyncVar automated network update on this object. I have yet to find what evil things I’m doing here and how this can influence things across different channels…
edit: found. Some strange condition made it happen that not all data was deserialized during startup/handshake phase.
Still, why is this wrong code on unreliable channel 1 influencing code for reliable channel 0? Are they sharing buffers internally? Is it a problem to use both channels on the same object in different behaviours?
I think the main reason is that player names also need to be available for those players joining the game at later points. This can be solved quite comfortably with SynVars. Alternatively, the server would probably need to send messages to joining players to inform them about all current players names. I don’t know if the second way might be a bit more efficient, but it is a bit more complicated and requires more code.
Unlike old Networking there don’t seem to be buffered RPCs anymore… or haven’t found out yet how to confgure those. SyncVar however is buffered.
I really miss the RPC buffer and the RemoveRPC feature. It was so useful for changing object states. Now it’s probably easier to use SyncVar and the SyncVar hook…