# Am I doing this node.js game loop wrong?

I have 2 arrays of JSON objects, actions and game objects.

At any time a user can make a request from the client which can add an action to the actions array.

I have a setInterval(function(){ }, 1000); that runs every second and first loops through all of the actions, doing them in order then dumping emptying the array. Then it for loops (on the length of the array) through all the objects and does change processing such as healing, repairing, consuming resources, updating location of travelling objects, etc.

Each game object is tied to a user and when it is done being modified it will push out to them using socket.io all the data they need for their current view (assuming they are in an active session still).

Is this right? Am I not taking advantage of the node.js event loop as well as I could for this?

• That depends on type of game, if you have game that is entirely event based (like tick tack toe) or something that can be made to look like event based (storing when action is going to finish, and finish it after someones query it - this could work for stuff like cooldown for spells, but not healing after time) Then you can have everything based just on events. If you have realtime game, like mmorpg, you need that loop. Btw when using setInterval like this will not work when time to process everything will be bigger then 1000. – Kikaimaru Dec 23 '12 at 18:05
• Healing over time with a delay can totally be query/event driven. If you can make a function to calculate absolute given an input of elapsed time, all you need is to store the previous value and the it was calculated at. – Sean Middleditch Oct 4 '13 at 21:29