Change Callback
For Properties
You can have a method get called whenever a networked property changes, which is very useful. To do that, add the attribute [OnChanged]
to the method and give it the name of the property. The method must have one parameter of the same type of the property which would contain its previous value.
Example:
[Networked]
public int Health { get; set; }
[OnChanged(nameof(Health ))]
private void OnHealthChanged(int previous)
{
// Something that happens when the Health property changes
}
For Arrays
If you have an array and want a callback for when an element changes, you can do so as follows:
Example:
[Networked(size: 10)]
public NetworkArray<int> Items { get; set; }
[OnChanged(nameof(Items))]
private void ArrayChanged(int index)
{
// Something that happens when an element of the array changes
}
The difference here to a variable property is that the callback method must have an index parameter that points to the changed element.
Last updated
Was this helpful?