1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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).

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$
    – Nick
    Apr 16, 2012 at 15:02
  • \$\begingroup\$ If it receives data while the (blocking, non-sleeping) tick processor is running, then it is delayed, yes. \$\endgroup\$
    – MarkR
    Apr 16, 2012 at 22:34
  • \$\begingroup\$ The "ticks" or how often you update your clients (browser) depends on the type of game i suppose. Think about it this way the amount of ticks per second can be your FPS (frames per second). I have my game running at 30fps. Also you should think that all players are getting this update - do they all need the update? For example does a player on the left side of the map need to know the persons coordinates on the right side of the map even though their viewport dimensions limit them seeing like 600 pixels on each side of them - and the map is 50000000000 pixels wide? \$\endgroup\$
    – Radmation
    Jan 23, 2017 at 22:40

You must log in to answer this question.

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