This page is a work in progress and could be pending updates.

Fusion 105 - Property Changes


When defining networked properties, Fusion will replace the provided get and set stubs with custom code to access the network state. This means that the application cannot use these methods to deal with changes in property values, and creating separate setter methods will only work locally.

To solve this problem, Fusion provides an argument to the [Networked] attribute that can be used to specify a static callback method to be invoked whenever that property changes.

This is useful for spawning local visual effects in response to state changes or perform other tasks that does not directly impact gameplay logic. This is an important caveat since property changes may occur multiple times due to re-simulation or be skipped entirely if the property changes back and forth between two values faster than network states are being sent.

The primary benefit of using change callbacks over common messages like RPCs is that the callback is executed in the correct tick where as an RPC may arrive when the game is in an unexpected state.

Consult the Manual for an in-depth description of this topic

Back To Top

Adding A Change Listener

The property changed callback is defined with the OnChanged parameter on the [Networked] attribute itself and takes the name of a static method accepting a single parameter of type Changed<T> where T is the type of the enclosing Behaviour.

The provided callback must be a static method because this allows Fusion to have a single delegate for the type rather than allocating delegates for each instance of the object.

In this example, the goal is to color the cube white when a ball is fired and then fade it towards blue.

To trigger the effect, the host will toggle a single bit in a networked variable. Since no more than one ball can ever be spawned in a single tick (by a given player), each new spawn will change the value of the bit to something that is different from the previous tick and thus trigger the OnChanged callback.

Before adding the code, note that this design can fail - snapshots can be lost or merged so that a state change disappears. To make it more resilient to this, one could replace the NetworkBool with a byte or an int and bump it on each invocation instead. In the end, it is a question of how important the visual effect is vs. how much bandwidth it consumes.

With that out of the way, open the Player class and add a new property as well as a simple implementation of the callback:

[Networked(OnChanged = nameof(OnBallSpawned))]
public NetworkBool spawned { get; set; }

public static void OnBallSpawned(Changed<Player> changed)
  changed.Behaviour.material.color = Color.white;

This obviously assumes that the Player has a material property which can be used to change the color of the cube mesh, so go ahead and add a new property for this:

private Material _material;
Material material 
      _material = GetComponentInChildren<MeshRenderer>().material;
    return _material;

The color should be updated in Render() as a linear interpolation from the current color towards blue. This is done in Render() rather than Update() because it is guaranteed to run after FixedUpdateNetwork() and it uses Time.deltaTime rather than Runner.DeltaTime because it is running in Unity's render loop and not as part of the Fusion simulation.

public override void Render()
  material.color = Color.Lerp(material.color,, Time.deltaTime );

All that remains is to trigger the callback by toggling the spawned property after calling Runner.Spawn():

Runner.Spawn(_prefabBall, transform.position+_forward, Quaternion.LookRotation(_forward));
spawned = !spawned;

Keep in mind that there are two places where Spawn() is called and both should toggle spawned.

Back To Top

But Why?

Q: But why not simply set the color immediately when calling spawn?

While this would work for the host and for the client with Input Authority because both are predicting based on the players input, it would not work for proxies.

Q: But what if the color was a networked property that was simply applied locally on all clients in Render(), surely there would be no need for an OnChanged handler?

That would indeed work, but it would need to be animated on the host and it would generate a lot of unnecessary traffic. Generally, visual effects should be triggered by the state authority and then left to run autonomously on each client. As much as everyone loves sparks, nobody cares if a particular spark is flying in one direction or the other.

Back To Top

Accessing Previous States

This simple example did not care about the actual value of the changed property, but it should be noted that the Changed<T> parameter provided to the callback has a reference to the source behaviour as it was when the change occurred, allowing the application to access all the networked properties of the behaviour during that tick.

What is maybe less obvious, is that there is also a method to load the prior state of the behaviour, effectively providing the application with the state from the previous tick for all networked properties:

  var newValue = changed.Behaviour.someNetworkedProperty;
  var oldValue = changed.Behaviour.someNetworkedProperty;

Similarly, the application can load the new state again with LoadNew(), but it does not need to worry about resetting the state before exiting, since the state is only relevant in the context of this particular callback.

To Document Top