Jump to content
Sign in to follow this  
igneous01

create case statements based on number of elements in array.

Recommended Posts

So i want to make a simple function that will basically setpos some units around the markers specified to make it more or less like a random search for them.

I also want to be able to incorporate as many markers as i want in my function, and then just randomly generate a number from the counted array, and use a case statement to setpos the unit to the marker selected from the array.

heres what i have so far:

if (isnil "DZ_fnc_RandomSpawn") then {
   DZ_fnc_RandomSpawn = {
       private ["_markers", "_unit", "_xrand"];
       _markers = _this select 0;
       _unit = _this select 1;

       _xrand = round(random (count _markers));

       switch {_xrand} do {
           case 

but as you can see, i dont know how to get the case statements based on number of elements in the array. if i make 3 case statements that only assumes 3 markers. but what if i wanted to have 5 or more? then the case statements wouldnt work anymore because they only checked up to 3 markers.

so how can you make a switch case using the number of elements in an array?

Share this post


Link to post
Share on other sites

I would change the _xrand line to this:

_xrand = _markers select floor(random(count _markers));

That will pick the marker by name, and floor will set it to the 0-based array index properly. At that point you have the marker name and don't need a switch statement at all.

Share this post


Link to post
Share on other sites

ahh thanks alot i was pondering on how to make this work for a sometime now thanks.

this is what i came up with on my own, it works sometimes, but i think its because the random value 0 is never considered in it

// random spawn function
if (isnil "DZ_fnc_RandomSpawn") then {
   DZ_fnc_RandomSpawn = {
       private ["_markers", "_unit", "_xrand", "_markerarraycount"];
       _markers = _this select 0;
       _unit = _this select 1;
       _markerarraycount = count _markers;
       player sidechat str _markerarraycount;
       _xrand = round(random _markerarraycount);
       player sidechat str _xrand;
       for "_x" from 0 to _markerarraycount do {
           if (_xrand == _x) then {
               _unit setpos getmarkerpos (_markers select _x);
           };
       };
   };
};

---------- Post added at 04:07 ---------- Previous post was at 04:00 ----------

now just one more question i want to ask, whats the command to setpos a unit relative to another unit? so the original spacing of artillery pieces for ex will setpos and retain their spacing?

---------- Post added at 04:45 ---------- Previous post was at 04:07 ----------

nevermind, problem solved, realized shk_moveobjects can do this, managed to get it to work.

Share this post


Link to post
Share on other sites

I strongly recommend using floor instead of round. Let's say your marker array has 4 marker names. If you use round, it will return either 0,1,2,3 or 4. That is 5 possible integers, but you only have 4 items in the array. Floor will return 0,1,2, or 3. Since arrays are 0-indexed, that works perfectly.

Share this post


Link to post
Share on other sites

yes i realized that now, no wonder it wasnt working for me with my original code.

thanks for that tip, ill be sure to remember using floor next time i deal with arrays like this.

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  

×