Jump to content
Sign in to follow this  
dragonsyr

addaction in while loop problem

Recommended Posts

i have this code that works for that im doing,

_vehPlayer = vehicle player;
_player = player;
_vehRole = assignedVehicleRole player select 0;
while {vehicle player isKindOf "Air";} do {if(_vehrole == "driver") then {_action = _player addAction ["Call myscript", "myAction.sqf", [], -1, false, false, ""] else {hint"no driver";sleep 2;};};
while {blah blah....};
hint"init running";sleep 5; //debug
_player removeaction _action;
execVM "Init.sqf";

but with this i get too many actions .

when i add waitUntil {!_vehplayer == "driver"; like this

while {vehicle player isKindOf "Air";} do {if(_vehrole == "driver") then {_action = _player addAction ["Call myscript", "myAction.sqf", [], -1, false, false, ""];waitUntil {!_vehplayer == "driver";};} else {hint"no driver";sleep 2;};};

i get this "Error in expression <", [], -1, false, false, ""];waitUntil {!_vehplayer == "driver";};} else {hint"n>

Error position: <!_vehplayer == "driver";};} else {hint"n>

Error !: Type Object, expected Bool"

what can i do as solution ? i need when the pilot get out of the heli/airplane , then the script continues on next line of code

Edited by dragonsyr

Share this post


Link to post
Share on other sites

Your waitUntil would be like:

waitUntil {driver vehicle player != player};

Share this post


Link to post
Share on other sites

thank you for the reply ...

no luck again. its stay in waituntil with this .if player get out of the heli the action not removed , and the script is not continue....

another thing is: can i do , waituntil the addaction is fired, ??? but this is not an option for my case because i want the addaction ONLY when the player is pilot,

Edited by dragonsyr

Share this post


Link to post
Share on other sites

Just add an action once to the player and use the actions condition to decide whether the player can see the action, rather than trying to keep adding and removing it.

player addAction ["Call myscript", "myAction.sqf", [], -1, false, false, "", "_target iskindof 'AIR' && { driver (vehicle _target) == _this} "]

So the player always has the action but he can only see it when, he is in an air vehicle AND is the pilot.

Share this post


Link to post
Share on other sites
waitUntil {driver vehicle player != player};

won't work due to bug in 'vehicle player' that bothers me to no end - a player not in a vehicle returns the player object, so:

player == player

vehicle player == player

driver vehicle player == player

etc...

you could use the code in your while{} loop for your waitUntil:

waitUntil {vehicle player isKindOf "Air"};

also, when the driver seat of the vehicle-itself is empty it returns NULL, so this would work if you remember the vehicle name once you're in it:

...
while {vehicle player isKindOf "Air} do {
...
_myVeh = vehicle player;
waitUntil {isNull (driver vehicle _myVeh)};
...

btw, there should be no semi-colon in your while{} expression!

while{ some_expression; } do {}; is incorrect - it would be while { some_expression } do {};

---------- Post added at 19:13 ---------- Previous post was at 19:12 ----------

or as Larrow says, much simpler

Share this post


Link to post
Share on other sites

@larrow, copy that but my problem is not the remove of the addaction, is how can i pause the loop when the player is pilot.

---------- Post added at 02:23 ---------- Previous post was at 02:17 ----------

you could use the code in your while{} loop for your waitUntil:

waitUntil {vehicle player isKindOf "Air"};

i get same error Error in expression <", [], -1, false, false, ""];waitUntil {!vehicle player isKindOf "Air";};} else >

Error position: <!vehicle player isKindOf "Air";};} else >

Error !: Type Object, expected Bool

Share this post


Link to post
Share on other sites

This would remove the error

waitUntil {!(vehicle player isKindOf "Air")};

Share this post


Link to post
Share on other sites
also, when the driver seat of the vehicle-itself is empty it returns NULL, so this would work if you remember the vehicle name once you're in it:

...
while {vehicle player isKindOf "Air} do {
...
_myVeh = vehicle player;
waitUntil {isNull (driver vehicle _myVeh)};
...

btw, there should be no semi-colon in your while{} expression!

thank you sir!! this works fine....

copy for the semi-colon .....

Share this post


Link to post
Share on other sites
i get same error Error in expression <", [], -1, false, false, ""];waitUntil {!vehicle player isKindOf "Air";};} else >

Error position: <!vehicle player isKindOf "Air";};} else >

Error !: Type Object, expected Bool

you've got semi-colons in your expressions still. i should have been clearer, anywhere where its an expected return of true/false (example: while{}, waitUntil{}, if(), etc...) cannot have a semi-colon

EDIT: wow im late on the replies :D

Share this post


Link to post
Share on other sites
This would remove the error

waitUntil {!(vehicle player isKindOf "Air")};

i try that also .....i get the error. edit: working at last ..... !(vehicle player isKindOf "Air")

ok you solve my problem with your next code..

thank you

---------- Post added at 02:49 ---------- Previous post was at 02:33 ----------

i get the point for semi-collon..... in my code removed allready :-D

Edited by dragonsyr

Share this post


Link to post
Share on other sites

I found several posts with this problem, but none offered a solution that wasn't specific to their problem... I kind of have a similar problem like dragonsyr had though:

I need to have an addaction inside a while-loop, in order to add an action to every object of a kind. If I place the object in the editor, I could do without it, but I'm planning on placing objects in front of my players in Zeus (which forces me to use a loop so the players can interact with the newly spawned objects).

