News Forums RAIN General Discussion and Troubleshooting [Solved] Dynamically set BTAsset of BasicMind in Editor Script

This topic contains 5 replies, has 2 voices, and was last updated by  tgraupmann 1 month, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39715

    tgraupmann
    Participant

    What is the proper way to dynamically set the behaviour on a BasicMind through editor script?

    Here’s the script I am using, but the BasicMind keeps switching back to None when I hit play in the editor.

    [MenuItem("RAIN/Setup AIRig and assign mind")]
            private static void SetupScaryZombiePack()
            {
                GameObject gameObject = Selection.activeGameObject;
                if (null == gameObject)
                {
                    Debug.LogError("Select the character's animator in the scene!");
                    return;
                }
                RainEnemy enemy = gameObject.GetComponent<RainEnemy>();
                if (null == enemy)
                {
                    gameObject.AddComponent<RainEnemy>();
                }
                AIRig aiRig = gameObject.GetComponentInChildren<AIRig>();
                if (null == aiRig)
                {
                    aiRig = AIRig.AddRig(gameObject);
                }
                BTAsset btAsset = (BTAsset)AssetDatabase.LoadAssetAtPath("Assets/ScaryZombiePackAI/AI/WaypointVisualSensorMovement.asset", typeof(BTAsset));
                if (null == btAsset)
                {
                    Debug.LogError("Failed to find WaypointVisualSensorMovement.asset!");
                    return;
                }
                BasicMind basicMind = (BasicMind)aiRig.AI.Mind;
                if (null == basicMind ||
                    !(basicMind is BasicMind))
                {
                    Debug.LogError("AI Missing Basic Mind!");
                    return;
                }
                basicMind.SetBehavior(btAsset, new List<BTAssetBinding>());
                basicMind.AIInit();
            }

    If I script adding the AIRig before assigning the behaviour, the behaviour tree asset won’t stick and keeps going back to none even though the BTAsset was loaded and referenced.

    It’s simple as I want to script having an editor script add the AIRig (works) and then assigning the BasicMind (doesn’t work).

    Dragging and dropping the script in the editor inspector works fine. I just want to replicate the manual steps.

    I tried loading from resources and asset database like this example.
    http://rivaltheory.com/wiki/rainelements/basicmind

    I know I’ve loaded the BTAsset properly and I can select it in the inspector.

    Selection.activeObject = btAsset;

    Feedback is appreciated!

    Thanks,

    ~Tim Graupmann

    • This topic was modified 1 month, 1 week ago by  tgraupmann.
    • This topic was modified 1 month, 1 week ago by  tgraupmann.
    #39719

    tgraupmann
    Participant

    It seems like

    basicMind.SetBehavior(btAsset, new List<BTAssetBinding>());

    expects to be in `play` mode and doesn’t like to be set at `edit` time from a [MenuItem]???

    • This reply was modified 1 month, 1 week ago by  tgraupmann.
    • This reply was modified 1 month, 1 week ago by  tgraupmann.
    #39722

    tgraupmann
    Participant

    Here’s a video on the issue.

    #39733

    tgraupmann
    Participant

    I also added an isolated repro project here:
    [RainMindDemoReproProject.unitypackage]

    #39741

    Sigil
    Keymaster

    Sorry about that, you’ll need to call aiRig.Serialize() after making changes in script (with most things involving RAIN at least). That should fix your issue.

    If you’re working on a prefab, you may also need to call PrefabUtility.RecordPrefabInstancePropertyModifications(aiRig).

    #39742

    tgraupmann
    Participant

    Ah I had tried calling Serialize on the Mind, just not the rig. That worked like a charm on my repro project. Thanks so much!

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

You must be logged in to reply to this topic.