# AI of Turn Based game using Entity System approach

I just recently learned about Entity Systems and want to try it out on a real example (I was developing a game recently and want to try to port it over Entity Systems). But I got problem right away. Actually that is two questions in one.

First would be:

In traditional OOP approach I had game loop which was picking action to do for idling units every 100ms (so-called 'turn'). But rendering obviously was run at every game loop iteration. How should I do that? Maybe I need something like individual interval for systems to run? Like RenderingSystem running every Update while AI Systems are running each 100ms. Or maybe there is some better approach which I should use?

I understand how to make AI which will manage every unit completely individually. However I have somewhat simultaneous pathfinding for all units which will do:

1. Mark units that should start moving as 'moving'.
2. Find path for every 'moving' unit to it's target (basically closest enemy). All 'moving' units aren't considered as obstacles for path but all not-'moving' units considered as obstacles.
3. Try to move every unit one step at it's path. If can't make step (someone is already standing there) then mark path as invalid (to recalculate at next AI iteration) and move one step in direction of target (if there is free nodes).
4. Mark all 'moving' units as not-'moving'

Besides there is addition: 3. Before making step we will check if we have enemy around us and instead of moving we will attack that enemy.

So, one AI iteration required to give action to each unit which will be run until it finishes. Then AI should give next action.

How I can make that in Entity System approach? Because in ES all systems are run one-after-another where I need to "jump" beetween them freely. Or maybe I'm overcomplicating things and should do something different instead of "jumping" between systems?

• Not trying to be rude, but you should use some question marks to denote the questions. – Den Jul 11 '13 at 20:02
• I agree, I have no idea what you are asking, because your post does not seem to contain any questions. – Anonymous Entity Jul 15 '13 at 10:32
• I denoted questions. Sorry, english isn't my own language. – bobby Jul 15 '13 at 12:59

RenderingSystem running every Update while AI Systems are running each 100ms.

This shouldn't be a problem. For example, simply have your AI systems accumulate the delta passed to them, and only take action when it exceeds 100ms. I.e. four (dramatically unstable fps) loops could look like this:

aisystem.update(20ms) -> 20ms
aisystem.update(21ms) -> 41ms
aisystem.update(60ms) -> 101ms, take action -> 1ms
aisystem.update(20ms) -> 21ms


Most likely this is not the best way to go about it, but it is one possible implementation.

Or maybe I'm overcomplicating things and should do something different instead of "jumping" between systems?

Correct, you don't want to be jumping between systems. The way I see it, you described one system, not a group of systems. This one system could be called something like AiMovementSystem. And it simply does what you described in your question. I believe you are a bit confused with the definition of System. A System basically iterates through a group of Entities with correct Components -- you don't have a movement system for each entity that should move.

• First question is gone. Thanks. I edited second question, could you look one more time please? I described way to move units in current OOP approach and I don't know how to do same thing in ES. – bobby Jul 15 '13 at 13:01
• @bobby I updated the answer. Hope it helps. – vijoc Jul 15 '13 at 14:17