Jump to content

Loop "ticks" in game server


Midraks
 Share

Recommended Posts

Hi. I building a game server in NodeJS. I need help to create a loop for IA, "ticks", I was read but I dont understand how its works. My game run at 60FPS, so I thought about recreate it on the server.


 



setInterval(function() {

  for (npc in npcs) {

    //npc moves every 250ms

    if (+Date.now() - npc.timeMove >= 250) {

      npc.timeMove = +Date.now();

      npc.pos.x++;

    }

  }

}, 1000 / 60); //FPS


 

I dont think that is optimal for the server to do it this way. How I can do it?


Sorry for my english.


Thanks.


Link to comment
Share on other sites

Well, whether or not your game runs at a certain FPS is irrelevant to the server.

 

You should do client prediction for handling npc / movement and run a ticker on the server (every 1 second, or whatever) to update positions accordingly. But that loop looks fine to me if you remove the / 60 :)

 

Only check for positions when players start initating attacks or opening an npc window, or whatever.

Link to comment
Share on other sites

It is optimal to have a loop running all NPC's then?

 

setInterval(function() {

  for (npc in npcs) {

      game.npcRangeUserAttack(npc);

  }

}, 250); //250MS

 

Every 250ms check all NPC's if see any user and attack, is optimal?

 

 

I'd personally only check once the player has clicked on the npc. Unless the monsters have some sort of extensive ai algorithm then yeah updating it serverside might be costly.  But for simple NPC interaction, there is no need

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...