#How do I sync TMP texts? I want to

1 messages · Page 1 of 1 (latest)

plucky river
#
NetworkVariable<FixedString32Bytes> playerName = new(string.Empty,writePerm: NetworkVariableWritePermission.Owner);
public TMP_Text playerTag;

 public override void OnNetworkSpawn()
 {
     if(HasAuthority)
     playerName.Value = new FixedString32Bytes(AuthenticationService.Instance.PlayerName.Split('#')[0]);
     playerName.OnValueChanged += OnPlayerNameChanged;
     OnPlayerNameChanged(string.Empty, playerName.Value);
}
 void OnPlayerNameChanged(FixedString32Bytes previousValue, FixedString32Bytes newValue)
 {
     playerTag.SetText(newValue.ToString());
 }
}
pastel raven
#

I don't use NetworkSpawn, because there is no spawning done in the scene right now. All I do is serialize the string with Inetworkserializable, then assign a playername, and then make a server rpc send the TMP's text through.

#

@plucky river what would you do in this case? Is this a bad code principle right here?

plucky river
#

RPCs have to be in a network behavior on a spawned Network object. So it needs to be spawned regardless of RPC or Network Variable

pastel raven
#

Well, right now what I do is just have 4 objects (networkObjects) and whenever a player joins, it sets 1 more active.The script itself is NetworkBehaviour, so what am I missing?

pastel raven
plucky river
#

no, it has to be spawned if you are sending data

pastel raven
#

I'm fine with NOs I believe, I just don't understand how could I spawn the object when it is already on the screen? Do I just spawn it on awake as a one-liner?

plucky river
#

also keep in mind that late joining clients will not get previous RPCs or Custom Messages

plucky river
pastel raven
#

I won't have late joining in my game I know that thanks.

pastel raven
#

Do they have to be a prefab?

plucky river
#

just make sure that you are loading the scene with NetworkManager.SceneManager

pastel raven
#

Well I have only 1 scene.

#

If they have to be a prefab fine, but they're not. They're just called 'LobbyPlayer' 1-4

plucky river
pastel raven
#

Let me read this through

pastel raven
#

Okay so apparently the RPC now runs (it didn't run before) but the text still didn't change

pastel raven
plucky river
pastel raven
#

Isn't that done with serverRpcs

#

Thats why I send ServerRPC data for the clients

pastel raven
plucky river
#

ServerRPC should work. Make sure you aren't using any Sinlgetons. That tends to not work well with networking

pastel raven
#

I'm not using any singletons

plucky river
#

You need to send the playername in the RPC

pastel raven
plucky river
#

playerNameText needs to be set to the playerName that the RPC is sending and not the public one in the class

pastel raven
plucky river
plucky river
pastel raven
#

But where should the serverrpc be located?

#

Inside the LobbyPlayer script or a manager script?

plucky river
#

It could be on a manager object if that manager is also keeping track on lobby players and their nam

pastel raven
pastel raven
#

Can I somewhere find a sample or a video where they explain or I can look into this @plucky river ?

plucky river
#

You can check out Code Monkey's Complete Multiplayer Course. There is a lecture on RPCs

pastel raven
#

Yeah I watched the full stuff and did it like months ago

#

Guess I'm gonna watch it again

#

But he doesn't sync this particular case

pastel raven
#

I found the problem @plucky river . The problem is that The PlayerData.playerName is always the Host's name which is weird.
Other than this, the RPCs and everything works. I just don't know how the playerName is the same for all (btw I'm using Lobby, and the OwnerClientID is 0 on both instances because I test it on the same pc, however I have different names setup in the lobby player settings)

plucky river
#

For in scene objects the owner client ID will always be 0. normally you would spawn lobby players in dynamically so they have the correct owner.

#

PlayerData.SetPlayerName() looks like a static function. that will get overridden when each player name you set

pastel raven
#

Yes you were right! I solved it before reading this but nevermind

pastel raven
plucky river
pastel raven
#

Alright thanks

#

I seem to get this error a lot, why could be it?

plucky river
pastel raven
plucky river
pastel raven
#

I don't use that version because it generates various problems and errors for me. The last problem I believe is that both OwnerClientIDs are 0 but the profiles are different. Can that be a problem?

pastel raven
#

Have you ever met this error?

plucky river
#

Not since updating to Unity 6 and NGO2.0. There was an issue with using Fixedstrings in network variables but it's was fixed a while ago.