1
\$\begingroup\$

What is the best way to run AI in Cocos2d?

I already have my AI implementations. Normally I would execute AI in my game loop by checking how much time has elapsed since last AI execution and whether I can execute it in the current situation, then finally execute it.

From my understanding, Cocos2d removes the concept of the GameLoop and replaces it with Actions, which is great, but I'm not sure how best to employ them to take over in this AI execution. My current workaround is to create a ccSequence that 1) Executes AI, 2) Executes another ccSequence that does the same thing (essentially make an Action loop). What I don't like about this is that Actions all have to be timing needs to be precise in Actions... but I can work with that.

Another thing I don't like is that while this is good for AI... what about rendering actions. I can easily render the result of my Action for me, but what about the result of another AI's action? Such as another AI killed me. I have to wait for my Action Sequence to execute before I can check if anything happened to me, but what if my action was 10 seconds long... do I stand there before I realise I should be dead?

I'm thinking now that I could have multiple Action sequences set up. One for my AI. One for general updates to me. But before I go too far down this path, are Action Sequences really a good replacement for ye olde Update methods?

\$\endgroup\$
2
  • \$\begingroup\$ Refer to my other answer. Cocos2D is event-based rather than loop-based, in this sense. Not ideal when it comes to game development, is it? :) Nothing worse than tying your developers into a restrictive development paradigm. \$\endgroup\$
    – Engineer
    Dec 31, 2012 at 0:44
  • \$\begingroup\$ Haha, stop reminding me :) While it is restrictive, I still want to give it a go as it has saved me a lot of time pumping out the UI side of things (despite this current Conundrum) \$\endgroup\$
    – Arkiliknam
    Dec 31, 2012 at 10:40

1 Answer 1

2
\$\begingroup\$

I'm making an assumption that this is the Cocos2d-iphone branch.

Cocos2d allows you to schedule an update that happens every frame, just like a game loop. If you schedule this update on your scene or some base layer/node that always exists, you can carefully execute your AI actions first, then check/update your game state. I find this to be a really good place to manage AI and game states.

Alternatively, you can schedule updates at specific intervals instead of every frame, and you can also specify the priority of the update callback, so you can make sure that your update method runs before or after everything else during the current frame and before rendering.

See the documentation on scheduling updates/selectors.

Personally, I do all my game logic and AI in the scheduled updates, not actions (with a few exceptions, like changing status after an action completes). True, you can manage a collection of actions and stop them prematurely if necessary, but it does get a little painful sometimes and the smoothness of your game can suffer. For myself, I often use custom actions that perform relative movements/changes, instead of absolutes, so they could be combined and canceled without messing up the visuals.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your answer. I'm actually using Cocos2d-xna, but was assuming the approach would be the same for any platform. I'll look into the Update scheduling you mentioned. It sounds pretty much what I was hoping for, as I feel a bit uneasy using Actions for everything. \$\endgroup\$
    – Arkiliknam
    Dec 31, 2012 at 10:37
  • \$\begingroup\$ Ah, Cocos2d-x is based off (and synchronized with) Cocos2d-iphone, so it should be exactly the same. Good luck! \$\endgroup\$
    – Levi
    Dec 31, 2012 at 15:12

You must log in to answer this question.

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