Jump to content
Sign in to follow this  
Pac Man

Iterate through backpack items putting one of each item into an array

Recommended Posts

I'm trying to iterate through back pack items and throw one of each item type into an array, for the purpose of weeding out duplicate/same items. ie; player has 3 mines, 2 grenades and 4 magazines. I only want one of each thrown into the array. The ultimate goal here is to search through a units backpack, find the items and then return how many of each item there is... on the fly. As the player could have all kind of things in his backpack at any given moment.

This partially works, and i'm getting really close to my goal, but throws an error and totally disregards the first "would be" item leaving the array one item short. Any help or other method is appreciated.

init.sqf

waituntil { time > 1 };
_array = [];
for [{_i=0},{_i < count backPackItems player},{_i = _i + 1}] do {
  _newitem = backPackItems player select _i;
  _oldItem = backpackItems player select _i - 1;
  if (_newItem != _oldItem) then {_array = _array + [_newItem];};
};

//[b]outputs one of each item / magazine except the first and counts them[/b] - almost there!
for [{_i=0},{_i < count _array},{_i = _i + 1}] do {
   _count = ({_x == _array select _i} count items player) + ({_x == _array select _i} count magazines player);
   player globalChat format ["%1 %2", _array select _i, _count];

};

rpt

Error in expression <ect _i;
_oldItem = backpackItems player select _i - 1;if (_newItem != _oldItem)>
Error position: <select _i - 1;if (_newItem != _oldItem)>
Error Zero divisor

Edited by Pac Man

Share this post


Link to post
Share on other sites

Something like this should work:

_array[];
{
if ((_array find _x) == -1) then {_array = _array + [_x]}
}foreach backPackItems player;

{
systemChat _x;
}foreach _array;

Edited by Jacmac

Share this post


Link to post
Share on other sites
_items = [];
{
if (!(_x in _items)) then {
	_items set [count _items, _x];
};
}forEach backpackItems player;

Share this post


Link to post
Share on other sites

Thanks for the replies. We've been trying to figure this pout for some hours now. Gonna try and report back asap.

Share this post


Link to post
Share on other sites

I ended up using Larrow's method. Thankyou both very much for the help.

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  

×