News Forums RAIN Sample Projects, How To, and Code Enable/Disable Canvas or Image

This topic contains 2 replies, has 2 voices, and was last updated by  Mad_Mark 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39850

    Mad_Mark
    Participant

    Greetings,

    I have a BT that is working on an NPC to detect a zombie, and when seen, the NPC entity flees to a random location.
    I have a canvas over the NPC’s head holding an image of “!” that I would like to enable when the zombie is seen, and disable when out of range. Any idea how I might do that? Does that have to be done in a Custom Action? <how?>

    Mark

    #39857

    Sigil
    Keymaster

    Custom action would be best, something like this:

    using RAIN.Action;
    using RAIN.Representation;
    using UnityEngine;
    [RAINAction]
    public class SetCanvasVisibility : RAINAction
    {
        public Expression Target = new Expression();
        public Expression Value = new Expression();
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            if (!Target.IsValid || !Value.IsValid)
                return ActionResult.FAILURE;
            GameObject tTarget = ai.WorkingMemory.GetItem<GameObject>(Target.VariableName);
            if (tTarget == null)
                return ActionResult.FAILURE;
            Canvas tCanvas = tTarget.GetComponentInChildren<Canvas>();
            tCanvas.enabled = Value.Evaluate<bool>(ai.DeltaTime, ai.WorkingMemory);
            return ActionResult.SUCCESS;
        }
    }

    If you want something a little less jarring, I would add a Canvas Group to the Canvas, and fade the alpha on that. I’d probably create a small component with FadeIn/FadeOut functions to handle the actual fading on the Canvas Group, and just look for that on the target instead of the Canvas itself.

    #39904

    Mad_Mark
    Participant

    Thanks again Sigil. Works like a charm.

    RAIN is slowly starting to make sense to me, or I am finally making sense of RAIN. I think I set my sights too high at the start, and tried to make it do to much at once. Gets a tad confusing. I have managed to pull together about a dozen simple little behaviors, and am starting to integrating them into a more “complete” AI…

    So, what are the best practices that I should be aware of as I progress. Do folks build one big-butt behavior tree, or implement multiple AI elements on a single object? Is that even do-able? Is there a use case?

    Cheers,
    Mark

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.