Jump to content
Sign in to follow this  
inofix

Continue in a loop/ or goto in sqf

Recommended Posts

Hello,

i'm currently struggling with the following:

I have a while loop running and i need to continue/start from beginning at some point.

Sadly i can't find any "continue" command in sqf, neither does Goto work and scopeName + breakOut/To.

Is there any way i can continue my loop (like in C/C++) or start the loop again from the beginning without reloading the script?

Share this post


Link to post
Share on other sites

Could you provide a bit more on what exactly your trying to accomplish, and maybe provide the code you have currently.

Share this post


Link to post
Share on other sites

Example:

while{true} do {

_result = [] call fnc_IsAtBase;
if(!_result) exitWith { };	

if(_damage > 0) then {
	vehicle player setDamage (_damage - 0.1);
	_damage = damage vehicle player;
	Continue; // Continue loop from beginning again
};

if(_fuel < 1) then {
	vehicle player setFuel (_fuel + 0.1);
	__fuel = fuel vehicle player;
	Continue; // Continue loop from beginning again
};

// And more threads for ammo, color, and custom scripts
};

Share this post


Link to post
Share on other sites

As soon as an if statement's code scope is completed it continues, you don't have to do anything. But you will need to put some sort of suspension to slow the loop down or it will freeze the game (has the possibility at least).

Share this post


Link to post
Share on other sites

Ok, get you now, just do this for each of your conditions:

if([] call fnc_IsAtBase && {_damage > 0}) then {...};

EDIT: Or,

while {true} do
{
if ([] call fnc_IsAtBase) then
{
	_fixStuff = [_unit] spawn
	{
		_unit = (_this select 0);

		if(_damage > 0) then 
		{
			vehicle player setDamage (_damage - 0.1);
			_damage = damage vehicle player;
		};

		if(_fuel < 1) then
		{
			vehicle player setFuel (_fuel + 0.1);
			_fuel = fuel vehicle player;
		};
	};
	waitUntil {scriptDone _fixStuff};
};
sleep 10;
};

Edited by JShock

Share this post


Link to post
Share on other sites

I may be confused about what you want... but this may be it.

[color="#FF8040"][color="#191970"][b]while[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#000000"]true[/color][color="#8B3E2F"][b]}[/b][/color] [color="#191970"][b]do[/b][/color] [color="#8B3E2F"][b]{[/b][/color]

[color="#1874CD"]_result[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#8B3E2F"][b]][/b][/color] [color="#191970"][b]call[/b][/color] fnc_IsAtBase[color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color][color="#8B3E2F"][b]([/b][/color][color="#8B3E2F"][b]![/b][/color][color="#1874CD"]_result[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color] [color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]	
[color="#191970"][b]if[/b][/color] [color="#000000"]true[/color] [color="#191970"][b]then[/b][/color] [color="#8B3E2F"][b]{[/b][/color] [color="#006400"][i]// for the scope[/i][/color]

	[color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]damage[/b][/color] [color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#191970"][b]if[/b][/color][color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]>[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color] [color="#006400"][i]// exit scope with action[/i][/color]
		[color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]setDamage[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]-[/b][/color] [color="#FF0000"]0.1[/color][color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]

	[color="#1874CD"]_fuel[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]fuel[/b][/color] [color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#191970"][b]if[/b][/color][color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_fuel[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color] [color="#006400"][i]// exit scope with action[/i][/color]
		[color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]setFuel[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_fuel[/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]0.1[/color][color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]

	[color="#006400"][i]// And more threads for ammo, color, and custom scripts[/i][/color]

[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]sleep[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color] [color="#006400"][i]// slow down loop[/i][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

EDIT: _damage & _fuel need to be defined before their if statements.

Edited by Fight9

Share this post


Link to post
Share on other sites
EDIT: _damage & _fuel need to be defined before their if statements.

Yea I think he gave us semi-pseudo code, just to get a concept of what needs to happen with it.

Share this post


Link to post
Share on other sites

To use goto you need to write your script in .sqs and execute it with "exec" command. There are plenty of alternatives of course:

[color="#FF8040"][color="#191970"][b]while[/b][/color][color="#8B3E2F"][b]{[/b][/color][color="#8B3E2F"][b][[/b][/color][color="#8B3E2F"][b]][/b][/color] [color="#191970"][b]call[/b][/color] fnc_IsAtBase[color="#8B3E2F"][b]}[/b][/color] [color="#191970"][b]do[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]switch[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#000000"]true[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]do[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
	[color="#191970"][b]case[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]>[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]:[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
		[color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]setDamage[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]-[/b][/color] [color="#FF0000"]0.1[/color][color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color]
		[color="#1874CD"]_damage[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]damage[/b][/color] [color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#191970"][b]case[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_fuel[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]:[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
		[color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]setFuel[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_fuel[/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]0.1[/color][color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color]
		[color="#1874CD"]__fuel[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]fuel[/b][/color] [color="#191970"][b]vehicle[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#191970"][b]default[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#006400"][i]// And more threads for ammo, color, and custom scripts[/i][/color]
[color="#191970"][b]sleep[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color] [color="#006400"][i]//<-- you dont really want immediate repeat[/i][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Share this post


Link to post
Share on other sites

num = 0;
scopeName "main";
while {true} do
{
scopeName "outerLoop";

while {true} do
{
	num = num + 1;
	hintSilent str num;

	if (num == 2) then
	{
		systemChat "WTF";
		breakTo "outerLoop";
	};
	if (num == 4) then
	{
		breakTo "main";
	};
	sleep 1;
};
sleep 1;
};
hint "Done";

scopeName, breakTo

But this is a terrible idea. There's a reason structured programming is specifically called "goto-less programming"

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

what if you avoid a loop and do something like this.

[u]functionName[/u] = {
_result = [] call fnc_IsAtBase;
if(!_result) exitWith { };	

if(_damage > 0) exitWith{
	vehicle player setDamage (_damage - 0.1);
	_damage = damage vehicle player;
	[] spawn [u]functionName[/u]; //or call if you like
};

if(_fuel < 1) exitWith{
	vehicle player setFuel (_fuel + 0.1);
	__fuel = fuel vehicle player;
	[] spawn [u]functionName[/u]; //or call if you like
};

// And more threads for ammo, color, and custom scripts

};
[] spawn [u]functionName[/u];

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  

×