admin管理员组文章数量:1278758
My setup is a multiplayer game, with sockets to async transfer data.
Now because of the nature of a game, I have a game loop which should tick every 500ms to do player updating (e.g. position, appearance,...).
var self = this;
this.gameLoop = setInterval(function()
{
for (var i = 0; i < playerSize; i++)
{
self.players.get(i).update();
};
}, 500);
I'm currently using setInterval
but when i did some benchmarks with about 200 connections, setInterval
drifted away to 1000ms instead of 500ms, which makes the whole game appear laggy. Also it's not accurate enough unfortunately with a little amount of players. (note that the update calls only take about 100ms maximum)
So to me after researching, there seems to be no other option then to spawn a process which only handles the timining mechanism? Or are there other options?
Anyone has done this before or can give some fundamental ideas/solutions on how to do this?
Full code:
Game.prototype.start = function()
{
var average = 0;
var cycles = 10;
var prev = Date.now();
this.thread = setInterval(function()
{
console.log("interval time="+(Date.now() - prev));
prev = Date.now();
var s = Date.now();
// EXECUTE UPDATES
for (var i = 0; i < playerSize; i++)
{
self.players.get(i).update();
};
average += (Date.now() - s);
cycles--;
if(cycles == 0)
{
console.log("average update time: " + (average/10) + "ms");
average = 0;
cycles = 10;
}
}, 500);
}
My setup is a multiplayer game, with sockets to async transfer data.
Now because of the nature of a game, I have a game loop which should tick every 500ms to do player updating (e.g. position, appearance,...).
var self = this;
this.gameLoop = setInterval(function()
{
for (var i = 0; i < playerSize; i++)
{
self.players.get(i).update();
};
}, 500);
I'm currently using setInterval
but when i did some benchmarks with about 200 connections, setInterval
drifted away to 1000ms instead of 500ms, which makes the whole game appear laggy. Also it's not accurate enough unfortunately with a little amount of players. (note that the update calls only take about 100ms maximum)
So to me after researching, there seems to be no other option then to spawn a process which only handles the timining mechanism? Or are there other options?
Anyone has done this before or can give some fundamental ideas/solutions on how to do this?
Full code:
Game.prototype.start = function()
{
var average = 0;
var cycles = 10;
var prev = Date.now();
this.thread = setInterval(function()
{
console.log("interval time="+(Date.now() - prev));
prev = Date.now();
var s = Date.now();
// EXECUTE UPDATES
for (var i = 0; i < playerSize; i++)
{
self.players.get(i).update();
};
average += (Date.now() - s);
cycles--;
if(cycles == 0)
{
console.log("average update time: " + (average/10) + "ms");
average = 0;
cycles = 10;
}
}, 500);
}
Share
Improve this question
edited Sep 15, 2015 at 15:27
Captain Obvious
asked Sep 15, 2015 at 13:55
Captain ObviousCaptain Obvious
7853 gold badges18 silver badges39 bronze badges
6
-
You said
update()
only takes about 100ms. How did you benchmark this? And does it mean the whole loop with 200 players only takes 100ms or one single call ofupdate
takes 100ms? – basilikum Commented Sep 15, 2015 at 14:26 - Hi, all the updates of the players takes less than 100ms, my point was just to make note that it is not more than the interval time. I measured it with a Date.now() difference – Captain Obvious Commented Sep 15, 2015 at 15:05
- Can you maybe show, in code, how you measured it? I am not sure, but considering these huge differences (from 500ms to 1000ms), I would assume, that the benchmark is maybe incorrect and that your loop actually takes more time then 500ms to execute which make the intervals bee longer and possible overlapping. – basilikum Commented Sep 15, 2015 at 15:15
- I've updated my post. – Captain Obvious Commented Sep 15, 2015 at 15:28
- Ok, looks good. Could it maybe have something to do with this: stackoverflow./questions/5927284/… – basilikum Commented Sep 15, 2015 at 15:42
2 Answers
Reset to default 3Here is an article on creating an accurate node.js game loop using a bination of setTimeout()
and setImmediate
so things are accurate but don't use up too much CPU.
The nanoTimer timing library might work for you. From the readme it sounds like it uses a similar approach. I don't know if it's been designed with games in mind but it should be worth a try!
Although I don't work with games, there is a better alternative than setInterval, as shown here. It worked well for me in a business application. I hope this helps.
本文标签: javascriptNodejs alternative to setInterval which is more precise for game loopStack Overflow
版权声明:本文标题:javascript - Node.js: alternative to setInterval which is more precise for game loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741250950a2365776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论