Jump to content
Dreadleif

Make a script LOOP a limited number of times

Recommended Posts

Hey, thanks for wanting to help :). So, I need some things to happen a certain number of times, once the conditions are true. But I'm struggling to design a script that works like that. After a lot of searching and trying I came up with this, but it doesn't work:

_incomeCount = -1;
_limit = 4;

while {(_incomeCount) < (_limit)} do {

    if (triggeractivated four_bluforcaptured) then {

    cash = cash + 2000;

    _incomeCount = _incomeCount + 1;
    sleep 60;
    
} };

The script is run at mission start from the init.sqf. What it tries to achieve is that once the trigger "four_bluforcaptured" is true, a timer will start and "cash = cash + 2000" will be run every 60 seconds, for 4 loops, and then come to a complete stop with a new message.

Share this post


Link to post
Share on other sites

_incomecount is out of scope. It does not exist outside the if condition.
Place private "_incomecount"; at the beginning of your script.

Share this post


Link to post
Share on other sites

You should not check the condition from init.sqf, as the loop starts at the beginning of the mission. Since the if statement is not true at this time, the while loop will just keep running up to the 10,000 loop limit (without any sleep pauses)

 

You are better off using the four_bluforcaptured trigger, or you could use a waitUntil loop with a sleep command in it:

 

waitUntil {sleep 60; (triggeractivated four_bluforcaptured)};

 

To execute a number of loops:

for "_i" from 1 to _limit do { CODE GOES HERE } 

http://community.bistudio.com/wiki/for_var

  • Like 1

Share this post


Link to post
Share on other sites

_incomecount is out of scope. It does not exist outside the if condition.

Place private "_incomecount"; at the beginning of your script.

This statement is false. Also, that's not what the private command does.

Share this post


Link to post
Share on other sites

Although the statement that _incomeCount is out of scope is false. The suggested use of private is in fact correct and although the variable would be nil would infact make the variable in scope.

private [ "_inScope" ];

if ( true ) then {
    _inScope = true;
};

hint str _inScope;

Share this post


Link to post
Share on other sites

 

Although the statement that _incomeCount is out of scope is false. The suggested use of private is in fact correct and although the variable would be nil would infact make the variable in scope.

Although the private command is a good recommendation (and practice), I've never had issues with not using it outside of recursive functions. I was simply saying that his reason for suggesting the use of the private command (implying that using it would make the variable "in scope") was false. The variable would always be in scope, as you mentioned

Share this post


Link to post
Share on other sites

Hey, thanks for wanting to help :). So, I need some things to happen a certain number of times, once the conditions are true. But I'm struggling to design a script that works like that. After a lot of searching and trying I came up with this, but it doesn't work:

_incomeCount = -1;
_limit = 4;

while {(_incomeCount) < (_limit)} do {

    if (triggeractivated four_bluforcaptured) then {

    cash = cash + 2000;

    _incomeCount = _incomeCount + 1;
    sleep 60;
    
} };

The script is run at mission start from the init.sqf. What it tries to achieve is that once the trigger "four_bluforcaptured" is true, a timer will start and "cash = cash + 2000" will be run every 60 seconds, for 4 loops, and then come to a complete stop with a new message.

 

Put this in your init.sqf:

[] execVM "MoneyBoost.sqf"; 
 

 

Then create an .sqf called "MoneyBoost.sqf". In the .sqf file, place this code in:

waitUntil {triggerActivated four_bluforcaptured}; //Waits until trigger activated
for "_i" from 1 To 4 do {
	cash = cash + 2000;
	sleep 60;
};
titleText ["Your Money boost has expired!", "PLAIN DOWN"]; //This line runs after the limited loop has completed

Ensure your trigger can be activated, otherwise the script will still wait for the activation.

 

 

 

 

Best Regards,

 

 

 

Rawner135

Share this post


Link to post
Share on other sites
Guest

Basicly if you know how many loops you need use for.

If the number of loops is not determined (condition) use while.

:)

Share this post


Link to post
Share on other sites

Basicly if you know how many loops you need use for.

If the number of loops is not determined (condition) use while.

:)

 

However, I wouldn't advise using while all the time, as it is resource heavy. More info Here.

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

×