Jump to content
gokitty1199

Inconsistent loop timer

Recommended Posts

I have noticed (for me anyways) that creating a while loop on the server and increment a variable(time) does not seem to be accurate when sleeping for very short durations such as UiSleep 0.008; I have a series of 5-7 timers around the map that a client can activate at his choosing. The time is a simple while loop that uses UiSleep to increment a variable that I have to keep track of how long the timer has been running for. While testing with just myself I noticed that having a while loop with a UiSleep of 0.008 and increment a variable by 0.008 is slower by different amounts each time. If i start a timer on my phone at the same time I start the timer in the game, when my phone reaches 60 seconds the timer can be either at 45 seconds or 58 seconds or anywhere in between. its just very inconsistent. In the COF challenges, how did BIS do their timers? Is there a way I can view the missions scripts to see how they did theirs? I just need my timers to be accurate. Here how my timer is running.

while {!_StageFinished && !_StageDone} do
{
	_TargetsDone = _Timer GetVariable "TargetsDone";
	_TargetCount = _Timer GetVariable "TargetCount";
	_StageDone = _Timer GetVariable "StageDone";
	_ShooterName = str (_Timer GetVariable "CurrentShooter");
	
	if (_TargetsDone < _TargetCount) then
	{
		_Time = _Time + 0.008;
		_Timer SetVariable ["TimerTime", _Time];
	} else
	{
		//misc stuff that doesnt run until timer is finished
	};
	UiSleep 0.008;
};

 

Share this post


Link to post
Share on other sites

There is no guarantee that sleep/uisleep will suspend the script for the exact duration, the only guarantee is that it will sleep for no less than given duration

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

A scheduled script has an execution time limit of 3 ms before being suspended to the benefit of another script until its turn comes back. here 

maybe to avoid suspension use a non-schedule? just an idea.

_Timer SetVariable ["TimerTime", (time + 5)];

[...] call {
_timer = _this select 0;
while {true} do {
	if (time > (_timer getVariable ["TimerTime", -1])) exitwith {//misc stuff that doesnt run until timer is finished
    };
};
};

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
3 hours ago, sabot10.5mm said:

A scheduled script has an execution time limit of 3 ms before being suspended to the benefit of another script until its turn comes back. here 

maybe to avoid suspension use a non-schedule? just an idea.


_Timer SetVariable ["TimerTime", (time + 5)];

[...] call {
_timer = _this select 0;
_true = true;
while {_true} do {
	if (time > (_timer getVariable ["TimerTime", -1])) then {//misc stuff that doesnt run until timer is finished
    _true = false;
    };
};
};

 

you just made me realize how big of an idiot i am. i completely forgot about Time. anyways fixed it, simply created a variable to get the starting time

_StartingTime = Time;

_TimerTime = 0;

then whenever i wanted to get the current time of the timer and assign it to _TimerTime just simply

_TImerTime = (Time - _StartingTime);

and you end up with the correct time. now i can remove the loop entirely and have a function get called when needed to handle everything. thanks again!

Share this post


Link to post
Share on other sites

 

5 hours ago, sabot10.5mm said:

maybe to avoid suspension use a non-schedule?

Call does not mean your code will run unscheduled. All it does is carry on the calling scopes state.

Code called from unscheduled remains unscheduled.

Code called from scheduled remains scheduled.

Scheduler

 

Also the while would bail after 10000 iterations if it was unscheduled.

  • Like 1

Share this post


Link to post
Share on other sites

as I said it was just an idea. I just tested it and it doesnt work as intended, 10000 iteration complete very fast

  • Confused 1

Share this post


Link to post
Share on other sites
12 minutes ago, sabot10.5mm said:

i realize this. i assumed he only wanted to sleep for a few seconds. 

wont be able to sleep in an unscheduled environment. you can use RemoteExecCall to run a function in an unscheduled environment though

Share this post


Link to post
Share on other sites
On 8/31/2019 at 1:00 AM, gokitty1199 said:

While testing with just myself I noticed that having a while loop with a UiSleep of 0.008 and increment a variable by 0.008 is slower by different amounts each time.

That's the scheduler for you.

 

On 8/31/2019 at 11:06 PM, sabot10.5mm said:

10000 iteration complete very fast

Well in unscheduled, the game freezes till it's done. So by definition a while loop with 10k iterations would finish in a single frame.

 

On 8/31/2019 at 11:19 PM, gokitty1199 said:

you can use RemoteExecCall to run a function in an unscheduled environment though

isNil has way less overhead.

  • Thanks 1

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

×