Midraks Posted October 20, 2015 Share Posted October 20, 2015 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. Quote Link to comment Share on other sites More sharing options...
WombatTurkey Posted October 24, 2015 Share Posted October 24, 2015 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. Quote Link to comment Share on other sites More sharing options...
Midraks Posted October 26, 2015 Author Share Posted October 26, 2015 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? Quote Link to comment Share on other sites More sharing options...
WombatTurkey Posted October 27, 2015 Share Posted October 27, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.