Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Currently I am creating an MMO using Node.js and Socket.IO.

The node server needs to handle connections for players, and also use a loop to update all monsters positions in my game and let them attack players in range.

For the loop I read that you can use process.nextTick or use a child process, which option would be the best choice? What are the advantages/disadvantages of using each?

Also, how many times per second should I run this loop for the monsters?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I am assuming here that you're keeping your world data in memory (with occasional load/save).

As it's in memory, you'll need to do the work in the same process, or you won't be able to have a consistent view of the data. So you'll want to set up a timed task on the server to handle MOBs etc.

You don't use process.nextTick, just set a timer using one of the (several) methods to do this in Node.

Because Node is a non-preemptive multitasking environment, you don't need to worry about consistency between threads, the "process monsters" function won't get interrupted unless it sleeps (e.g. doing network IO or something).

How often should you run the ticks? As infrequently as possible. I would suggest starting at 1 tick per second and going from there (the client can still show animations, etc, as much as it wants in between).

share|improve this answer
    
Interesting, thanks! So I would just run a function at startup and let it call itself every second using setTimeout()? What would happen however if a client connects while the game loop is running? Will it simply get delayed? –  Nick Apr 16 '12 at 15:02
    
If it receives data while the (blocking, non-sleeping) tick processor is running, then it is delayed, yes. –  MarkR Apr 16 '12 at 22:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.