kazesim 2 Posted August 15, 2010 I wanted to ask about some strange links Iv found beween copies of an array when using the "set" command. They've been causing my endless problems for the last two days. It seems that whenever an array (apparently not any other data type) is copied, it retains some kind of link with its original, when you use the "set" function on the new copy, it also changes the original. As with the following code: _var = [10]; _newVar = _var; _newVar set [0, 20]; hint format ["%1 - %2", _var, _newVar]; Logic would suggest that it would show "[10] - [20]" when in fact it shows "[20] - [20]", where set is apparently changing "_var" as well. It also does this across different scripts when var is a global and _newVar is local, and through set/getVariables such as in: _var = [10]; player setVariable ["testv", _var, false]; _newVar = player getVariable "testv"; _newVar set [0, 20]; hint format ["%1 - %2", _var, _newVar]; //Hint is [20] - [20] It also does this when the original array is included in another array, changing both the original and the one in the larger array: _var = [10]; _var2 = [10]; _var2 = _var2 + [_var]; _newVar = _var; _newVar set [0, 20]; hint format ["%1 - %2 - %3", _var, _var2, _newVar]; //Hint is [[20] - [10,[20]] - [20] I haven't been able to break this dependency with "private" or by running it in call or spawn, the only way seems to be to create an whole new array, there are two ways Iv found to do this. Ether create a new one and copy all the elements into it with select: _newVar = [_var select 0, _var select 1, etc]; Or copy the array, then convert it to a string and then back to an array before using "set": _newVar = _var; _newVar = call compile str _var; Hope that helps, and I wanted to ask if anyone else has noticed this? Iv spent the last two years scripting missions for ArmA and ArmA2 and this is the first time Iv had a problem with it. If anyone else can verify this it would be helpful to post a note on the wiki. Share this post Link to post Share on other sites
Muzzleflash 111 Posted August 15, 2010 This is quite normal for most scripting and programming languages. _var is not a an array. It is a reference to an array. When you set _newVar = _var; Then you make _newVar a reference to the same array. You now have to references to the same array. When you change something in one reference it is the underlying array that get's changed. Maybe you have not seen this before because you haven't used set alot before? ---------- Post added at 23:26 ---------- Previous post was at 23:23 ---------- If you want to copy an array the wiki gives this method: _array1 = [1,2,3] _array2 = + _array1 From: http://community.bistudio.com/wiki/Array#Copying Share this post Link to post Share on other sites
kazesim 2 Posted August 15, 2010 Thanks for the reply, Iv used set a lot but I guess its never altered other variables in a way that was noticable. By the way, are other data types also saved in that way, where the variables are just pointers? Share this post Link to post Share on other sites
Muzzleflash 111 Posted August 15, 2010 (edited) I'm sure which exactly. However, pretty sure basic primitives such as: Numbers Booleans Strings (probably due to immutability) is not by-reference. I think all data types more complex than the array is most likely all by reference. Arrays Groups Probably Sides too. Personally, I just treat it like a reference if it is not one of the 3 types I listed first. However, I can't at the top of my head, come up with any situation where the difference is important in practice besides arrays. Edited August 15, 2010 by Muzzleflash Share this post Link to post Share on other sites
kazesim 2 Posted August 15, 2010 (edited) Alright thanks for the info, I can now complete my l33t weaponset system. My best guess what was happening is that my weaponset global includes the return from ACE_fnc_ruckWeaponsList, which gets the array from the unit's ruck contents variable. And when you put an item in your rucksack and save the weapon set, then take the item out, ACE_fnc_removeWepFromRuck would set the count to 0 on its own local copy of the ruck contents, which also changed the count nested 3 deep inside my weaponset array. Scripting is so much fun sometimes! ...And I fixed the whole thing by adding a space and a plus sign! Edited August 15, 2010 by kazesim Share this post Link to post Share on other sites