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 create an Relation ?

A Relation describe a behavior between types. It is characterized by a script inheriting from XURelation.

To show you have to create a relation, we will take the example of relations present in the tutorial.

The first example is RelationSwapColor. This relation takes two TypeColor, including one of TypeUser.

If we take the object containing the relation, the script component looks like this :

Relation SwapColor

Code explained

Now, let's look at the code!

First, the class inherits from XURelation

	using System;
	using Xareus.Relations.Unity;

	public class RelationSwapColor : XURelation

	    [ObjectPattern("TypeCube", "Cube")]
	    public TypeColored ColoredObject1;

	    [ObjectPattern("User", "Sphere")]
	    public TypeColored ColoredObject2;

	    [ObjectPattern("User", "User")]
	    public TypeUser UserObject;

IsRunnable() is an abstract method of XURelation. If this method return true it will call the method Run(Action). So, here you are putting all the conditions necessary for the relation to run.

RelationSwapColor waits for two object of type TypeColored to be in collision.


	    public override bool IsRunnable()
	    {
	        return ColoredObject1.hasBeenCollided && ColoredObject2.hasBeenCollided;
	    }

The Run(Action) method contains the action of the relation. In the example, the color swapping between two colliding objects.

Important

Do not forget to call the callback at the end of the realtion, unless you know what you are doing.

	    public override void Run(Action resultCallback)
	    {
	        ColoredObject1.SwapColor(ColoredObject2.gameObject);
	        resultCallback();
	    }

Add relation in the scene

A relation need to be in the scene hierachy to be recognize by the relation engine. It should be separated from other relation e.g. one GameObject per Relation, as shown in the image below of the tutorial.

Relations in the hierarchy
Note

Make sure every relation got an ID. You can give them an ID or auto-assign it with the Xareus menu in Unity.

Full code

	using System;
	using Xareus.Relations.Unity;

	public class RelationSwapColor : XURelation
	{
	    [ObjectPattern("TypeCube", "Cube")]
	    public TypeColored ColoredObject1;

	    [ObjectPattern("User", "Sphere")]
	    public TypeColored ColoredObject2;

	    [ObjectPattern("User", "User")]
	    public TypeUser UserObject;

	    public override bool IsRunnable()
	    {
	        return ColoredObject1.hasBeenCollided && ColoredObject2.hasBeenCollided;
	    }

	    public override void Run(Action resultCallback)
	    {
	        ColoredObject1.SwapColor(ColoredObject2.gameObject);
	        resultCallback();
	    }
	}
INSA     IRISA     Inria     Ouest Valorisation Back to top