NetworkRNG returing always same value

RNG struct will always return same value at whatever tick. Is there a bug with it or I am doing something wrong.

ex. usecase

public class MNetBehaviour : NetworkBehaviour

{

  [Networked] private NetworkRNG rng { get; set; }

  public override void Spawned()

  {

    rng = new NetworkRNG(Runner.Tick);

  }

  public override void FixedUpdateNetwork()

  {

    for (int i = 0; i < 5; i++)

      Debug.Log(rng.Next());

  }

}

This will always return same value, depending on seed, if no seed passed will always return 0.

Best Answer

  • PizzaPie
    PizzaPie
    Answer ✓

    To awnser my self, obviously properties of a value type return a copy of the struct which gives the above result.


    Potential fix:

    public override void FixedUpdateNetwork()

        {

          var tempRng = rng;

          for (int i = 0; i < 5; i++)

          {

            Debug.Log(tempRng.Next());

            rng = tempRng;

          }

        }

Answers

  • PizzaPie
    PizzaPie
    Answer ✓

    To awnser my self, obviously properties of a value type return a copy of the struct which gives the above result.


    Potential fix:

    public override void FixedUpdateNetwork()

        {

          var tempRng = rng;

          for (int i = 0; i < 5; i++)

          {

            Debug.Log(tempRng.Next());

            rng = tempRng;

          }

        }