News Forums RAIN General Discussion and Troubleshooting How to access Behavior Tree Asset?

This topic contains 3 replies, has 2 voices, and was last updated by  prime 7 months, 4 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #34191

    dmortensen1
    Participant

    Hi there, i wish to access the Behavior Tree Asset through code, because i am going to instantiate several enemies which each has it’s own waypoint, but the thing is i am instantiating the same enemy prefab onto several different areas in my scene!

    But i do not know how to get access to the Behavior Tree Asset, in the Mind area of the AI GameObject through code…

    like if i could get access to it like a normal calling of an GameObject / Prefab that would be cool but i just don’t know how to… i personally haven’t been touching the RAIN asset through script yet as it looks terrifying to me :(

    If anybody knows that would be awesome :) i appreciate any kind of help i can get 😉
    ~David

    #34195

    prime
    Keymaster

    This is pretty much all you need. You will have to have a relative path to the asset and access to the AI Mind. The second argument to SetBehavior is a List of behavior tree bindings, but I’ll assume you don’t have any.

    BTAsset tAsset = Resources.LoadAssetAtPath<BTAsset>(pathToAsset);
    AI.Mind.SetBehavior(tAsset, new List<BTAssetBinding>());
    #34196

    dmortensen1
    Participant

    Thanks prime :) but one question, those two lines of code, are they meant to be implemented onto my enemy prefabs or is it meant to be called from another game object? Thanks in advance!

    ~David

    #34304

    prime
    Keymaster

    I would put it right with the code that instantiates the prefab.

    GameObject tObj = PrefabUtility.InstantiatePrefab(tYourPrefabAsset) as GameObject;
    AIRig tRig = tObj.GetComponentInChildren<AIRig>();
    BTAsset tBTAsset = Resources.LoadAssetAtPath<BTAsset>(pathToAsset);
    tRig.AI.Mind.SetBehavior(tBTAsset, new List<BTAssetBinding>());

    something like that…

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

You must be logged in to reply to this topic.