Jump to content
egbert

Random pick in an array and _forEachIndex

Recommended Posts

Hi there !

 

I'm not very good at scripting and I try to make this happen :

I have a unit created in a group spawning at some position :

_vip = createGroup [EAST, true];
_jose = _vip createUnit ["O_G_Soldier_F", [25304.1,21805.5,0.1], [],0,"CAN_COLLIDE"];

I would like to move this _jose in a position randomly picked in an array of 8 positions, so I wrote :

_pos_jose = [[24871.3,23103.8,1.3],[24869.2,23099.7,1.4],[24852.4,23106.2,0.3],[24843.9,23082.7,0.1],[24837.9,23206.3,0.4],[24868.6,23219.4,3.1],[24655.2,23186.9,0.2],[24876.7,23097.9,0.1]];
_random_pos_jose = _pos_jose select (floor (random (count _pos_jose)));
_jose setPosAtl _random_pos_jose;

So far so good, but now I would like to add some directions to _jose (each direction match the index of the above array so first dir in the array is the good one for the first position in the array above) :

_dir_jose = [138,63,70,117,44,128,172,205];

And furthermore, I would like to add a bodyguard to this _jose, but in a different group. This bodyguard (named _gdc1) is also already spawned I just would like to move him near _jose. I guess the logic is quite the same than for directions.

The trick is, as the position of _jose is randomly picked, I don't know how to get the right index picked in the first array and select it for the other arrays.

I tried this, but without success, the position of _gdc1 is always the same (the first one in the _pos_gdc1 array, so I guess index 0) :

private ["_pos_jose","_random_pos_jose"];
_pos_jose = [[24871.3,23103.8,1.3],[24869.2,23099.7,1.4],[24852.4,23106.2,0.3],[24843.9,23082.7,0.1],[24837.9,23206.3,0.4],/*masure_foret*/[24868.6,23219.4,3.1],[24655.2,23186.9,0.2],[24876.7,23097.9,0.1]];
_dir_jose = [138,63,70,117,44,128,172,205];

_pos_gdc1 = [[24874.2,23106,1.3],[24866.6,23095.8,1.5],[24852,23111.7,0.1],[24846.8,23079.4,0.1],[24847.7,23202.8,0.1],[24872.1,23214.8,3.1],[24665.9,23173.6,0.2],[24884.3,23099.6,0.1]];
_dir_gdc1 = [321,298,10,145,271,137,76,102];

_random_pos_jose = _pos_jose select (floor (random (count _pos_jose)));
{
_jose setPosAtl _random_pos_jose;
_jose setDir (_dir_jose select _forEachIndex);
_gdc1 setPosATL (_pos_gdc1 select _forEachIndex);
_gdc1 setDir (_dir_gdc1 select _forEachIndex);
} forEach _pos_jose;

I can't figure where I'm wrong, since as I said first, I'm not good at scripting SQF (or every other language to tell the truth).

If anybody does have a clue, it will be highly appreciated !

Thanks in advance !

Share this post


Link to post
Share on other sites
private ["_pos_jose","_random_pos_jose"];
_pos_jose = [[24871.3,23103.8,1.3],[24869.2,23099.7,1.4],[24852.4,23106.2,0.3],[24843.9,23082.7,0.1],[24837.9,23206.3,0.4],/*masure_foret*/[24868.6,23219.4,3.1],[24655.2,23186.9,0.2],[24876.7,23097.9,0.1]];
_dir_jose = [138,63,70,117,44,128,172,205];

_pos_gdc1 = [[24874.2,23106,1.3],[24866.6,23095.8,1.5],[24852,23111.7,0.1],[24846.8,23079.4,0.1],[24847.7,23202.8,0.1],[24872.1,23214.8,3.1],[24665.9,23173.6,0.2],[24884.3,23099.6,0.1]];
_dir_gdc1 = [321,298,10,145,271,137,76,102];

_random_pos_jose = selectRandom _pos_jose;
_index = _pos_jose find _random_pos_jose;
_jose setPosAtl _random_pos_jose;
_jose setDir (_dir_jose select _index);
_gdc1 setPosATL (_pos_gdc1 select _index);
_gdc1 setDir (_dir_gdc1 select _index);

selectRandom and find are your friends. The forEach command goes through ALL array elements, therefore only the last array element will be the final position and direction.

  • Like 1

Share this post


Link to post
Share on other sites

Aaand it works like a charm ! I guess I was overcomplexing the thing !

Thanks a lot 7erra ! You rock !

Share this post


Link to post
Share on other sites
private _idx = floor random count _pos_jose;
private _pos = _pos_jose select _idx;
private _dir = _dir_jose select _idx;

Because searching array in array of arrays operation is too slow

  • Like 2

Share this post


Link to post
Share on other sites

Hi Serena, thanks for your reply !

So, do I need to write these 3 "private" at the beginning of my code ? Instead of this :

private ["_pos_jose","_random_pos_jose"];

 

Share this post


Link to post
Share on other sites

Any syntax is allowed. You can use syntax that suits you best. Keyword effects and usage cases described here.

 

Also good approach is to minimize allocation of variables where possible:

private _idx = floor random count _pos_jose;
_jose setPosAtl (_pos_jose select _idx);
_jose setDir (_dir_jose select _idx);

 

Share this post


Link to post
Share on other sites

Thanks a lot Serena, and sorry for my late answer, I wasn't around till today !

I'll will give it a try and let you know !

Thanks!

 

Edit : It works perfectly, thank you for your help Serena and 7erra !

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

×