0
\$\begingroup\$

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?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Kikaimaru
    Dec 23, 2012 at 18:05
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 4, 2013 at 21:29

1 Answer 1

2
\$\begingroup\$

It's better to use setImmediate() or process.nextTick() for game loop. setImmediate vs. nextTick

\$\endgroup\$

You must log in to answer this question.

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