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 :
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.
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();
}
}