Editors Depot - Mission Editing and Scripting > ArmA - Editing/Scripting General

Question about another timer script

(1/1)

Seven:
Howdy,

I found this script & works nice, only I want to add a check to it so it would quit when "the commander" is being healed.
When he is, I use healing = true in the mission trigger.
Since I don't get too much of *.sqf yet, I'll just go and ask I figure :)

I call the script from init.sqf by [30] execVM "timer.sqf"


--- Code: ---_timetowait = _this select 0;
while {_timetowait > 0} do
{
   if (_timetowait != 1) then
   {
      cutText[format["%1 minutes left", _timetowait], "PLAIN DOWN"];
   }
   else
   {
      cutText["One minute left", "PLAIN DOWN"];
   };
   Sleep(60);
   _timetowait = _timetowait - 1;

}
cutText["Our commander is dead... We didn't make it in time.", "PLAIN"];
timesover = true;
PublicVariable "timesover";

--- End code ---

So in brief, can somebody tell me where to put if (healing == true): exitWith {};  :scratch:

Thx in advance!
Seven

Spooner:
Well, you can't just place a exitWith in the loop to exit the script, since it would just break out of the loop, actually causing the "commander is dead" message and setting timesOver. Thus use exitWith to break out of the loop (like "break" statement in most languages), then ensure that the commander dying effects only occur if healing hasn't already happened.

A small point, but "if (x) then ..." is the same as "if (x == true) then ..." if x is a boolean (true/false). "if (not x) then ..." is the same as "if (x == false) then ...". Generally, when using if with booleans, one would use the former, simpler versions.


--- Code: ---_timetowait = _this select 0;
while {_timetowait > 0} do
{
   // Break out of the while loop *before* printing any messages (which is after the sleep, when looping), otherwise you could get messages AFTER the commander is healed.
   if (healing) exitWith { };

   if (_timetowait != 1) then
   {
      cutText[format["%1 minutes left", _timetowait], "PLAIN DOWN"];
   }
   else
   {
      cutText["One minute left", "PLAIN DOWN"];
   };

   Sleep(60);

   _timetowait = _timetowait - 1;
};

// Tell the world that the commander has "died" only if healing hasn't reached him.
if (not healing) then
{
    cutText["Our commander is dead... We didn't make it in time.", "PLAIN"];
    timesover = true;
    PublicVariable "timesover";
};

--- End code ---

One alternative to the exitWith would be to use:

--- Code: ---while {(_timetowait > 0) and (not healing)} do

--- End code ---

This would have the same effect; it is just personal choice (well, OK, certain programming dogmas would favour one technique over the other, but you needn't be too worried about that ;P).

Mandoble:
Just to add a bit more colour to this thread  ;)
This way you dont need to use any exitWith. In this case, the script will end as soon as you set the healing var to true, so you will not get the message up to 60 seconds later due to the original Sleep 60 within the script.


--- Code: ---_script = []execVM"yourwaitingscript.sqf";
while {!healing && !scriptDone _script} do
{
   Sleep 2;
};
if (healing) then
{
   terminate _script;
};

--- End code ---

or for SQS

--- Code: ---_script = []execVM"yourwaitingscript.sqf"
#check
(waitsymbol here, sorry cannot reproduce) 2
? !healing && !scriptDone _script: goto "check"
? healing: terminate _script

--- End code ---

Seven:
Thank you both for the reply;
I already tried first solution which works perfectly!

I'll try yours too Mandoble; I had no clue you could terminate a script from within another  :good:

offtopic: try "alt+126" Mandoble  :scratch:

Mandoble:
alt 126 is easy on my desktop and THE HELL to try in my laptop  :P

Navigation

[0] Message Index

Go to full version