Search Unity

Question Help with Network variables

Discussion in 'Netcode for GameObjects' started by GuirieSanchez, Dec 15, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Could someone provide a link/guide, or elaborate a little example on how network variables work?

    I know these variables will be set on a server/host and can modify the same variables' values on the client side, but I have 0 idea about how to implement this system.

    I think an example of a single network variable set on the server/host and modified on the client will be really helpful for me to understand the system.


    PS: As a side question, but not less important: I have my variables stored on a scriptable object. Can network variables be also on scriptable objects? and if not, is there any workaround?


    Thank you!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,979
    Isn‘t such an example in the docs?
     
    GuirieSanchez likes this.
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @GuirieSanchez , check the doc here . It has the example you're looking for! :D

    You can't have NetworkVariables in a ScriptableObjects. NetworkVariables must ve in NetworkBehaviour classes. What's your use case? What are you trying to achieve?
     
    GuirieSanchez likes this.
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Thank you for the link. Back in the day I was checking the doc and didn't see NetworkVariables, so I thought it would be somewhere else. My bad.

    It's a simple game where all the game parameters (let's say +50) are stored in a scriptable object because they are referenced throughout the entire game (on different scenes). Parameters are modified at the start of each session (for starting parameters) and during the game.

    Not having a scriptable object as a middle-man would be a little chaotic at this point I believe :(

    PS: what I'm trying to achieve is updating all those parameters (+50) from the host to the client. I thought about sending the parameters through JSON, but then I wanted to try NetworkVariables first. I want to pass the info at the start, and everytime a client connects.
     

    Attached Files:

    Last edited: Dec 16, 2022
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    It's ok to have scriptable oebjects, but you need to have a networked middleman that can send the synced data around.

    So what you can do is to load the scriptable object on both clients and server at startup, and then make the server overwrite (and sync with clients) only these variables that were changed.
     
    GuirieSanchez likes this.
  6. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    That's smart. I'm going to give it a try.
    So, if I'm not totally off, since every player has the same data container, I can make a networked middleman sync NetworkVariables and overwrite each data container's (SO) variables with NetworkVariables

    PS: Hmm, according to the documentation, NetworkVariables' value can only be set at the start of the game (Awake). So, now that I think about it, I shouldn't be able to make a method to update NetworkVariables' values at runtime. If this is correct, is there another way to accomplish it?
     
    Last edited: Dec 16, 2022
  7. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452

    Sorry, I got a little confused by this sentence
    upload_2022-12-17_14-13-51.png

    But yeah, by looking at the example, you can change the NetworkVariable's value on a ServerRpc, and, if I'm not mistaken, its value will automatically be updated across all connected clients.
     
  8. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    I noticed that clients who want to receive the "OnValueChanged" callback must subscribe to it. In my case, where I have +50 NetVars, do I have to make a callback for each of them? It seems a little too much, doesn't it?
     
  9. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    You have to subscribe to each one, but 50 sounds a lot for one class to contain. You might want to look at separating them out by responsibility.
     
  10. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    This is because I want the parameters to be stored in one place so that they can be referenced easily, as I mentioned earlier. I was wondering if having many callbacks called at the same time can have some performance impact.

    PS: Is it possible to have arrays as NetworkVariables? It doesn't look like I'm able to do it. Also, I saw an "OnListChanged" callback, but not "OnArrayChanged" callbacks. If there's such a thing, will it be able to track the values of all the items of the array?
     
    Last edited: Dec 17, 2022