ID:173822
 
Okay, here's the deal. I am attempting to merge the spell lists of two mobs (because they are merging into one), and redundant spells (copy of the same spell) from the two previous mobs still appear in the resulting mob's spell list. Here is the code I use to do it.

(whereas pedigree and soul are the two mobs that will merge... I also added debugging lines to help me out, but to no avail...)

var/obj/spells/list/newskills = list()
for(var/obj/spells/skillz in pedigree.skills)
if(soul.skills.Find(skillz))
usr << "Found [skillz] in soul list, removing..."
soul.skills.Remove(skillz)
newskills.Add(skillz)
for(var/obj/spells/skillz2 in soul.skills)
newskills.Add(skillz2)


As you see, my spells/skills are objs. I don't know what I'm doing wrong, so if anyone could help, it would be greatly appreciated.
You may want to check out Copy(), I believe this allows you to copy one list into another while keeping duplicates out, but I canīt be sure, Iīm in a hurry (at the library) and I canīt check the details right now.
In response to Nadrew
Copy() seems to return a new list, and it seems to only work with one list at a time. So I still am lost. I may be dense, I dunno, but I've been trying to figure this out since early this morning, but to no avail...

Anyone care to show me how I could use it in this particular situation?


Thanks in advance
In response to Nadrew
Btw, I just figured out how to use it, but it gives me the same result: it still keeps duplicate in the new list...

Well, your problem is that obj A and obj B, will not be equal, and thus, Find() does not find obj A in obj B's list. What you need to do is make a new proc for this purpose:
// First argument is list to search.  Second argument is type path to search for.
proc/findType(list/L, var/T)
for(datum/V in L)
if(istype(V, T))
return V

Then, use that instead of Find() (You'll have to do findType(soul.skills, skillz.type)), and it should work.
In response to Garthor
Garthor wrote:
Well, your problem is that obj A and obj B, will not be equal, and thus, Find() does not find obj A in obj B's list. What you need to do is make a new proc for this purpose:
> // First argument is list to search.  Second argument is type path to search for.
> proc/findType(list/L, var/T)
> for(datum/V in L)
> if(istype(V, T))
> return V
>

Then, use that instead of Find() (You'll have to do findType(soul.skills, skillz.type)), and it should work.


Well, here's how I modified my code:

for(var/obj/spells/skillz in pedigree.skills)
if(findType(soul.skills,skillz.type))
usr << "Found [skillz] in soul list, removing..."
soul.skills.Remove(skillz)
newskills.Add(skillz)
for(var/obj/spells/skillz2 in soul.skills)
newskills.Add(skillz2)


The result is exactly the same... objs duplicate in the newskills list. What did I do wrong? Or maybe I didn't use it the way I should have?...

In response to Mart2J
Well, you're close. However, Remove() works the same way as Find(), and thus, you are removing something that isn't in the list. You need to store the result of findType() and remove that instead.
In response to Garthor
Thanks a million! It now works wonders! I really appreciated your help.