Jump to content
JohnHSmith

breaking a loop from outside

Recommended Posts

Hi,

I want to make a scheduler, which is in a file called "fn_roundTimer.sqf".

This timer should be able to be started and stopped from outer.

I thought something like this:

["start"] spawn fnc_roundTimer;
["end"] spawn fnc_roundTimer;

So I tried this:

_type = param [0,"none"];
_roundTime = 60;

if(_type == "start") then {
	while(_roundTime >= 0) do {

		if(_roundTime == 0) exitWith {
			diag_log "round finished";
		};
		
		sleep 1;
		_roundTime = _roundTime-1;
	};
};
if(_type == "end") exitWith { 
	diag_log "round has been stopped";
};

But this won't work. What am I doing wrong?

Share this post


Link to post
Share on other sites

Seems, the code, you spawn in the second line doesn't affect in any way the code, you spawn in the first line. Both are running the same function independently, in parallel (that's how spawning/execVM-ing works). Instead perhaps try global variable way:

 

 

StopNow = false;

[] spawn fnc_roundTimer;

//(...)

StopNow = true;

 

_roundTime = 60;

	while(_roundTime >= 0) do {

		if(_roundTime == 0) exitWith {
			diag_log "round finished";
		};
		
		sleep 1;

		if(StopNow) exitWith { 
			diag_log "round has been stopped";
		};		
		
		_roundTime = _roundTime-1;
	};

Or terminate way:

 

_handle = [] spawn fnc_roundTimer;

//(...)

terminate _handle;

diag_log "round has been stopped";

 

_roundTime = 60;

	while(_roundTime >= 0) do {

		if(_roundTime == 0) exitWith {
			diag_log "round finished";
		};
		
		sleep 1;		
		_roundTime = _roundTime-1;
	};

BTW in this case, the function in this exact form does nearly same thing as:

 

sleep 60;

diag_log "round finished";

but I assume, there will be something more inside. 

Share this post


Link to post
Share on other sites
4 minutes ago, Rydygier said:

Seems, the code, you spawn in the second line doesn't affect in any way the code, you spawn in the first line. Both are running the same function independently, in parallel (that's how spawning/execVM-ing works). Instead perhaps try global variable way:

 

 

StopNow = false;

[] spawn fnc_roundTimer;

//(...)

StopNow = true;

 


_roundTime = 60;

	while(_roundTime >= 0) do {

		if(_roundTime == 0) exitWith {
			diag_log "round finished";
		};
		
		sleep 1;

		if(StopNow) exitWith { 
			diag_log "round has been stopped";
		};		
		
		_roundTime = _roundTime-1;
	};

Or terminate way:

 

_handle = [] spawn fnc_roundTimer;

//(...)

terminate _handle;

diag_log "round has been stopped";

 


_roundTime = 60;

	while(_roundTime >= 0) do {

		if(_roundTime == 0) exitWith {
			diag_log "round finished";
		};
		
		sleep 1;		
		_roundTime = _roundTime-1;
	};

BTW in this case, the function in this exact form does nearly same thing as:

 


sleep 60;

diag_log "round finished";

but I assume, there will be something more inside. 

Okay, thanks for your ideas. But I want to start it from file1, and stop it from file2. How to do this? Your code only works at the same file I think.

Share this post


Link to post
Share on other sites

The variable stopNow is global and accessible from all scripts on the client

Share this post


Link to post
Share on other sites

The solution above works but if you really absolutely need it, you can do this with your original code:

 

_type = param [0,"none"];
_roundTime = 60;

if ((isNil {roundInProgres}) or (_type == "end")) then {
	roundInProgres = false;
}

if ((_type == "start") and (!roundInProgres)) then {
	roundInProgres = true;
	while (_roundTime >= 0) do {

		if ((_roundTime == 0) or (!roundInProgres)) exitWith {
			diag_log "round finished";
			roundInProgres = false;
		};
		
		sleep 1;
		_roundTime = _roundTime-1;
	};
};

Untested, just an idea.

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

×