f2k sel 164 Posted January 23, 2013 (edited) A while back someone made me a function to help keep track of actions, I thought I had it working but left it unfinished. Coming back to it I'm struggling to understand it again. The problem seem to be removing the element based upon the name. I have this array, it may contain all the same items or a mixture _actionsarray = [[0,"money",3],[1,"money",2],[2,"money",1]] 0 - is the id , "money" the name , 3 how many of that item. When I want to remove an item use [player, "money"] call fnc_removeAction the problem is it removes all but one item What I need is to remove the first [0,"money",3] private ["_target", "_actionname", "_actionsarray", "_actionid"]; _target = _this select 0; // object or vehicle or player the action will be removed from _actionname = _this select 1; //String. Name of the action, visible when you have the action _actionsarray = player getVariable "ActionsArray"; //checks the array of available actions for that object { if (_actionname in _x) then //finds the action name in the main array { //_x = _actionsarray select 0; _x set [2, (_x select 2) - 1]; //remove 1 from the number of actions of this type remaining if (_x select 2 != 0) then //if 0 actions of this type remaining { _actionid = _x select 0; //gets the ID of the action _actionsarray set [_actionid, -1]; _actionsarray = _actionsarray - [-1]; //removes the action from the array _target removeAction _actionid; //removes the action from the target hint str _actionsarray; }; }; } forEach _actionsarray; Any ideas on how I can remove 1 element at a time? Don't worry if it makes no sense it doesn't to me either any more. Thanks. Edited January 23, 2013 by F2k Sel Share this post Link to post Share on other sites
Mattar_Tharkari 10 Posted January 23, 2013 It's the ol - nested elements can't be subtracted thing? http://community.bistudio.com/wiki/Array#Subtraction Nested arrays cannot be substracted - i.e. the following does NOT work: _a1 = [[1,1],[2,2],[3,3]]; _a2 = [[2,2]]; _a3 = _a1 - _a2; // will still return [[1,1],[2,2],[3,3]] Workaround: You can remove nested arrays by first replacing them with a non array variable. Example: _array = [["first","hello1"],["second","hello2"],["third","hello3"]]; _array set [1,-1]; _array = _array - [-1]; // _array will now contain: [["first","hello1"],["third","hello3"]] Share this post Link to post Share on other sites
f2k sel 164 Posted January 23, 2013 (edited) Isn't that what I'm doing? here I'm reducing a number _x set [2, (_x select 2) - 1]; and later I remove the element _actionsarray set [_actionid, -1];_actionsarray = _actionsarray - [-1]; I'm not seeing the difference. Edited January 23, 2013 by F2k Sel Share this post Link to post Share on other sites
riouken 15 Posted January 23, 2013 Your algorithm also has a logic error. It will never select the last item, ie when you have 1 left. It will just skip over that one. private ["_target", "_actionname", "_actionsarray", "_actionid"]; _target = _this select 0; // object or vehicle or player the action will be removed from _actionname = _this select 1; //String. Name of the action, visible when you have the action _actionsarray = player getVariable "ActionsArray"; //checks the array of available actions for that object { if (_actionname in _x) then //finds the action name in the main array { //_x = _actionsarray select 0; // [color=#ff0000][b]This line should be at the end of your function, what happens when _x is 1 and this line runs before your if check below? The way it is now it will never work for the last element.[/b][/color] _x set [2, (_x select 2) - 1]; //remove 1 from the number of actions of this type remaining if (_x select 2 != 0) then //if 0 actions of this type remaining { _actionid = _x select 0; //gets the ID of the action _actionsarray set [_actionid, -1]; _actionsarray = _actionsarray - [-1]; //removes the action from the array _target removeAction _actionid; //removes the action from the target hint str _actionsarray; }; }; } forEach _actionsarray; Share this post Link to post Share on other sites
Mattar_Tharkari 10 Posted January 23, 2013 (edited) It's the _actionname as well - all three nested arrays contain "money" so it will remove all 3. You need to refer to them with something unique eg the id number? Or change the action names to make them unique for each addaction? eg: _actionsarray = [[0,"USmoney",3],[1,"EUmoney",2],[2,"UKmoney",1]]; Edited January 23, 2013 by Mattar_Tharkari Share this post Link to post Share on other sites
f2k sel 164 Posted January 23, 2013 Thanks guys, I don't want it to be a unique name as that would involve naming them or at least auto incrementing the name with a number. I'm trying to use the the ID and remove the last entry of that type ie last in first out. I think I have just about gotten my head around it again, so if there are more than one of that type I need to check the the final number in the first element and use that as the ID number for the action I want to remove. I then adjust the final number in the remaining elements as Riouken suggests. Share this post Link to post Share on other sites