Global scene variable?

Options
Wobbles67
Wobbles67
edited July 2022 in Fusion

What is the best way to create a 'global' variable that is connected to the current scene and not any individual player?

It also needs to be read/writable by all clients, including new clients as they connect with every player seeing the same value at all times.

Best Answer

  • Wobbles67
    Wobbles67
    Answer ✓
    Options

    Well, this is how I've implemented it which seems to work. Whether it's the best way or not I don't know. I created a Network Object in the scene with the following script attached:

        public class MatchQueue : NetworkBehaviour, ISpawned
        {
            [Networked] public int testQueue { get; set; }
    
            public override void Spawned()
            {
                Debug.Log("Match Queue spawned!");
            }
    
            [Rpc(RpcSources.All, RpcTargets.StateAuthority)]
            public void RPC_UpdateQueue()
            {
                testQueue++;
            }
    
            public int GetTestQueue()
            {
                return testQueue;
            }
        }
    

    The RPC method is called when the user clicks a button to join a match. The current queue can be retrieved by calling the GetTestQueue() method, which I do from an Update() on the GUI Panel so it always shows the current queue length.

Answers

  • Wobbles67
    Wobbles67
    Answer ✓
    Options

    Well, this is how I've implemented it which seems to work. Whether it's the best way or not I don't know. I created a Network Object in the scene with the following script attached:

        public class MatchQueue : NetworkBehaviour, ISpawned
        {
            [Networked] public int testQueue { get; set; }
    
            public override void Spawned()
            {
                Debug.Log("Match Queue spawned!");
            }
    
            [Rpc(RpcSources.All, RpcTargets.StateAuthority)]
            public void RPC_UpdateQueue()
            {
                testQueue++;
            }
    
            public int GetTestQueue()
            {
                return testQueue;
            }
        }
    

    The RPC method is called when the user clicks a button to join a match. The current queue can be retrieved by calling the GetTestQueue() method, which I do from an Update() on the GUI Panel so it always shows the current queue length.

This discussion has been closed.