Enable/Disable Individual AI at Runtime

  • July 17, 2017 at 14:38 #21415
    shawn.m.wignall
    Participant

    I have about 25 small AI for each unit. At the root of each one I have a quick “bail-out” check to see if the rest of that AI needs to be executed, to try and avoid wasted processing. What I’ve found is that I still have a lot of issues. When I actually disable the unneeded AI in the inspector and run I have significantly better performance, even though there is very little in those AI that happens.

    Question: Can I enable/disable the individual AIs at runtime? I’d like to turn off most of them when they aren’t needed, and then turn them back on when they are.

    July 18, 2017 at 17:03 #21429
    creo
    Participant

    It’s possible to access the individual AIs. For example, if you wanted to pause all of them, you could add a using directive for Apex.AI.Components, then do something like this:

    August 1, 2017 at 11:20 #21580
    Rami
    Keymaster

    Hi Shawn,

    Thanks for posting, I apologize for the late response. Also thank you Creo for chiming in. You are very correct in your example.

    I just wanted to add that there are more ways to enable and disable AIs: Toggle the component state (Component.enabled), Toggle the GameObject state (GameObject.SetActive), call Stop() or Pause() on the UtilityAIComponent or on the individual clients. The first two are standard Unity ways of enabling/disabling objects, and Apex Utility AI will respect these.

    January 25, 2018 at 21:38 #22669
    Marcus
    Participant

    Hello,

    i´m also working with smaller AI´s and i want do enable/disable them during runtime. Two questions:

    – whats the best way to start a Unit with a certain setup of enabled/disabled AI´s? Whatever i disable during Awake/Start it gets enabled by once the game is started. At which in the Unity-Lifecycle is Apex enabling all attached and enabled (gameobject/component level) AI´s, so i can switch them off again?

    – whats the exact difference between Pause() and Stop()? I was hoping that Stop also prevents Apex from Starting them (since the doc says that they get ungeristered from the AIManager) but it seems, that they start in state “stopped” anyway and get enabled and registered sometime during the startup phase.

    January 26, 2018 at 10:20 #22673
    Rami
    Keymaster

    Hi Marcus,

    Thanks for posting.

    The easiest way to ensure a certain setup of AI clients would be to prepare prefabs with those AI clients enabled or disabled as desired. Then when you instantiate the prefab, it will run whatever is enabled.

    The UtilityAIComponent will run all enabled AI clients in OnEnable or Start – whichever comes first (expected to be OnEnable). So if you change the enabled state before that, it may not stick. You may want to write your own UtilityAIComponent if you need more control. Otherwise, delay your setup till after OnEnable and Start have run.

    Pause() will halt the execution and the state will later be resumed from there. Stop unregisters and resets the state completely. The methods should have API documentation as well.

    January 26, 2018 at 19:49 #22677
    Marcus
    Participant

    Hello Rami,

    the idea is to have something like a LOD for the AI, depending on the proximity to the player. If the AIClient is far away, only the most basic AIs are active…. once the Client gets closer or meets certain conditions, it swaps/enables/disables AIs.

    Prefabs: Not really an option for my scenario, if i got you right, since the setup changes during runtime

    Own UtilityAIComponent: I tried that, but it´s pretty clear, that this class is not build around having custom versions of it. You are using namespace internal Objects and the EditorScript is also not made to be hooked onto another UtilityAIComponent. One would have to put a lot of rewriting effort into it, just to override the ExtendedMonoBehaviour methods.

    Derive from UtilityAIComponent: I also tried simply derive from UtilityAIComponent and override the ExtendedMonos, but that also leaves me with a class without your convinient editor-script.

    Add/Remove clients at runtime: I found that the UtilityAIComponent has Add/remove Methods exposed, so i tried adding AI´s during runtime. Maybe i´m doing something wrong, but i got this Error:

    I was simply using this code to add an AIClient:

    ContextProvider and UtilityAIComponent are simply attached to the very same GO.

    It adds the AI, but also adds a second Clients which seems to be broken and looks like this:
    ScreenShot

    • This reply was modified 3 months, 3 weeks ago by Marcus.
    • This reply was modified 3 months, 3 weeks ago by Marcus.
    January 29, 2018 at 10:44 #22687
    Rami
    Keymaster

    It seems to me that you would need to add your AI Clients later than in Awake. As mentioned previously, the UtilityAIComponent initializes in OnEnable or Start – whichever comes first. Awake on another component can, but may not always, come before OnEnable on another component (even on the same game object). Maybe try delaying your own initialization till the AI is definitely done? E.g. 0.1 seconds later. You may also have luck using the Script Execution Order.

    [EDIT] Another idea is to have an “initialization” AI client setup beforehand, which simply adds other AI clients depending on some settings, e.g. a list of names, enums or similar.

    February 7, 2018 at 23:02 #22760
    Marcus
    Participant

    Hello,

    i finally got back to the project and i managed to add AI´s on runtime by delaying the Add-Call, so everything works fine so far. Unfortunatly i lost the Editor-Debug Visualisation this for those AI´s!

    Gizmos/GUI Visualizers work fine, just the Editor-Debugger is not working.

    If i add the AI pre-runtime and it works again.
    If i remove it and add it via code it does not. I tried both overloads for adding, since i expected it maybe to need the one with the ContextProvider, but it didn´t work.

    • This reply was modified 3 months, 1 week ago by Marcus.
    February 8, 2018 at 11:52 #22771
    Rami
    Keymaster

    In attempting to reproduce your issue, I did discover a bug with adding AIs at runtime if there are no AIs pre-runtime. I got exceptions in the console window though, so when you say that the AIs did work this indicates to me that you must have AIs pre-runtime in the list on the UtilityAIComponent and thus have not encountered the same issue that I have.

    Visual debugging worked fine however for AI clients added at runtime, so that issue I cannot reproduce. Perhaps you could share a snippet of the code you use for adding AIs at runtime?

    February 8, 2018 at 12:10 #22773
    Marcus
    Participant

    Hey Rami,

    strange, but my UtilityAIComponent had NO AI´s attached preruntime. In my current setup (Unity 2017.2.1) i got no console errors, but as stated no editor debugging. My Agents do start as disabled gameobjects. For debugging i have some editorbuttons which call InitMe() and Setup() on the Agents:

    -InitMe: enable the gameobject and call InitMe() on each ZombieController

    – Setup: Call Setup() on each ZombieController. The WandererUtil is just using the LoadBalancer and requesting an ApexPath-Path)

    February 13, 2018 at 08:35 #22791
    Marcus
    Participant

    Little update:

    i moved to Unity 2017.3.1f1 and updated Apex AI to 1.0.7.2, but the problem is still present.

    Could you recreate the bug with my setup/code?

    February 13, 2018 at 10:20 #22797
    Rami
    Keymaster

    Sorry for the late reply.

    I don’t have the exact same setup as you do, but I do have several cases where I add AI clients at runtime and they work just fine – as intended – with the runtime visualization working as well.

    However, as mentioned previously I did find a bug with adding runtime AIs to a component that is empty at edit-time. While it doesn’t seem to be the same issue that you have, you could try to test out this updated .dll – we will of course update our products at some point, I’m just not sure when.

    Updated Apex AI Dll

You must be logged in to reply to this topic.