I'm working on a turn based game in Unity. I need to perform field/grid analysis(2D grid) when i'm iterating trough each unit "entity". The A.I. should be able to plan ahead and therefor needs to consider the components each unit have. Not every unit will have the same ability. These abilities are defined as components.

I want to make it flexible in the sense that I can plug in new abilities and my A.I. will take it into consideration.

While user control is somewhat implemented, A.I. hasn't even been written. So I'm open to any suggestion.

Q : What is a way for the A.I. system to collect all abilities of one unit and make decisions depending on each component?

edit: I know there is the "null" checking method to see if the gameobject contains a component. But this is highly inflexible because you are hard coding a stack of if else. Something I want to prevent.

  • \$\begingroup\$ One approach could be to simulate the game in advance trying out different decisions and rating the outcome. That's (overly simplified) how AIs for games like chess work. \$\endgroup\$
    – Philipp
    Apr 9, 2013 at 7:24
  • \$\begingroup\$ @Philipp I came to that conclusion myself. But that's not what I asked though =). \$\endgroup\$
    – Sidar
    Apr 9, 2013 at 7:25
  • \$\begingroup\$ Answering the "how to put it into components" question first without even knowing anything about the AI system is shooting in the dark. Tell us more about your AI system, the answer depends on it. \$\endgroup\$ Apr 9, 2013 at 7:49
  • \$\begingroup\$ @MaikSemder Im not asking how to put it in components. I'm asking how I could setup my A.I. system in such a way it knows what Components my gameobjec contains. And make decisions based on that. I Don't have a A.I. system yet because I'm trying to understand how I could make it work best in combination with components. \$\endgroup\$
    – Sidar
    Apr 9, 2013 at 7:56
  • \$\begingroup\$ What is common between abilities in a way your AI handles them? Can't you just make them implement common interface? \$\endgroup\$
    – Den
    Apr 9, 2013 at 12:01

1 Answer 1



Polymorphism is probably one of the primary solutions to component based algorithms. I do not know how well unity can handle this sort of thing but this is likely going to lead you in the direction you are looking for.

The idea is each ability a unit has is derived from a basic ability class, and added into an ability list, then the AI can look at each ability in the list and determine how to handle the ability.

Your best bet is to have a function in said ability that returns a data structure to the AI that contains analysis data the AI can use to learn about the ability.

  • \$\begingroup\$ Thanks for your input. Unity does handle Interfaces pretty well. You can request the components implementing the interface by passing the interface type just like you would pass the component type (gameObject.GetComponent<MyInterface>() ). While some stuff is bound to be "hardcoded" it feels like most of my A.I. is going to be hardcoded and hardly flexible in the end. I just don't have a clue on how to make the A.I. "understand" what an Attack component should represent opposed to a Walk component. \$\endgroup\$
    – Sidar
    Apr 9, 2013 at 18:20
  • \$\begingroup\$ I don't know the specifics of how you want your AI to behave, but in most cases, AI do not need to know all the complexities of each ability that may be coming at them (or do they?). If you spend too much time analyzing every action made by the enemy then you risk bogging down the game or having an AI be "too intelligent." \$\endgroup\$ Apr 9, 2013 at 20:35
  • \$\begingroup\$ Well, you can always dumb down the A.I. . Since it's a turn based mechanic risking to bog it down seems fairly slim. I'm still looking into it. I'll leave the question open for now. But ill try to work it out with a polymorphic approach. \$\endgroup\$
    – Sidar
    Apr 10, 2013 at 7:30
  • \$\begingroup\$ I think I have an idea. Have some points! \$\endgroup\$
    – Sidar
    Apr 10, 2013 at 7:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .