Jump to content
Sign in to follow this  
TurokGMT

while loop doesn't appear to be being executed - little help please?

Recommended Posts

Hey guys, I've been pulling my hair out trying to get this script to work.

Mission tested both through editor and local dedicated server (ie running a dedi server, but connecting to it from the same machine, different instance of arma 2)

Problem, as you can see, I've riddled this script with debug sidechats, trying to find out the issue.

ALL of the sidechats before the while loop are producing results, but NONE of the sidechats INSIDE the while loop are firing - can anyone see something wrong with my loop setup?

// script to allow pilot of MV22 to deploy firebase when landed with engine off
// script is fired by a GETIN event handler for the pilot only
// passed parameters of vehicle and driver
// requires a marker named firebase to be placed in editor

_debug = true;

if (_debug) then
{
player sidechat "DeployFOB.sqf has started successfully";
};

_MV22 = _this select 0;
_pilot = _this select 1;

if (_debug) then 
{
_string = format ["Vehicle passed : %1 \n Pilot passed : %2 \n value of firebase : %3",_mv22,_pilot,firebase];
player sidechat _string;
player sidechat "beginning main loop";
_string = format ["mv22 alive chack : %1",(alive _MV22)];
player sidechat _string;
};



while {(alive _MV22)} do
{	
if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) then
{
	exitWith {player sidechat "something fucked up"};
};

if (_debug) then
{
	player sidechat "valid call of deployFOB script";
};


if (speed _MV22 == 0 && !(isEngineOn _MV22) && (firebase == false)) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
};

if (speed _MV22 == 0 && (firebase == true) && (_MV22 distance (getmarkerpos "firebase") < 20) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
};

if (speed _MV22 != 0 || isEngineOn _MV22) then 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "you're moving! removing actions";
	};
};

if (_pilot != driver _MV22) exitWith 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "driver got out or got shot - removing actions";
	};	
};
sleep 1;
};


if (true) exitWith {}; 

Share this post


Link to post
Share on other sites

You have missed a ")"

Yours:

	if (speed _MV22 == 0 && (firebase == true) && (_MV22 distance (getmarkerpos "firebase") < 20) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
};

Correct: ( Marked Red )

	if (speed _MV22 == 0 && (firebase == true) && (_MV22 distance (getmarkerpos "firebase") < 20) [b][color="Red"])[/color][/b] then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
};

Share this post


Link to post
Share on other sites

nice find, thank you.

Script still not functioning as intended though - still showing same problem =[

I've now inserted a debug sidechat as the first line in the while loop and set the while condition to true for debug purposes - nothing!

Edited by TurokGMT

Share this post


Link to post
Share on other sites

eventhandler being called from init line of the vehicle:


this addEventHandler ["GETIN",{if ((_this select 1) == "driver") then {[_this select 0, _this select 2] execVM "FOB\deployFOB.sqf"}}];

event handler is firing successfully - I am seeing deplyFOB.sqf producing sidechat when I enter vehicle as driver.

The global variable firebase is defined as false in init.sqf and made public with publicvariable.

As a side note, I'm also running this respawn script from init.sqf also:


// script to respawn MV22 mobile FOB deployer
// adds the event handler back as well as respawning
// a complete rip-off of norrins MHQ revive script =]

if (!isServer) exitWith {};

_MV22 			= _this select 0;
_respawn_delay		= _this select 1; 
_MV22_dir			= getDir _MV22;	
_MV22_pos			= getPos _MV22;
_type_MV22 			= typeOf _MV22;

waitUntil {!alive _MV22}; 

_wait = time + _respawn_delay;

waitUntil {time > _wait};
_MV22_new = _type_MV22 createVehicle _MV22_pos;
_MV22_new setDir _MV22_dir;
_MV22_new setPos _MV22_pos;
_init = "this addEventHandler	['GETIN',{if ((_this select 1) == 'driver') then {nul = [_this select 0, _this select 2] execVM 'deployFOB.sqf'}}]";

_MV22 setVehicleInit _init; 
processInitCommands;
[_MV22_new, 600] execVM "respawnmv22.sqf";

if (true) exitWith {};

Share this post


Link to post
Share on other sites

Another:

Yours:

	if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) then
{
	exitWith {player sidechat "something fucked up"};
};

Correct:

	if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) exitWith
{
	player sidechat "something fucked up";
};

B.t.w.: What is firebase?

Is it a variable with a True/False value?

If yes the you can not use

(firebase == false)

You have to check it with (firebase) for True and !(firebase) if False.

Edit:

Okay this should work:

// script to allow pilot of MV22 to deploy firebase when landed with engine off
// script is fired by a GETIN event handler for the pilot only
// passed parameters of vehicle and driver
// requires a marker named firebase to be placed in editor
private ["_deploy","_packup"];

_debug = true;

if (_debug) then
{
player sidechat "DeployFOB.sqf has started successfully";
};

_MV22 = _this select 0;
_pilot = _this select 1;

if (_debug) then 
{
_string = format ["Vehicle passed : %1 \n Pilot passed : %2 \n value of firebase : %3",_mv22,_pilot,firebase];
player sidechat _string;
player sidechat "beginning main loop";
_string = format ["mv22 alive chack : %1",(alive _MV22)];
player sidechat _string;
};



while {(alive _MV22)} do
{	
if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) exitWith
{
	player sidechat "something fucked up";
};

if (_debug) then
{
	player sidechat "valid call of deployFOB script";
};


if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase)) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
};

if (speed _MV22 == 0 && (firebase) && (_MV22 distance (getmarkerpos "firebase") < 20) ) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
};

if (speed _MV22 != 0 || isEngineOn _MV22) then 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "you're moving! removing actions";
	};
};

if (_pilot != driver _MV22) exitWith 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "driver got out or got shot - removing actions";
	};	
};
sleep 1;
};


if (true) exitWith {};

I have added the "private" scope at the beginning of the file this is used to define variables which was created inside of a loop for example.

If you don't use the "private" scope then the variables created inside of the loop ( while ) will be reported as nil.

So make sure you always use this.

A few more informations: Wiki: Private

Edited by SNKMAN

Share this post


Link to post
Share on other sites

while loop is now working, thanks for the fix!

Only problem now is the actions are being repeatedly added!!

I've edited the for loops to include a check variable:

// script to allow pilot of MV22 to deploy firebase when landed with engine off
// script is fired by a GETIN event handler for the pilot only
// passed parameters of vehicle and driver
// requires a marker named firebase to be placed in editor
private ["_deploy","_packup"];

_debug = true;

if (_debug) then
{
player sidechat "DeployFOB.sqf has started successfully";
};

_MV22 = _this select 0;
_pilot = _this select 1;
_a = 0;

if (_debug) then 
{
_string = format ["Vehicle passed : %1 \n Pilot passed : %2 \n value of firebase : %3",_mv22,_pilot,firebase];
player sidechat _string;
player sidechat "beginning main loop";
_string = format ["mv22 alive chack : %1",(alive _MV22)];
player sidechat _string;
};



while {(alive _MV22)} do
{	
if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) exitWith
{
	player sidechat "something fucked up";
};

if (_debug) then
{
	player sidechat "valid call of deployFOB script";
};


if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase) && (_a != 1) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
	_a = 1;
};

if (speed _MV22 == 0 && (firebase) && (_MV22 distance (getmarkerpos "firebase") < 20) && (_a != 2) ) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
	_a = 2;
};

if (speed _MV22 != 0 || isEngineOn _MV22) then 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "you're moving! removing actions";
	};
};

if (_pilot != driver _MV22) exitWith 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "driver got out or got shot - removing actions";
	};	
};
sleep 1;
};


if (true) exitWith {};

AND NOW IT'S BROKEN AGAIN!!! AAAARGGGGHHHH!!!!

firebase is a boolean variable (true/false)

why can't it be compared to either value?

what's wrong with (firebase == true) to return a boolean result?

Edited by TurokGMT

Share this post


Link to post
Share on other sites

To check for a boolean you use:

if(firebase) then {do stuff};

and to check if it is false:

if !(firebase) then {do stuff};

Hope that helps

Share this post


Link to post
Share on other sites
firebase is a boolean variable (true/false)

why can't it be compared to either value?

what's wrong with (firebase == true) to return a boolean result?

Well that's simply the way it is. :)

The syntax is fix and can not be changed like you would like to see it.

Sometimes things look ( sound ) logical to you but not to the syntax.

Well let's see:

_a = 0;

Okay the variable was set to 0 that's ok.

Now you check:

	if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase) && (_a != 1) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
	_a = 1;
};

_a != 1 and you set _a to 1 so this part will not be executed becouse the variable _a is 1.

Now have a look at the next part:

	if (speed _MV22 == 0 && (firebase) && (_MV22 distance (getmarkerpos "firebase") < 20) && (_a != 2) ) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
	_a = 2;
};

_a != 2 and you set _a to 2 so this part will not be executed becouse the variable _a is 2.

But now look back:

	if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase) && (_a != 1) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
	_a = 1;
};

You check _a != 1 but _a was changed to 2 so _a is not 1 and this part will be executed again. :)

Better use:

// script to allow pilot of MV22 to deploy firebase when landed with engine off
// script is fired by a GETIN event handler for the pilot only
// passed parameters of vehicle and driver
// requires a marker named firebase to be placed in editor
private ["_deploy","_packup"];

_debug = true;

if (_debug) then
{
player sidechat "DeployFOB.sqf has started successfully";
};

_MV22 = _this select 0;
_pilot = _this select 1;
_a = [True, True];

if (_debug) then 
{
_string = format ["Vehicle passed : %1 \n Pilot passed : %2 \n value of firebase : %3",_mv22,_pilot,firebase];
player sidechat _string;
player sidechat "beginning main loop";
_string = format ["mv22 alive chack : %1",(alive _MV22)];
player sidechat _string;
};



while {(alive _MV22)} do
{	
if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) exitWith
{
	player sidechat "something fucked up";
};

if (_debug) then
{
	player sidechat "valid call of deployFOB script";
};


if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase) && (_a select 0) ) then 
{
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
	_a set [0, False];
};

if (speed _MV22 == 0 && (firebase) && (_MV22 distance (getmarkerpos "firebase") < 20) && (_a select 1) ) then 
{
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
	_a set [1, False];
};

if (speed _MV22 != 0 || isEngineOn _MV22) then 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "you're moving! removing actions";
	};
};

if (_pilot != driver _MV22) exitWith 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	if (_debug) then
	{
		player sidechat "driver got out or got shot - removing actions";
	};	
};
sleep 1;
};


if (true) exitWith {};

Share this post


Link to post
Share on other sites

Ok, looks the script is finally behaving as I want it to.

Had to add a few more removeaction lines to ensure all situations were covered eg starting engines then getting out etc.

Final version looks like this:

// script to allow pilot of MV22 to deploy firebase when landed with engine off
// script is fired by a GETIN event handler for the pilot only
// passed parameters of vehicle and driver
// requires a marker named firebase to be placed in editor
private ["_deploy","_packup","_a"];

_debug = true;
_a = 0;

if (_debug) then
{
player sidechat "DeployFOB.sqf has started successfully";
};

_MV22 = _this select 0;
_pilot = _this select 1;

if (_debug) then 
{
_string = format ["Vehicle passed : %1 \n Pilot passed : %2 \n value of firebase : %3",_mv22,_pilot,firebase];
player sidechat _string;
player sidechat "beginning main loop";
_string = format ["mv22 alive chack : %1",(alive _MV22)];
player sidechat _string;
};



while {(alive _MV22)} do
{
if (!(local _MV22) || _pilot != (driver _MV22) || !(isplayer (driver _MV22))) exitWith
{
	player sidechat "something fucked up";
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	_a = 0;

};

if (speed _MV22 == 0 && !(isEngineOn _MV22) && !(firebase) && (_a != 1) ) then 
{
	_MV22 removeaction _packup;
	_deploy = _MV22 addAction ["Deploy Firebase", "FOB\firebase.sqf"];
	if (_debug) then
	{
		player sidechat "Adding deploy firebase action";
	};
	_a = 1;
};

if ( (speed _MV22 == 0) && (firebase) && (_MV22 distance (getmarkerpos "firebase") < 20) && (_a != 2) ) then 
{
	_MV22 removeaction _deploy;
	_packup = _MV22 addAction ["Pack up Firebase", "FOB\packupfirebase.sqf"];
	if (_debug) then
	{
		player sidechat "firebase deployed, adding packup option";
	};
	_a = 2;
};

if ( (speed _MV22 != 0 || isEngineOn _MV22) && (_a != 0) ) then 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	_a = 0;
	if (_debug) then
	{
		player sidechat "you're moving! removing actions";
	};
};

if (_pilot != driver _MV22) exitWith 
{
	_MV22 removeAction _deploy;
	_MV22 removeAction _packup;
	_a = 0;
	if (_debug) then
	{
		player sidechat "driver got out or got shot - removing actions";
	};	
};
sleep 1;
};


if (true) exitWith {};

Many thanks for all your excellent debugging skills and advice on syntax.

PS - The job of this (and related scripts) is to create a MP compatible, deployable base via osprey delivery, the idea being that there can only ever be one on the map at a time.

In order to get it working, I've had to wrestle around compatibility with norrin's excellent revive scripts and the lkscript pack (which is regretably designed for SP only and makes MASSIVE use of action menu commands added to "player" - locality an issue!)

Mission nearly ready, just need to create respawning insurgents that make use of the UPSMONS script (and remember that they have to patrol when they respawn!)

Oh the joys of scripting =]

Share this post


Link to post
Share on other sites

Im having some trouble with upsmon ...I got the script to work some how even when it says script\upsmon.sqf not found....it still runs it but heres the funny part now it seams if invisible to the enemy ai thats got me confused so how do I change it so they see me to along with my squad?thanks

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  

×