Search Unity

Converting a tree structure (Utility AI)

Discussion in 'Entity Component System' started by TommiH, May 25, 2018.

  1. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    Hello,

    I'm considering moving some of my code over to ECS / Jobs, but I'm finding it difficult to figure out how to do it for many of my current designs. Especially difficult: my treelike Utility AI structure.

    The structure consists of:

    - Nodes: each node either has child nodes or refers to an action. The execution starts at the root, selects one of its children, that in turn selects one of its children, etc. until we reach an action.

    - Scorers: each node has one or more scorers that are used to determine how desirable that node is. All scorers share one interface but there are a lot of different types of scorers, implemented via polymorphism. Each type of scorer has its own logic and usually some public fields for setup. Most of them are pretty simple, and I have about 50 of them at the moment.

    All of these are currently implemented as separate MonoBehaviours on separate GameObjects, but I could easily convert it into pure object-oriented code.

    My questions about this:

    1) How would I represent and traverse this tree? As a structure of entities with either Node or one of many Scorer components on them? I assume a Node would not be able to directly reference its child nodes or Scorers, so would I always have to explicitly traverse it via the Entity?

    2) Should I create a separate component type for each scorer subclass, with no actual interface to bind them? How about systems? Should I create a different system for each scorer?

    3) In what order should I run this? I can't just iterate over all the scorers first and then all the nodes, because I have to follow the tree structure. (For one thing, iterating over nodes and scorers that aren't even going to be considered would be a waste. And a node needs to know if it's been selected or not in order to be updated properly, so the tree has to be traversed top down.)

    I would appreciate any help. I have difficulties seeing how to convert many other parts of my code too, but this is a particularly clear example.
     
    one_one likes this.