Jump to content
Archosaurus

Continuous check of if condition instead of continuous loop

Recommended Posts

Hello. Yet again, help is needed.

 

I have a script that has an "if, then" segment of code in it that I want to ONLY loop if the condition changes.

 

So, for example.

 

_cond = true;

 

if {_cond} then {

_unit doMove pos;

};

 

Then let's say I have a trigger or whatever that sets _cond to false, so

 

if !{_cond} then {

doStop _unit;

};

 

Then another trigger that changes _cond to true. So with all logic, the former's statements should be called, again, if the whole thing is looped. That's all good and the unit moves and stops accordingly.

 

Now, the problem here is that I need the script to not loop through the "doMove" or "doStop" more than once as long as the condition does not change, but I need it to constantly check if the condition changes. Make any sense?

 

The reason I need to do this is because it's obviously problematic to call some statements over and over again when they only need to be called once. With my current knowledge, I can only do a loop that loops the statements continuously, or only a single call of the statements and then no more. 

 

What I want is continuous checking, then looping the statements again, but then no more looping until the condition changes.

 

EDIT: Perhaps waitUntil or exitWith can solve this? Although I failed to find a way to make the script "triggerable" again.

Share this post


Link to post
Share on other sites

You can add another variable to the condition. Then once it's triggered, set the variable to false so it won't trigger again.

 

Example:

if (_cond and _var) then {
  _unit doMove pos;
  _var = false;
};

And in another trigger that wants it to move again, just simply change _var back to true.

Share this post


Link to post
Share on other sites

I would do something like this I think. Its a little vague as doMove only has to be given once, unless you have an algorithm that needs to give it as things move etc. Anyway, this is just a while loop, which you would probably use [] spawn with, that waits until a boolean variable is either true or false, and when it is one or the other, a second while loop traps so that you can repeat some process or you could exit with. 

 

There are so many ways to do it really.. in this case I assume you want the parent loop to go on forever. Sometimes nested if statements are just going to be the fastest even if they are more work to create.

boolX = true;
someCriteria = 123;
while {true} do {
  sleep 1;
  // while the condition is true (enemy is alive, task is not complete, etc)
  while {boolX} do {
    sleep 1;
    // every second, see if unit needs to be told to move?
    if (someCriteria) then {_unit doMove;};
  };
  // condition is not true, so do some code if desired
  while {!boolX} do {
    sleep 1;
    if (someOtherCriteria) then {doStop _unit;};
  };
};
 

Share this post


Link to post
Share on other sites

Depending on what the condition is, you might find and Event Handler which could help you with the "when the condition changed" problem.

 

Otherwise, you only have the options to either go with a loop which constantly checks if the condition has changed or you create a function which you call whenever you change the condition and which executes your above code.

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

×