Jump to content
logan83

Operating with Arrays

Recommended Posts

Hi Guys! I'm stuck on one of those things that look easy, but then they are not. Im going to explain my self... I Have two arrays

_ArrayA = [1,2,3];
_ArrayB = [2,1,0];

If I try to add them together, I get:

_ArrayC = _ArrayA + _ArrayB;
hint str _arrayc;

_ArrayC returned  [1,2,3,2,1,0], oks my question Now... How can i add each value to get [3,3,3] not [1,2,3,2,1,0]. Im sorry but my english not good enought. :(

Share this post


Link to post
Share on other sites
5 minutes ago, logan83 said:

Hi Guys! I'm stuck on one of those things that look easy, but then they are not. Im going to explain my self... I Have two arrays


_ArrayA = [1,2,3];
_ArrayB = [2,1,0];

If I try to add them together, I get:


_ArrayC = _ArrayA + _ArrayB;
hint str _arrayc;

_ArrayC returned  [1,2,3,2,1,0], oks my question Now... How can i add each value to get [3,3,3] not [1,2,3,2,1,0]. Im sorry but my english not good enought. :(

 

a loop

_ArrayC = [];
for "_i" from 0 to ((count _ArrayA)-1) do {

	_value = (_arrayA select _i) + (_ArrayB select _i);

	_arrayC pushback _value;
};

hint _arrayC;

 

 

  • Like 1

Share this post


Link to post
Share on other sites

if the arrays are 3D vectors as in ur example then you should do it with vectorAdd:

_arr1 = [1,2,3]; 
_arr2 = [3,2,1]; 
_arr3 = _arr1 vectorAdd _arr2;

hint format["%1", _arr3]; //throws [4,4,4]

if your arrays are not such vectors then I woul do a loop as well but i would choose forEach

_arr1 = [1,2,3,4];  
_arr2 = [3,2,1,0]; 
_arr3 = []; 
 
{
 _value = _x + (_arr2 select _forEachIndex);
 _arr3 pushBack _value;
} forEach _arr1; 

hint format["%1", _arr3]; //throws [4,4,4,4]

EDIT:

here is another solution which is not creating a third array. it just modifies _arr2. this should be faster but the contents of _arr2 are changed after that:

_arr1 = [1,2,3,4];  
_arr2 = [3,2,1,0]; 
 
{
 _value = _x + (_arr2 select _forEachIndex);
 _arr2 set [_forEachIndex, _value];
} forEach _arr1; 

hint format["%1", _arr2]; //throws [4,4,4,4]

 

Edited by sarogahtyp
Inserted another method
  • Like 2

Share this post


Link to post
Share on other sites
44 minutes ago, sarogahtyp said:

if the arrays are 3D vectors as in ur example then you should do it with vectorAdd:


_arr1 = [1,2,3]; 
_arr2 = [3,2,1]; 
_arr3 = _arr1 vectorAdd _arr2;

hint format["%1", _arr3]; //throws [4,4,4]

if your arrays are not such vectors then I woul do a loop as well but i would choose forEach


_arr1 = [1,2,3,4];  
_arr2 = [3,2,1,0]; 
_arr3 = []; 
 
{
 _value = _x + (_arr2 select _forEachIndex);
 _arr3 pushBack _value;
} forEach _arr1; 

hint format["%1", _arr3]; //throws [4,4,4,4]

 

 

Yeah for each is better than a for loop, reason i did it for loop is that's how i did it in java and c++

Share this post


Link to post
Share on other sites

Great! Thanks to you both guys!! You'res my heroes! 

 

Share this post


Link to post
Share on other sites

uhm, another question, how can i make this cleaner?

_unitpos = this select 0;
_DropPos = [];
if (_UnitPos == 1) then {_DropPos pushBack .8; _DropPos pushBack 3.18; _DropPos pushBack 1.16594};

Cause if i use:

if (_UnitPos == 1) then {_HaloPos pushBack [.8,3.18,1.16594]};

_HaloPos returns [[0.8,3.18,1.16594]] instead of [0.8,3.18,1.16594]...

 

Sorry about the stupid questions but im newbie :(

Share this post


Link to post
Share on other sites

I dont get the point.

What should that script do?
If you only want to get those values in an array then just use:

_HaloPos = [.8,3.18,1.16594];


 

Share this post


Link to post
Share on other sites
7 minutes ago, logan83 said:

uhm, another question, how can i make this cleaner?


_unitpos = this select 0;
_DropPos = [];
if (_UnitPos == 1) then {_DropPos pushBack .8; _DropPos pushBack 3.18; _DropPos pushBack 1.16594};

Cause if i use:


if (_UnitPos == 1) then {_HaloPos pushBack [.8,3.18,1.16594]};

_HaloPos returns [[0.8,3.18,1.16594]] instead of [0.8,3.18,1.16594]...

 

Sorry about the stupid questions but im newbie :(

 

As Saro said, you can just put them in in the array straight

_unitpos = this select 0;
_DropPos = [];
if (_UnitPos == 1) then {_DropPos = [0.8,3.18,1.16594]};

 

Share this post


Link to post
Share on other sites

ok i trying to set objets positions relative to a another Object position, i cant use attachto or something, and the base object has a random start. Sorry about my english i cant explain myself better :( 

Share this post


Link to post
Share on other sites
_offset = [0, 0, 10];
_obj1 setPos (_obj2 modelToWorld _offset);

_ obj1 is the object which should be attached to _obj2

_offset is the offset which should be used

  • Like 1

Share this post


Link to post
Share on other sites
20 minutes ago, sarogahtyp said:

_offset = [0, 0, 10];
_obj1 setPos (_obj2 modelToWorld _offset);

_ obj1 is the object which should be attached to _obj2

_offset is the offset which should be used

I feel very stupid right now... better take a break, my head its burning xDD thanks a lot sarogahtyp.

Share this post


Link to post
Share on other sites
2 minutes ago, logan83 said:

I feel very stupid right now... better take a break, my head its burning xDD thanks a lot sarogahtyp.

bro i feel like that every time he corrects me, but don't worry bro :) it just means we are learning

  • Like 2

Share this post


Link to post
Share on other sites

its just about reading the wiki and I m far far away from understanding all the stuff in armas scripting.

The important thing is to read the examples and the comments in the wiki. And also try, try, try...

  • Like 2

Share this post


Link to post
Share on other sites
// null = [player,1,Tresure,RescuePlane] execVm "scripts\location.sqf";

_unit =	_this select 0;
_UnitPos = _this select 1;
_load = _this select 2;
_Plane = _this select 3;

if (_UnitPos == 1) then {_OffsetPos = [1,3.18,1.16594]};
if (_UnitPos == 2) then {_OffsetPos = [.8,4.97,1.16594]};

_unit setpos (_Plane modelToWorld _OffsetPos);

my mind blowup, this returnme _OffsetPos not defined... im let this for today :(

Share this post


Link to post
Share on other sites

_OffsetPos is a local variable and as this it is only known in its scope. In ur script the scope is the if statement,
just initialize it at the beginning of ur script like this:

_OffsetPos = [];

to clarify that:

if you set a local variable within a pair of {} brackets only then that variable is not known outside of those brackets

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, sarogahtyp said:

_OffsetPos is a local variable and as this it is only known in its scope. In ur script the scope is the if statement,
just initialize it at the beginning of ur script like this:


_OffsetPos = [];

to clarify that:

if you set a local variable within a pair of {} brackets only then that variable is not known outside of those brackets

yea i forget... too many hours... im having a breack now watching VIKINGs xD need to refresh my mind, thank u  saro. 

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

×