News Forums RAIN Sample Projects, How To, and Code Accessing variables on scene objects other than AI objects

This topic contains 1 reply, has 2 voices, and was last updated by  Sigil 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39004

    quest_2016
    Participant

    Hello,
    I have an AI character detecting then chasing a non-AI Player. I need to check the Player’s health so that the AI stops attacking if the health falls to 0.
    In a separate post, you state:
    – Pull direct: When your AI detects an enemy/player, it could look for a particular script on the game object. If found it could access a public variable or method called Health() to get the value.

    Could you please explain this in detail? How should I look for the script on the Player? How to pass the health value back to the BT node?

    In that post you also mention a method of doing the above using sensors and Aspects… How is this done? Is it the same method as the above?

    Help appreciated.

    #39019

    Sigil
    Keymaster

    You would need to define a custom action in your behavior tree and a component on your player to hold the value. So assuming you have never done this before I’ll go from the top:

    In the behavior tree editor add a custom action to your tree, right click, Create->Actions->Custom Action.
    On the Custom Action select the drop down for Class and choose Create Custom Action.
    Fill out a name and choose a language, and hit OK.
    This creates an action in the folder AI/Actions

    The action can do almost anything, but in your case it’s going to try to access some health component to see your current health. The component might look like this:

    using UnityEngine;
    public class HealthComponent : MonoBehaviour
    {
        [SerializeField]
        private int _health = 10;
        public int Health
        {
            get { return _health; }
            set { _health = value; }
        }
        // Some code that modifies your health would be elsewhere, maybe in an update or function
    }

    And the action might look like this:

    using RAIN.Action;
    using UnityEngine;
    [RAINAction]
    public class CheckHealth : RAINAction
    {
        private HealthComponent _targetHealth = null;
        public override void Start(RAIN.Core.AI ai)
        {
            base.Start(ai);
            _targetHealth = null;
            // This was detected and assigned to the Form Variable "target"
            GameObject tTargetObject = ai.WorkingMemory.GetItem<GameObject>("target");
            if (tTargetObject != null)
                _targetHealth = tTargetObject.GetComponent<HealthComponent>();
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            // Look at the component and return failure if it has no health
            if (_targetHealth == null || _targetHealth.Health <= 0)
                return ActionResult.FAILURE;
            return ActionResult.SUCCESS;
        }
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.