Home   Help Search Login Register  

Author Topic: Question about another timer script  (Read 1643 times)

0 Members and 1 Guest are viewing this topic.

Offline Seven

  • Members
  • *
  • Am I a llama?
Question about another timer script
« 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

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Question about another timer script
« Reply #1 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).
« Last Edit: 18 Sep 2007, 18:12:22 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Question about another timer script
« Reply #2 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

Offline Seven

  • Members
  • *
  • Am I a llama?
Re: Question about another timer script
« Reply #3 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:

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Question about another timer script
« Reply #4 on: 18 Sep 2007, 21:13:29 »
alt 126 is easy on my desktop and THE HELL to try in my laptop  :P