Jump to content
Sign in to follow this  
zuff

addAction and removeAction in WHILE loop

Recommended Posts

I've set up a MHQ with some addActions on it that I want to remove if the vehicle is moving or destroyed.

The problem is I'm not quite sure how to setup my WHILE loop:

This is in the mhq's init:

this = execVM "mhq_action.sqf";

mhq_action.sqf

_mhqAdd1 = 0;
_mhqAdd2 = 0;
_mhqAdd3 = 0;
while {true} do {
if (alive mhq && ((speed mhq)) == 0) then {
	_mhqAdd1 = mhq addAction ["<t color='#74E868'>Drop Ammo Box</t>", "scripts\mhq\mhq_drop_ammo.sqf", "", 0, false]; 
	_mhqAdd2 = mhq addAction ["<t color='#74E868'>Packup Box</t>","scripts\mhq\mhq_delete_box.sqf", "", 0, false];
	_mhqAdd3 = mhq addAction ["<t color='#74E868'>Teleport to Base</t>","scripts\mhq\mhq_tobase.sqf", "", 0, false];
} else {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd2;
	mhq removeAction _mhqAdd3;
};	
};

I'm sure you see the problem here, the script keeps adding the addActions over and over, duplicating them.

So I thought well I'll have the script remove them then add them:

mhq_action.sqf

_mhqAdd1 = 0;
_mhqAdd2 = 0;
_mhqAdd3 = 0;

while {true} do {
if (alive mhq && ((speed mhq)) == 0) then {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd2;
	mhq removeAction _mhqAdd3;
	_mhqAdd1 = mhq addAction ["<t color='#74E868'>Drop Ammo Box</t>", "scripts\mhq\mhq_drop_ammo.sqf", "", 0, false]; 
	_mhqAdd2 = mhq addAction ["<t color='#74E868'>Packup Box</t>","scripts\mhq\mhq_delete_box.sqf", "", 0, false];
	_mhqAdd3 = mhq addAction ["<t color='#74E868'>Teleport to Base</t>","scripts\mhq\mhq_tobase.sqf", "", 0, false];
} else {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd2;
	mhq removeAction _mhqAdd3;
};
       sleep 3;
};

Which technically works, but the addactions themselves flicker when the script loops. If I set "sleep 15" the flicker is not as noticeable (until 15 seconds, of course) but then if the vehicle is moving or destroyed, the actions are still available for 15 or less seconds before removed.

So then I tried a waitUntil condition instead of sleep:

mhq_action.sqf

_mhqAdd1 = 0;
_mhqAdd2 = 0;
_mhqAdd3 = 0;

while {true} do {
if (alive mhq && ((speed mhq)) == 0) then {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd2;
	mhq removeAction _mhqAdd3;
	_mhqAdd1 = mhq addAction ["<t color='#74E868'>Drop Ammo Box</t>", "scripts\mhq\mhq_drop_ammo.sqf", "", 0, false]; 
	_mhqAdd2 = mhq addAction ["<t color='#74E868'>Packup Box</t>","scripts\mhq\mhq_delete_box.sqf", "", 0, false];
	_mhqAdd3 = mhq addAction ["<t color='#74E868'>Teleport to Base</t>","scripts\mhq\mhq_tobase.sqf", "", 0, false];
} else {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd2;
	mhq removeAction _mhqAdd3;
};
waitUntil {!alive mhq || ((speed mhq)) != 0};
};

But this breaks after the vehicle has stopped moving at least once, and the actions never are added again.

I'm racking my brain here, I hope someone can help!

EDIT: Seemed to get it to work like this:

_mhqAdd1 = 0;
_mhqAdd2 = mhq addAction ["<t color='#74E868'>Packup Box</t>","scripts\mhq\mhq_delete_box.sqf", "", 0, false];;
_mhqAdd3 = 0;

while {true} do {
if (alive mhq && ((speed mhq)) == 0) then {
	_mhqAdd1 = mhq addAction ["<t color='#74E868'>Drop Ammo Box</t>", "scripts\mhq\mhq_drop_ammo.sqf", "", 0, false]; 
	_mhqAdd3 = mhq addAction ["<t color='#74E868'>Teleport to Base</t>","scripts\mhq\mhq_tobase.sqf", "", 0, false];
	waitUntil {!alive mhq || ((speed mhq)) != 0};
} else {
	mhq removeAction _mhqAdd1;
	mhq removeAction _mhqAdd3;
	waitUntil {alive mhq && ((speed mhq)) == 0};
};

};

The waitUntil in each of the if statements tells the script to hold up until one of the conditions change.

I also moved the Packup box script to always be available so you can pick up the box no matter if you're moving or not.

Edited by zuff

Share this post


Link to post
Share on other sites

while {true} do {

if (alive mhq && ((speed mhq)) == 0) then {

_mhqAdd1 = mhq addAction ["<t color='#74E868'>Drop Ammo Box</t>", "scripts\mhq\mhq_drop_ammo.sqf", "", 0, false];

_mhqAdd3 = mhq addAction ["<t color='#74E868'>Teleport to Base</t>","scripts\mhq\mhq_tobase.sqf", "", 0, false];

waitUntil {!alive mhq || ((speed mhq)) != 0};

} else {

mhq removeAction _mhqAdd1;

mhq removeAction _mhqAdd3;

waitUntil {alive mhq && ((speed mhq)) == 0};

};

};

i use this above method i must say - however for anyone reading this - the addaction function itself has various arguments/conditions within it that MAY be suitable to house (speed mhq == 0). worth considering.

Share this post


Link to post
Share on other sites

any chance of seeing the script inside the 3 sqf files it's calling? This looks exactly like what I'm wanting to add to my MHQ(though barriers instead of ammo box)! Will continue the search through the forums for more info.

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  

×