OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: Seven on 18 Sep 2007, 16:47:35

Title: Question about another timer script
Post by: Seven on 18 Sep 2007, 16:47:35
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: [Select]
_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";

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

Thx in advance!
Seven
Title: Re: Question about another timer script
Post by: Spooner on 18 Sep 2007, 18:02:33
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: [Select]
_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";
};

One alternative to the exitWith would be to use:
Code: [Select]
while {(_timetowait > 0) and (not healing)} do

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).
Title: Re: Question about another timer script
Post by: Mandoble on 18 Sep 2007, 18:22:08
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: [Select]
_script = []execVM"yourwaitingscript.sqf";
while {!healing && !scriptDone _script} do
{
   Sleep 2;
};
if (healing) then
{
   terminate _script;
};

or for SQS
Code: [Select]
_script = []execVM"yourwaitingscript.sqf"
#check
(waitsymbol here, sorry cannot reproduce) 2
? !healing && !scriptDone _script: goto "check"
? healing: terminate _script
Title: Re: Question about another timer script
Post by: Seven on 18 Sep 2007, 18:46:19
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:
Title: Re: Question about another timer script
Post by: Mandoble on 18 Sep 2007, 21:13:29
alt 126 is easy on my desktop and THE HELL to try in my laptop  :P