Search Unity

Should my action planner be generating massive amounts of garbage?

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Nov 20, 2018.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm working off of an (admittedly old) GOAP tutorial at https://gamedevelopment.tutsplus.co...d-action-planning-for-a-smarter-ai--cms-20793 . I simplified a lot of the code and changed the key value pair from <string,object> to <enum,object> for faster indexing, but I find that generating plans is still absurdly inefficient: with three preconditions and ten starting actions to path through generating a single plan hitches the game for a good 2-3 seconds and triggers a huge GC spike in the profiler, and trying to path through any larger number of actions tends to freeze the game entirely.

    So is this a problem with the tutorial, or a fundamental limitation of action planning that can't be optimized around? My original case only generates one plan every few minutes (no realtime/asynchronous AI ticks), but it requires dozens or low hundreds of actions (I'm basically working on a monster hunter-esque creature AI in which every interactable bit of flora/fauna has goap actions associated with it). It occurs to me that even really slick implementations of action planning, like what they did in FEAR, tends to restrict itself to 10-20 actions divided into hierarchies- is this a case where the technology is simply never going to be a good match for my use case, no matter how much time I spend trying to optimize it?
     
  2. I don't think that GOAP would be impossible to do right. Admittedly I haven't done it in Unity ever, so I'm just stating the obvious:
    - you should avoid strings as much as possible (they're basically GC-source)
    - try to minimize the creation and instantiation of new objects during the execution (and during the planning as well of course)

    The frequent creating-abandoning or destroying objects are also excellent source of GC.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Hmm, even with no string usage and no object instantiation, GC takes an enormous hit. I probably just have to play with my graph building logic in that tutorial's GoapPlanner.cs, since I'm pretty sure that's where the lurch is.. am I correct that building a node graph with two hashset<keyvaluepair<int, bool> to store conditions and effects, then just using A* to find the least-distance route through said graph is going to be the most-ish efficient approach?