Custom Key
Instruction to create custom user key
In the AI Tree you can easily create your own task by using scripting API. In this page we will show how you can do it.
In this context, we believe that you have already read the previous pages of User Scripting section.
In the previous section, we created a task for the platform moving. Let's create unique key for defining point A, B and speed.
Create new script and call it PlatformKey, after creation open it script in you scripts editor tool.
Create -> MonoBehaviour Script

Default Unity MonoBehaviour script template.
using UnityEngine;
public class PlatformKey : MonoBehaviour
{
// Start is called once before the first execution of Update
// after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
First of all we need to decide what we will define as key. Let's create new struct PlatformData. Don't forget to add [Serializable]
attribute for custom types.
using UnityEngine;
using System;
[Serializable]
public struct PlatformData
{
public Vector3 pointA;
public Vector3 pointB;
public float speed;
}
public class PlatformKey : MonoBehaviour
{
// Start is called once before the first execution of Update
// after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Now we are ready to define our PlatformData key. We need to change the PlatformKey class.
Remove all methods.
Change
MonoBehaviour
parent class toKey<PlatformData>
using RenownedGames.AITree;
using System;
using UnityEngine;
[Serializable]
public struct PlatformData
{
public Vector3 pointA;
public Vector3 pointB;
public float speed;
}
public class PlatformKey : Key<PlatformData> { }
Done. It's all required steps to define your our key in AITree. Any other type can be used instead of the PlatformData type.
Now we can update MovePlatformTask class.
using RenownedGames.AITree;
using UnityEngine;
[NodeContent("Move Platform", "Tasks/Custom/MovePlatformTask")]
public class MovePlatformTask : TaskNode
{
[SerializeField]
private PlatformKey platformKey;
private Transform transform;
private Vector3 destination;
protected override void OnInitialize()
{
base.OnInitialize();
transform = GetOwner().transform;
destination = platformKey.GetValue().pointB;
}
protected override void OnEntry()
{
base.OnEntry();
PlatformData data = platformKey.GetValue();
if(destination == data.pointA)
{
destination = data.pointB;
}
else
{
destination = data.pointA;
}
}
protected override State OnUpdate()
{
if(Vector3.Distance(transform.position, destination) > 0.1f)
{
float speed = platformKey.GetValue().speed;
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
return State.Running;
}
return State.Success;
}
}

We can improve displaying of PlatformKey in editor. For that we will use Apex library what included in AITree out of box.
Open PlatformKey class add following attributes on field of PlatformData struct.
using RenownedGames.AITree;
using RenownedGames.Apex;
using System;
using UnityEngine;
[Serializable]
public struct PlatformData
{
[HideLabel]
public Vector3 pointA;
[HideLabel]
public Vector3 pointB;
[LabelWidth(100)]
public float speed;
}
public class PlatformKey : Key<PlatformData> { }
Now our PlatformKey looks much better.

Last updated