Jump to content
Sign in to follow this  
rotceh_dnih

Remove from array if it dosnt exist in another array

Recommended Posts

Hey guys im having some trouble coming up with some code to remove items from a array

the array is getting all the backpackItems and i wish to save some things but exclude others

in my example i have

_backPackStuff = backpackItems _character;

Return something like

["item1","item2","item3","item4","item5","weapon1","weapon2"]

and i have a array of items i do want ie

_stufftokeep = ["item3","weapon1"];

how do i make a new array from _backPackStuff with only the items that are in _stufftokeep if they exist ?

i kinda have an idea of looping the array checking if the current item is in _stufftokeep then added it if it is there but im not sure about syntax or if thats the best way to do what i want and i do apologize for this thread i understand this is pretty basic and i have looked for a few hours but caint find an example of what im trying todo so any help would be great :) thanks again

hector

Share this post


Link to post
Share on other sites

that would be an intersection of arrays, and i don't know of a more direct way either than:

_result = [];
{
   if (_x in _backPackStuff) then {intersect set [count _intersect, _x];
}forEach _stufftokeep;

// _result now contains the stuff you want to keep

you do have some functions for arrays which you could look into by going into the function browser.

Array subtraction should also work but it is said to be less efficient than "set" command

Share this post


Link to post
Share on other sites

hey thanks for the reply that look like exactly what i want to do and i just tried it but im not sure its working

im running the code server side if that makes any difference

here how ive got it

_magazines = vestItems _character;
_backPackStuff = backpackItems _character;
_stufftokeep = ["ItemMap","ItemCompass","ItemWatch","Binocular"];

	_result = [];
	{
	if (_x in _backy) then {intersect set [count _intersect, _x];
	}forEach _stufftokeep;

if ((count _magazines) > 0) then {

diag_log ("_backPackStuff gear: " +str(_backPackStuff)); 
diag_log ("_result gear: " +str(_result)); 


};

could anything be stopping that ? it worked before with just diag_log ("_backPackStuff gear: " +str(_backPackStuff)); before i added your code.

Share this post


Link to post
Share on other sites

if (_x in _backy) then {intersect set [count _intersect, _x];

	_result = [];
	{
	if (_x in _backPackStuff) then {_result set [count _result, _x];
	}forEach _stufftokeep;

my bad kinda sleepy, changed the temp variable midway (twice! :butbut:), hopefuly you haven't spent much time banging your head against this :j:

Edited by gammadust
I'm on restraining order to bed

Share this post


Link to post
Share on other sites

Why would you have assigned items inside a backpack and not in their proper slots?

Share this post


Link to post
Share on other sites
Why would you have assigned items inside a backpack and not in their proper slots?

because i want players to be able to store 2'nd items in there bags and not sure how to get everything out of there bag my thinking at the moment is get the weapons + mags + items. even thought i think backpackItems returns everything in the bag im not 100% sure it dose this way i know im not going to miss anything.

and i got it working was missing a } did think to look till it didnt work a 2'nd time so thought in your tiredness u missed it and i was right :)


_magazines = vestItems _character;
_backPackStuff = backpackItems _character;
_stufftokeep = ["ItemMap","ItemCompass","ItemWatch","Binocular"];

	_result = [];
	{
	if (_x in _backPackStuff) 
	then {		
	_result set [count _result, _x];
	}		
	}forEach _stufftokeep;

thanks heaps for the help this was great glad i came here

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  

×