Unfortunately there can't be a condition in which the condition for the addaction is not true, so the solution in this thread can't help me: A ReammoBox is always a ReammoBox.

	while {true} do 
	{
		{
		if(_x isKindOf "ReammoBox") then 
			{
			_x addAction [action1];   
			};
		} forEach vehicles;
	};

So... I'm kind of out of ideas on how to run a while loop only once, or if there's a better way for me to reach my goal...

Share this post


Link to post
Share on other sites

If you want any loop to run only once, you don't need a loop at all.

You can place addAction on vehicles and ,most other, objects too.

(semicode) like:

_veh = createVehicle [.................];
_actID = _veh addAction [...............],

greetings Na_Palm

Share this post


Link to post
Share on other sites
If you want any loop to run only once, you don't need a loop at all.

You can place addAction on vehicles and ,most other, objects too.

I know that, but it doesn't help in this specific case, as we're talking about objects that are created neither by script nor by trigger. We're talking Zeus here.

To specify: I'm trying to create a loop which everytime an ammobox is created adds an action to the created ammobox. The addition of the addaction to ammoboxes is supposed to be infinite, but every ammobox should have the action only once.

Edited by Pergor

Share this post


Link to post
Share on other sites

Than use an loop over allMissionObjects of the specific type:

{
   {
       ....
       sleep 0.001;
   }forEach allMissionObjects _x;
   sleep 0.001;
}forEach [your class types here];

Inside it, check if this object has an local var assigned. And if not addAction and setVariable to it.

greetings Na_Palm

PS: only run it on the server! :)

Share this post


Link to post
Share on other sites
Than use an loop over allMissionObjects of the specific type:

Inside it, check if this object has an local var assigned. And if not addAction and setVariable to it.

greetings Na_Palm

PS: only run it on the server! :)

Thanks! That sounds reasonable... could you elaborate that though? :D

Share this post


Link to post
Share on other sites

As of Zeus it would be for MP so you have to assure that the addAction would be called on all clients as addAction has local effect only!

My idea therefor would be to place it in an extra .sqf and precompile it at mission start, on every machine, with:

<scriptname> = call compileFinal preProcessFileLineNumbers "scriptfile";

In your "initServer.sqf" somewhere after all initialization parts are finished:

spawn {
 while {true} do {
   {
     {
       if (isNil {_x getVariable "BoxhasAction"}) then {
         [_x, "<scriptname>", true, true] call BIS_fnc_MP;
         _x setVariable ["BoxhasAction", true, false];
       };
       sleep 0.001;
     }forEach allMissionObjects _x;
     sleep 0.001;
   }forEach ["Box_IND_Ammo_F", "Box_IND_AmmoOrd_F"];
 };
};

your scriptfile.sqf:

if (isServer) exitWith {false};
_box = _this;

_actID = _box addAction [.........................................];

that should be all.

you have to check for typos, as i only wrote it here in.

greetings Na_Palm

Edited by Na_Palm
an typo

Share this post


Link to post
Share on other sites

I tried different versions of your code, but unfortunately to no avail:

my init.sqf:

myscript = call compileFinal preProcessFileLineNumbers "scripts\script.sqf";

my initserver.sqf:

  while {true} do {
   {
     {
       if (isNil {_x getVariable "BoxhasAction"}) then 
	{
         [_x, "myscript", true, true] call BIS_fnc_MP;
         _x setVariable ["BoxhasAction", true, false];
       };
       sleep 0.001;
     }forEach allMissionObjects _x;
     sleep 0.001;
   }forEach ["Land_HumanSkull_F", "Box_IND_Ammo_F", "Box_IND_AmmoOrd_F"]; // Land_HumanSkull_F, to add some dramatic :D
 };

(The "spawn" gave me an immediate error, so I left it out. Not sure if it's part of the problem though...)

and my scriptfile:

if (isServer) exitWith {false};
_box = _this;

_actID = _box addAction ["The bestest action ever"];

With that configuration I get the following error:

'...params call _function;

} else (

_params |#|spawn _function;

};

true

} else (

["'Func...'

Error spawn: Type Bool, expected code

File A3\functions_f_curator\Tempfunctions\fn_MPexec.sqf, line 108

I wonder if our little script is flawed or are we having a bug here (or did I miss out on an important step...)?

Share this post


Link to post
Share on other sites

try this edits then...

init.sqf:

myscript = compileFinal preProcessFileLineNumbers "scripts\script.sqf"; 

initserver.sqf:

[] spawn {
while {true} do {
	{
		{
			if (isNil {_x getVariable "BoxhasAction"}) then {
				[_x, "myscript", true, true] call BIS_fnc_MP;
				_x setVariable ["BoxhasAction", true, false];
			};
			sleep 0.001;
		}forEach allMissionObjects _x;
		sleep 0.001;
	}forEach ["Land_HumanSkull_F", "Box_IND_Ammo_F", "Box_IND_AmmoOrd_F"]; // Land_HumanSkull_F, to add some dramatic :D
}; 
};

the spawn is essential as you want to run the code permanently.

Think it should work now after crosschecking some code...

greetings Na_Palm

PS: i assume the best action evaa are infact the correct parameter for your addAction!

Share this post


Link to post
Share on other sites

PS: i assume the best action evaa are infact the correct parameter for your addAction!

Yes, it is of course. ;) Works like a charme now, thank you very much for your assistance!

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  

×