Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A couple of simple questions

Discussion in 'Netcode for GameObjects' started by thefitz35, Aug 12, 2021.

  1. thefitz35

    thefitz35

    Joined:
    Jul 6, 2021
    Posts:
    1
    Hey, Ive been really struggling about the basics of MLAPI, and just wrapping my head around how it works, and Id really appreciate some clarity about a couple things.

    1. When calling a ServerRpc, I know Im running that code on the server, but what object is it running in? The Server version of the clients Object?

    2. Are networked variables unique to each object using them? Or are they shared? This is the Code Ive been trying to figure it out with.

    public NetworkVariableFloat healthy;

    public void Takedamage(int dmg)
    {
    if (IsHost)
    {
    healthy.Value -= dmg;
    return;
    }
    if (IsClient)
    {
    HurtHealthyServerRpc(dmg);
    return;
    }
    }

    [ServerRpc(RequireOwnership = false)]
    public void HurtHealthyServerRpc(float dmg)
    {
    healthy.Value -= dmg;
    }

    This treats the healthy variable as separate, and each player has his own healthy.value, which works how I want, but when I try and do it with a team variable.

    I want this code to set the host a team 10 and the clients as team 1.


    public NetworkVariableFloat teamy;

    public void SetTeam()
    {

    if (IsHost)
    {
    teamy.Value = 10;
    return;
    }
    if (IsClient)
    {
    ChangeTeamServerRpc();
    return;
    }

    }

    [ServerRpc(RequireOwnership = false)]
    public void ChangeTeamServerRpc()
    {
    teamy.Value = 1;
    }

    This treats teamy as a shared value, and when either client or host change it, it changes both to the same team and that isnt what I want.

    These seems to be written the same way to me though, I dont undersatnd how these are effecting the variables different.



    Ive been up all night working on this so if its a easy code fix, sorry, but just missing some of the basics of where code is actually being used at would help a lot.

    Thanks
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    1. Yes they will be run on the server's version of the same object
    2. They are unique for each object