Jump to content
Ilias38rus

Question. ( for from do )

Recommended Posts

Wanted to make a little test, but the testing code hanging game, interesting why

tsts = true;
tst = false;
_ln = 1;
for "_ln" from 1 to 2 do {
	_lst = false;
	if (_ln == 1) then {
		while {tsts} do {
			_lst = tst;
			if (_lst) then {hint "t1";} else {hint "t";};
			sleep 1;
		};
	} else {
		sleep 0.2;
		while {tsts} do {
			if (_lst) then {systemchat "t1";} else {systemchat "t";};
			sleep 1;
		};
	};
};

Share this post


Link to post
Share on other sites

You have no exit conditions on your while loops, therefore it won't continue after the first iteration of the for loop.

Share this post


Link to post
Share on other sites

Try this:

tsts = true;
tst = false;
for "_i" from 1 to 2 do {
	private ["_lst"];
        scopeName "loop";
	_lst = false;
	if (_i == 1) then {
		while {tsts} do {
			_lst = tst;
			if (_lst) then {hint "t1"} else {hint "t"; breakOut "loop"};
			sleep 1;
		};
	} else {
		sleep 0.2;
		while {tsts} do {
			if (_lst) then {systemchat "t1"} else {systemchat "t"; breakOut "loop"};
			sleep 1;
		};
	};
};

Share this post


Link to post
Share on other sites

 

Try this:

tsts = true;
tst = false;
for "_i" from 1 to 2 do {
	private ["_lst"];
        scopeName "loop";
	_lst = false;
	if (_i == 1) then {
		while {tsts} do {
			_lst = tst;
			if (_lst) then {hint "t1"} else {hint "t"; breakOut "loop"};
			sleep 1;
		};
	} else {
		sleep 0.2;
		while {tsts} do {
			if (_lst) then {systemchat "t1"} else {systemchat "t"; breakOut "loop"};
			sleep 1;
		};
	};
};

Am i understanding right what here will be: for 1 -> break out -> for 2 -> break out -> over |for| (i guies no because no 2-nd iteration)

And that will stay looped? If yes then where?:          |while                            |while,

While {} {
     while {          //without breakout the while will not be created again until it's over?
     };
};

Share this post


Link to post
Share on other sites

It's fairly simple

while {condition} do {
  //do stuff here
};

As long as the condition returns true, the while loop will continue to run. So if you simply put a variable which has the value true inside the condition and you don't change that variable somewhere, the while loop will infinitely run.

Share this post


Link to post
Share on other sites

It's fairly simple

while {condition} do {
  //do stuff here
};

As long as the condition returns true, the while loop will continue to run. So if you simply put a variable which has the value true inside the condition and you don't change that variable somewhere, the while loop will infinitely run.

 

Actually in a non-scheduled environment the while loop exits after 10000 iterations.

  • Like 2

Share this post


Link to post
Share on other sites

I would also like to add that using While too often can clog up script performance, and has been proven Here.

 

However, there are 2 ways that I know of (probably more) that you can use to loop, without having the dreaded "Script Lag".

 

 

 

Function Loop:

TAG_MyFunction = {
	//Code
	[] spawn TAG_MyFunction; //For functions that have script suspension.
	//Or
	[] call TAG_MyFunction; //For functions that don't have script suspension.
};
[] spawn TAG_MyFunction;

onEachFrame: (Only one onEachFrame can be used for each mission)

onEachFrame {
	//Code. (There should be no script suspension, whatsoever!!!)
};

In case if anyone asks, but "Script Suspension" is when you have script commands like sleep, waitUntil, etc. If you have script suspension in your function, you must use spawn. Else otherwise, use call. (I believe you also use call only to return a result, but this theory may be incorrect)

Share this post


Link to post
Share on other sites

onEachFrame: (Only one onEachFrame can be used for each mission)

onEachFrame {
	//Code. (There should be no script suspension, whatsoever!!!)
};

 

Unless I'm mistaken by what you mean or by what this post says, I can do as many onEachFrame stackedEHs as I please, while getting the same result.

Share this post


Link to post
Share on other sites

 (I believe you also use call only to return a result, but this theory may be incorrect)

It's not a question of belief, it's a cornerstone of scripting: https://community.bistudio.com/wiki/call& https://community.bistudio.com/wiki/Function#Return_Values

 

While loop doesn't clog up the performance if you use a reasonable sleep in it. And onEachFrame isn't really an alternative for it, they just serve a different purpose.

 

And what's that function thing? Isn't that exactly the same as a while loop, or possibly even worse because it spawns a new thread after each run? I've never seen anything like that. :huh: 

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

×