How to be notified when a relation is executed?
There are 2 ways to be notified when a relation is executed.
Global event
The following code shows how to register and unregister to the global event (OnRelationExecuted).
private void OnEnable()
{
XURelation.OnRelationExecuted += OnRelationExecuted;
}
private void OnDisable()
{
XURelation.OnRelationExecuted -= OnRelationExecuted;
}
private void OnRelationExecuted(XURelation relation)
{
Debug.Log($"OnRelationExecuted event received from {relation.id}");
}
The event gives the executed relation in parameter. You can filter a specific relation with the following code.
if (relation is RelationChangeColor changeColor)
{
// Only for RelationChangeColor
}
else if (relation is RelationSwapColor swapColor)
{
// Only for RelationSwapColor
}
Be aware that the given relation object will be destroyed the next frame so save the desired values if you want to use them in a coroutine or later.
XUType messages
Automaticaly, each GameObjects of the XUTypes involved in a Relation will be notified using SendMessage()
.
To receive this message, you have to add a method in a MonoBehaviour (the XUType or any Monobehaviour on the involved GameObject) named like: public void OnRelationFooExecuted(XURelation relation)
where RelationFoo
is the classname of the desired XURelation.
The following code shows an example of a message received by a XUType XUTypeColored
when a RelationChangeColor
is executed.
/// <summary>
/// Called after <see cref="RelationChangeColor"/>.Run()
/// </summary>
public void OnRelationChangeColorExecuted(XURelation relation)
{
Debug.Log($"OnRelationChangeColorExecuted message received from {relation.id}");
}