Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Netcode: String Array Syncing

Discussion in 'Netcode for GameObjects' started by jessedavis88, Nov 22, 2023.

  1. jessedavis88

    jessedavis88

    Joined:
    Mar 2, 2017
    Posts:
    6
    Hi everyone,

    I'm building a two-player asymmetrical game.

    It's really quite simple, I have several string arrays that generate random content. I'd like to sync the values for those string arrays from player 1 to player 2. So that both players get the same randomly generated strings.

    I'm at a total loss. Thanks for any help.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,961
    NetworkVariable / NetworkList if you rarely synch these, for instance when the session starts or a player joins

    Otherwise implement INetworkSerializable and use RPC calls.
     
    jessedavis88 likes this.
  3. jessedavis88

    jessedavis88

    Joined:
    Mar 2, 2017
    Posts:
    6
    That finally worked!!!!!!!!! I had tried NetworkLists several times with no hope.

    I had to use a FixedString but it finally worked. That fixed my biggest problem.

    Thank you @CodeSmile I really appreciate your help.

    However, now I'm running into an error about memory leaks with
    using Unity.Collections; (So that I could use the FixedString)

    It says
    A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details. Leak tracking may be enabled via `Unity.Collections.NativeLeakDetection.Mode` or from the editor preferences menu Edit > Preferences > Jobs > Leak Detection Level.

    Any idea where I'm going wrong?

    **Sorry still learning a lot.
     
  4. jessedavis88

    jessedavis88

    Joined:
    Mar 2, 2017
    Posts:
    6
    I haven't dug into the Leak tracking yet, but that's my next option. Didn't know if this was common or not.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,961
    Native collections need to be Dispose() ed. Unlike C# collections these won't be garbace collected. So if you have a class instance which creates a native collection and stores it in a field and that class receives OnDestroy then that's the (latest) point where you should call collection.Dispose() on the collection.

    Note there are also allocators like Allocator.Persistent and others if the lifetime of the collection should be indefinite.
     
    jessedavis88 likes this.
  6. jessedavis88

    jessedavis88

    Joined:
    Mar 2, 2017
    Posts:
    6
    @CodeSmile Thank you for all your help. I'm still having issues.

    The error is still showing itself after adding in a dispose method.

    Any idea what else might be causing the message to appear?

    I've downloaded the jobs package, and ran full stack tracing, so that I could get a location of the error, and they all point to the network variables.

    I think the other scripts attached, that generate the random code are causing the issues.

    So I think it's gotta be an issue with another script.
     
    Last edited: Nov 25, 2023
  7. jessedavis88

    jessedavis88

    Joined:
    Mar 2, 2017
    Posts:
    6
    @CodeSmile I think I might have fixed it. I saw in a random forum that you should initialize your NetworkLists in the Awake function.

    I was doing this:

    Code (CSharp):
    1.     [SerializeField]
    2.     private NetworkList<FixedString32Bytes> Wirecut_NetworkList  = new NetworkList<FixedString32Bytes>();

    When I should have been doing this:


    Code (CSharp):
    1.     [SerializeField]
    2.     private NetworkList<FixedString32Bytes> Wirecut_NetworkList;
    3.  
    4.     void Awake()
    5.     {
    6.         Wirecut_NetworkList = new NetworkList<FixedString32Bytes>();
    7.     }
    Does that make sense to you?