Jump to content
Sign in to follow this  
Antorugby

While (true) script not working

Recommended Posts

Hello guys,

on my mission I got some game logic with a large spawn radius, they are called "flare1" "flare2" etc, I want to flares to be fire randomly and I wrote this:

while {true} do {
flare21 = "F_40mm_White" createvehicle (position flare2);
flare21 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
while {true} do {
flare31 = "F_40mm_White" createvehicle (position flare3);
flare31 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
...

And so on for the other game logic, but it's not working, if I use just one "while {true}" in the mission preview using the console, it works, but if I call the script in the init doesn't work.

It's 2 days that I'm working on something and I'm tired so probably I'm doing in the wrong way something easy, can you help me out?

Share this post


Link to post
Share on other sites

Huh. Is it popping the flare in the right spot (ie, in the sky)? If so, very interesting. I'dda thought you would need to create it at a set altitude.

Anyway, it is important to note that a while loop doesn't run parallel with the script. This means that if you have 1 while loop for {true}, as you do, and have anything after it, be it another while loop or any other code, that code will not be executed unless true becomes not true. And if that happens, well we're screwed.

If you're looking to pop multiple at a time or even a bit spread out, as you seem to want, do it in the same loop. With what you gave above, you will get 1 flare every 60+random 120 seconds, and nothing else.

Share this post


Link to post
Share on other sites

What Grimes said. Think of it this way, your code won't run another instruction until the first while statement is finished. I think using spawn may be a work around for this.

Share this post


Link to post
Share on other sites
;2650618']Huh. Is it popping the flare in the right spot (ie' date=' in the sky)? If so, very interesting. I'dda thought you would need to create it at a set altitude.

Anyway, it is important to note that a while loop doesn't run parallel with the script. This means that if you have 1 while loop for {true}, as you do, and have anything after it, be it another while loop or any other code, that code will not be executed unless true becomes not true. And if that happens, well we're screwed.

If you're looking to pop multiple at a time or even a bit spread out, as you seem to want, do it in the same loop. With what you gave above, you will get 1 flare every 60+random 120 seconds, and nothing else.[/quote']

The game logic is 230meters up in the air, and yes they spawn fine in the air.

But if I do it in the same while loop how can I spawn multiple flares in multiple location?

Anyway, if i set a variable like this for every loop:

flareloop1 = true;

and I do "while {flareloop1) { etc"

should it work?

Share this post


Link to post
Share on other sites

You could just run the script twice with the single while and pass the position to the script

null=[flare2] execvm "scriptname.sqf";null=[flare3] execvm "scriptname.sqf";

_pos =_this select 0; 

while {true} do {
flare21 = "F_40mm_White" createvehicle (position _pos);
flare21 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};

or run them in parallel by spawning the code.

[] spawn {
while {true} do {
flare21 = "F_40mm_White" createvehicle (position flare2);
flare21 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
};

[] spawn {
while {true} do {
flare31 = "F_40mm_White" createvehicle (position flare3);
flare31 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
};

Share this post


Link to post
Share on other sites
You could just run the script twice with the single while and pass the position to the script

null=[flare2] execvm "scriptname.sqf";null=[flare3] execvm "scriptname.sqf";

_pos =_this select 0; 

while {true} do {
flare21 = "F_40mm_White" createvehicle (position _pos);
flare21 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};

or run them in parallel by spawning the code.

[] spawn {
while {true} do {
flare21 = "F_40mm_White" createvehicle (position flare2);
flare21 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
};

[] spawn {
while {true} do {
flare31 = "F_40mm_White" createvehicle (position flare3);
flare31 setVelocity [0,0,-0.15]; 
sleep (60 + (random 120));
};
};

The first one is perfect, thank you!

But this doesn't spawn randomly in the radious of the game logic, I think because once the game logic it's spawned in one position it doesn't change, any way to make it spawn randomly in a specific area?

I tried with this:

_pos = _this select 0; 

while {true} do {
_SpawnRadius = 300 + (random 30); 
_Dir = (random 359);
_Spawnflare = [( _pos select 0)-(_SpawnRadius)*sin(_Dir),( _pos select 1)-(_SpawnRadius)*cos(_Dir),( _pos select 2)];

_flare21 = "F_40mm_White" createvehicle (position _Spawnflare);
_flare21 setVelocity [0,0,-0.15]; 
sleep (10 + (random 10));
};  

But it seems to break the loop.

Edited by Antorugby

Share this post


Link to post
Share on other sites

Your on the right track but if you using a game logic/object you can use the more efficient createvehicle array.

It's quicker and has the ability to randomize the position

just replace the old createvehicle with this (200 being the random radius)

_flare21 = createVehicle ["F_40mm_White",position _pos,[],200, "FLY"] ;

now to why your modified code didn't work.

You slightly mixed up the positions. To get the x,y,z you need to use getpos first and then modify the positions.

Also when you use those modified positions you don't need getpos as it's now all ready in the correct format.

_pos =  getpos _this select 0; // added getpos to get [x,y,z]

while {true} do {
_SpawnRadius = 300 + (random 30); 
_Dir = (random 359);
_Spawnflare = [( _pos select 0)-(_SpawnRadius)*sin(_Dir),( _pos select 1)-(_SpawnRadius)*cos(_Dir),( _pos select 2)];

_flare21 = "F_40mm_White" createvehicle (_Spawnflare);// removed position as it's not needed
_flare21 setVelocity [0,0,-0.15]; 
sleep (10 + (random 10));
};

Share this post


Link to post
Share on other sites
Your on the right track but if you using a game logic/object you can use the more efficient createvehicle array.

It's quicker and has the ability to randomize the position

just replace the old createvehicle with this (200 being the random radius)

_flare21 = createVehicle ["F_40mm_White",position _pos,[],200, "FLY"] ;

now to why your modified code didn't work.

You slightly mixed up the positions. To get the x,y,z you need to use getpos first and then modify the positions.

Also when you use those modified positions you don't need getpos as it's now all ready in the correct format.

_pos =  getpos _this select 0; // added getpos to get [x,y,z]

while {true} do {
_SpawnRadius = 300 + (random 30); 
_Dir = (random 359);
_Spawnflare = [( _pos select 0)-(_SpawnRadius)*sin(_Dir),( _pos select 1)-(_SpawnRadius)*cos(_Dir),( _pos select 2)];

_flare21 = "F_40mm_White" createvehicle (_Spawnflare);// removed position as it's not needed
_flare21 setVelocity [0,0,-0.15]; 
sleep (10 + (random 10));
};

Thanks a lot F2k, it's perfect!

---------- Post added at 16:49 ---------- Previous post was at 16:14 ----------

Hey F2k sorry if I bother you again, I have a problem with another script.

It's a script that spawn randomly the units, with the possibility to spawn people on the same position, the script is working fine in single player, and it's working fine when I host a room, but when it's hosted on a dedicated server it's not working, and everyone spawn in where they were placed in the editor, this is the script:


Private ["_unit","_target"];
_unit = _this select 0; //player unit
call compile preprocessfile "SHK_pos\shk_pos_init.sqf";
_pos = ["mrkGreen",0,["mrkRed","mrkYellow"]] call SHK_pos;
_unit setposATL _pos;
sleep 1;
if (random 1 < 0.30) then {

_target = (playableunits + switchableunits) call bis_fnc_selectrandom;
_unit setposATL (_target modeltoworld [0,-3,0]); //spawn behind the unit - avoid friendly fire incidents.
};

Any idea why it's not working?

Share this post


Link to post
Share on other sites

Sorry can't help with multiplayer scripting I've no experience with it.

I'd imagine it has somethingg to do with isserver isdedicated commands.

Share this post


Link to post
Share on other sites
Sorry can't help with multiplayer scripting I've no experience with it.

I'd imagine it has somethingg to do with isserver isdedicated commands.

Thanks anyway! ^^

Share this post


Link to post
Share on other sites

This version of your code should work in multiplayer

Private ["_unit","_target"];
_unit = _this select 0; //player unit
call compile preprocessfile "SHK_pos\shk_pos_init.sqf";

if (random 1 < 0.30) then {
   _target = (playableUnits) call BIS_fnc_selectRandom;
   _unit setPosATL (_target modelToWorld [0,-3,0]); //spawn behind the unit - avoid friendly fire incidents.
} else {
   _pos = ["mrkGreen",0,["mrkRed","mrkYellow"]] call SHK_pos;   
   _unit setPosATL _pos;
};

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  

×