#NetworkVariable w/ RPCs

1 messages · Page 1 of 1 (latest)

regal narwhal
#

That last block has a couple issues

#
  • Multiple clients are setting the value of the NetworkVariable. Which is not supposed to be done. The idea is that one person (the owner) sets the value and it gets auto synced to everyone.
#
  • You're sending attributes.Value through an RPC which doesn't make sense. If you set attributes.Value (from the owner), that means it got synced to all other connections and there's no point in using an RPC.
solid sentinel
#

Oh interesting I must have made some other mistake before since it tdidnt work without the RPC. Probably the first thing.

regal narwhal
#
  • attributes.Value.Health = HP I'm not sure this works or is how you're supposed to set NetworkVariables structs or classes. I think you have to send the whole Class, even if you are just changing one variable of the class.
#

I would take a step back and review this bit of sample. You'll see the simplicity of working with NetworkVariable.

#

The server owns the door, so they're the only one who is to set the NetworkVariable.

#

If a client wishes to open the door (and thus set the NetworkVariable) they send a [Rpc(SendTo.Server)] to the server to set it for them.

#

and then that change of the NetworkVariable's value is automatically synced to all clients.

solid sentinel
#

I'm not sure sure what you mean with how you're supposed to set it. I think I get the server thing was just trying to fix stuff before and did it wrong.

regal narwhal
#

By default, NetworkVariables are owned by the server. So only the server can set them.

solid sentinel
#

That makes sense, I just mean how you said " I think you have to send the whole Class, even if you are just changing one variable of the class."

regal narwhal
west ermine
#

If your attributes network variable will automatically sync to the clients. So no need for client RPCs

solid sentinel
#

So is it mostly a case of putting all the variable changes inside a if(IsServer)?

west ermine
#

If it's being changed in a server RPC, that's not needed either. It will never run a client

#

Also network variables don't work with classes. They should be structs

solid sentinel
#

When it's in IsServer or set to send only to server it works for the host but only the host, clients don't seem to sync.

regal narwhal
#

on the clients

solid sentinel
#

I have a test HUD element that is just text that gets a reference to all the NetworkedGameStats components and grabs the values from that

solid sentinel
#

foreach(GameObject player in playerObjs) { if (player != null) { NetworkedGameStats stats = player.GetComponent<NetworkedGameStats>(); GameplayUI.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text += stats.GetName() + " - Health: " + stats.GetHP() + "\n"; } }

#

public int GetHP() { return attributes.Value.Health; }

regal narwhal
#

That looks fine. Although I would encourage you to use OnValueChange.

How are you setting the NetworkVariable now?

west ermine
solid sentinel
#

`[Rpc(SendTo.ClientsAndHost)]
public void DamageRPC(int damage)
{
if(IsServer)
{
int HP = attributes.Value.Health;
HP = HP - damage;
attributes.Value.Health = HP;
}

    if(IsLocalPlayer)
    {
        UpdateHUDDisplay();
    }
}`
solid sentinel
regal narwhal
solid sentinel
#

That's for the local non-test HUD, the string one I am calling on update is not syncing.

regal narwhal
#

I would say you should probably set attributes.Value only

#

Which means creating a new Attributes, setting the Health and then setting that to the Value.

solid sentinel
#

I think that works now, even updates the other variables that I didn't update to use this yet

#

Here is what I did
if(IsServer) { AttributeContainer new_attributes = new AttributeContainer(attributes.Value); int HP = new_attributes.Health; HP = HP - damage; new_attributes.Health = HP; attributes.Value = new_attributes; }