User Tools

Site Tools


rainelements:basicmemory

Basic Memory

Properties

  • Use Unity Messages (Advanced): Whether the AI is initialized and updated by Unity or done manually.
  • Use Fixed Update (Advanced): Whether the AI responds to Unity Update or FixedUpdate messages. Only valid if Use Unity Messages is checked.
  • Is Active (Advanced): Whether the AI is currently updating.
  • Body: The GameObject that the AI will move and animate.
  • Memory: The type of memory used. Currently RAIN supports only one memory type, Basic Memory (this one). If you create your own Custom Memory you can select it here.

Actions

  • Add Variable: Allows you to add variables to the AI memory. Basic Memory currently supports the following types:
    • int: A whole number
    • float: A decimal number
    • bool: A true/false value
    • string: A text string (it does not need double quotes around it)
    • Vector2: A Unity Vector2
    • Vector3: A Unity Vector3
    • Vector4: A Unity Vector4
    • Color: A Unity Color
    • GameObject: A Unity GameObject

Usage

Creating variables in the editor is useful when you need your AI to have preexisting knowledge of certain things. Maybe this is the location of their base, or several waypoint routes they should patrol, or where their team members are. It can also be useful for generic items that are used in Custom Actions that may change with each instantiation even though they use the same prefab (prefabs, assets, etc).

You can access any variables you create in Behavior Trees, Custom AI Elements, and in any MonoBehaviour through the AI Rig component.

Code

If you are using a Custom Action, the Start, Execute, and Stop functions are given an AI that they can use to access the memory and other elements.

using RAIN.Action;
using RAIN.Core;
using UnityEngine;
[RAINAction]
public class GetAmmo : RAINAction
{
    public GetAmmo()
    {
        actionName = "GetAmmo";
    }
    public override ActionResult Execute(AI ai)
    {
        // We don't check this because we assume the behavior tree did
        GameObject tAmmo = ai.WorkingMemory.GetItem<GameObject>("AmmoTarget");
        SoldierAmmo tSoldierAmmo = tAmmo.GetComponent<SoldierAmmo>();
        // If we already have ammunition, add to it
        if (ai.WorkingMemory.ItemExists("MyAmmo"))
        {
            SoldierAmmo tCurrentAmmo = ai.WorkingMemory.GetItem<SoldierAmmo>("MyAmmo");
            tCurrentAmmo.IncreaseAmmo(tSoldierAmmo);
        }
        // Otherwise create it fresh
        else
        {
            SoldierAmmo tCurrentAmmo = new SoldierAmmo();
            tCurrentAmmo.IncreaseAmmo(tSoldierAmmo);
            // You can't add custom components like SoldierAmmo in the RAIN editor,
            // but you can add them in code like this
            ai.WorkingMemory.SetItem<SoldierAmmo>("MyAmmo", tCurrentAmmo);
        }
        // Unspawn the ammo
        tSoldierAmmo.Unspawn();
        return ActionResult.SUCCESS;
    }
}

If you are not in a Custom Action node, you can still access and modify memory by going through the AI Rig.

using RAIN.Core;
using UnityEngine;
public class InitializeSoldier : MonoBehaviour
{
    [SerializeField]
    SoldierAmmo _startingAmmo = null;
    void Start()
    {
        // Get our AI rig
        AIRig tRig = GetComponentInChildren<AIRig>();
        // Our memory isn't on the rig itself, but in an AI object held by the rig
        if (_startingAmmo != null)
            tRig.AI.WorkingMemory.SetItem<SoldierAmmo>("MyAmmo", _startingAmmo);
    }
}

See Also

rainelements/basicmemory.txt · Last modified: 2022/09/29 15:54 by sigil