Jump to content
Sign in to follow this  
sakura_chan

sleep command vs. framerate

Recommended Posts

I am working on a velocity based script that slows an object from an unknown speed down to a specific speed. It was working fine when I used a basic "while {(_velocity select 2) <= -3}" line with a loop that removed some velocity and was timed with a "sleep 0.01" command. It was working until I tried it on my laptop which gets about 10-20 fps instead of my desktop's solid 60fps. The object still slowed down but at a much slower rate. Doesn't this mean that "sleep" is unreliable? Are there any alternatives to using it that would not be fps dependant?

One workaround I found was to use:

	while {(_velocity select 2) <= -3} do
{
	_fps = (diag_fps / 60);
	_chutevel = velocity _chute;
	_velocity = [_chutevel select 0,_chutevel select 1,(_velocity select 2) + (0.3 / _fps)];
	_chute setvelocity _velocity;
	hintsilent format ["slowing %1\n%2 FPS", _velocity select 2, round diag_fps];
	sleep 0.01;
};

So the actually amount of deceleration is based on the framerate.

Share this post


Link to post
Share on other sites

The game simulation only runs one update per frame, so the minimum time a script will sleep is one frame (ie, loops contain sleep will run maximum of once per frame). At 20 FPS, sleep 0.01 == sleep 0.05. At sixty FPS your script will run 3x more iterations per second. Modifying the velocity at a rate dependant on iteration time/FPS seems like a reasonable idea to me.

You can test scripts at artificially low FPS using the FPS "cheat".

Also, since Arma (2?) the script scheduler limits the amount of CPU time spent on scripts, which can cause problems for real time scripts at low FPS. I don't think this applies to this script on it's own, but it may do so if there are many other scripts running at the same time. I believe the onEachFrame command runs script outside of this scheduled environment.

Share this post


Link to post
Share on other sites
The game simulation only runs one update per frame, so the minimum time a script will sleep is one frame (ie, loops contain sleep will run maximum of once per frame). At 20 FPS, sleep 0.01 == sleep 0.05. At sixty FPS your script will run 3x more iterations per second. Modifying the velocity at a rate dependant on iteration time/FPS seems like a reasonable idea to me.

You can test scripts at artificially low FPS using the FPS "cheat".

Also, since Arma (2?) the script scheduler limits the amount of CPU time spent on scripts, which can cause problems for real time scripts at low FPS. I don't think this applies to this script on it's own, but it may do so if there are many other scripts running at the same time. I believe the onEachFrame command runs script outside of this scheduled environment.

Does sleep otherwise use a wall-clock time, if the minimum is dependant on the framerate?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×