Jump to content
Sign in to follow this  
MuffEater

nesting if statement?

Recommended Posts

hi guys,

so there is my problem (i'm a beginner to scripting):

I want to simulate a random bombing over a city, and I ended up with this code:

_v1 = position target_bomb; // game logic in the editor
_radius = 450; 
_type = random 4; 
_mun = "";

if(_type < 1) then {_type = 1} else {if(_type < 2) then {_type = 2} else {if(_type < 3) then {_type = 3} else {_v4 = 4}}}};

switch (_type) do {
case 1: {_mun = "Bo_FAB_250"};
case 2: {_mun = "Bo_GBU12_LGB"};
case 3: {_mun = "Sh_120_HE"};
case 4: {_mun = "G_40mm_Smoke"};
};


_coord = getPos target_bomb;
_tx = _coord select 0;
_ty = _coord select 1;

while {alive player} do {

_x = random _radius;
_y = random _radius;
_z = random 1;
_z2 = random 1;

if(_z < 0.5) then {_x = -_x};
if(_z2 < 0.5) then {_y = -_y};

_mine = _mun createVehicle [_tx+_x,_ty+_y,-0.1];

_v3 = random 15; 	
Sleep _v3;
};

if (true) exitWith {};

the problem is that no bomb never get spawned! /*===> me = sad:'( */

any ideas??

Share this post


Link to post
Share on other sites

One of the most complicated randomization methods i've ever saw. try this:

_radius = 450;

_bombs = ["Bo_FAB_250", "Bo_GBU12_LGB", "Sh_120_HE", "G_40mm_Smoke"];
_mun = _bombs select (floor(random(count _bombs)));

_coord = getPos target_bomb;
_tx = _coord select 0;
_ty = _coord select 1;

while {alive player} do {

_x = random _radius;
_y = random _radius;
_z = random 1;
_z2 = random 1;

if(_z < 0.5) then {_x = -_x};
if(_z2 < 0.5) then {_y = -_y};

_mine = _mun createVehicle [_tx+_x,_ty+_y,-0.1];

_v3 = random 15; 	
Sleep _v3;
};

if (true) exitWith {};

Edited by [FRL]Myke

Share this post


Link to post
Share on other sites

IT WORKS!!!!!!!!!!!!!

Thank you very much!

(p.s.: was it really so complicated =O ?)

Edited by MuffEater

Share this post


Link to post
Share on other sites

What is your impression? Your way had 8 lines of code, mine has 2 lines. ;)

BTW, this way is easy to expand/reduce...just add/remove munition types to/from the _bombs array, the rest is self-adapting.

Share this post


Link to post
Share on other sites

Also if you want to later bomb again with something different you can remove the selected bomb from the array and then pick a new one:

....
// bombing complete, choose new type of bomb that hadn't been picked before, just make sure you don't do this more times than the number of bombs you use.
_bombs = _bombs - _mun
_mun = _bombs select (floor(random(count _bombs)));
// bomb again!
...

Of course this type of thing is not that great here but it can be great in other situations where you want to pick random unique elements such as building positions.

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  

×