INSA
  • Tutorials
  • Documentation
  • Api Documentation
  • Changelog
Show / Hide Table of Contents
  • Getting Started
    • Quickstart
  • Unity tutorial
    • Introduction
    • Time for practice
    • Restricted scenario by script?
    • How to create the tutorial scene?
  • Scenario
    • How to create a sensor?
    • How to create an effector?
    • How to setup the engine?
    • How to edit a scenario?
    • How to run a scenario?
    • How to?
  • Relation engine
    • How to create a type?
    • How to create a relation?
    • What are queries? How do queries work?
    • How to be notified when a relation is executed?
  • Highlight Manager
    • How to create a Highlight?

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}");
}
INSA     IRISA     Inria     Ouest Valorisation Back to top