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.
-
AuthorPosts
-
December 13, 2022 at 3:48 pm #39715
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/basicmindI 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.
December 13, 2022 at 5:43 pm #39719It 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.
December 13, 2022 at 7:34 pm #39722Here’s a video on the issue.
December 14, 2022 at 9:58 am #39733I also added an isolated repro project here:
[RainMindDemoReproProject.unitypackage]December 14, 2022 at 12:13 pm #39741Sorry 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).
December 14, 2022 at 12:19 pm #39742Ah I had tried calling Serialize on the Mind, just not the rig. That worked like a charm on my repro project. Thanks so much!
-
AuthorPosts
You must be logged in to reply to this topic.