Jump to content
Sign in to follow this  
Fuzzy Bandit

A 'While' loop or Triggers?

Recommended Posts

Hello!

Looking into optimizing a set amount of code, and thought it best to ask here, as I'm not totally sure.

The question, in basic terms, is:

Is it better to create a 'While' containing several if / case statements, or is it more effective to produce several triggers within the editor?

I'll give some example mockup code for what the While loop would look like (using IF statments because can't correctly remember the CASE syntax!):

_x = 1;
While {(_x == 1)} do {
sleep 1;
if (blah == 1) then {
 do this blah blah blah
};
if (blah2 == 2) then {
 do this blah2 blah2 blah2
};
if (blah3 == 3) then {
 do this blah3 blah3 blah3
};
if (blah4 == 4) then {
 do this blah4 blah4 blah4
};
if (blah5 == 5) then {
 do this blah5 blah5 blah5
};
if (blah6 == 6) then {
 do this blah6 blah6 blah6
};
};

So would it be better to create that loop, or better to create six triggers in the editor? The main reason I asked is that I did a small test with the While loop using the following code:

_x = 1;
_count = 0;
While {(_x == 1)} do {
_count = _count + 1;
hint format ["Check has run.\n%1", _count];
};

Now that beast showed me the loop running roughly 100 checks per second, and by the time I'd tested what this script was intended for, it'd already checked the condition over 17,000 times! Surely this can't be effective, and surely it would take painful amounts of precious processing power. Now, with the loop I would use, I've put in a 'sleep 1' to the beginning of the loop. Would that make it more effective? Or is it still checking 100 times a second anyway to make sure that the condition _x == 1 is still met?

Cheers!

Edited by Fuzzy Bandit

Share this post


Link to post
Share on other sites

Don't use _x, that is a reserved special counter, i.e. used in forEach statements.

As for any loops, you would use the sleep command to avoid hogging up resources.

The nice thing about the switch case syntax is that when it has a hit, no further checks is needed.

You can also do

switch (true) do {

case (a > b) : { do this };

case (c == d) : {do that};

case (name player == _namevariable) : {kick player};

default {take a nap}; //Note the missing : on the default block.

}; //End switch

Even with triggers I tend to use delays, then in the form of i.e. this && (time % 20 > 19), or if I need shorter loops, I just spawn the code so I can use sleeps.

I don't know about effectiveness of triggers, but I prefer the scripted way, as too many triggers in the mission make it look ugly.

Share this post


Link to post
Share on other sites

Aye, the reason I didn't want lots of triggers in the map was because of it looking a bit crowded. I'd prefer to do what I can with scripts, as then I can apply the same ideas to different maps / layouts.

Didn't think about not using _x. I'll change it to _n! :D

Still though, surely it wouldn't effect a seperate forEach statement, unless you were using the variable itself within the forEach statement, meaning there would be two _x's within it! :D

Share this post


Link to post
Share on other sites

A trigger check every 0.5 seconds it's condition expression.

If you avoid the keyword this in it, probably no activation will be ever checked.

Trigger expressions are not scheduled like scripts.

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
Sign in to follow this  

×