ID:151809
 
Alright...I just want to know. Would these to do the same thing:
var/list/stuff = list()
//...
var/list/L = new()
L += stuff// Would that do the same as this
//...
L = stuff//Or will that actually mean refer to that variable as this.
Yeah...my second one was right guess was right...it actually refers to the variable as that.

I was just wondering, because I had an enemy list...and a proc that returns a mob for instance:
proc/Atk(mob/M)
list/L = M.enemies
for(var/mob/O in M.enemies
if(O.dead) L.Remove(O)
return L


Then I noticed that whenver I did that, they started attacking null, so I put in a verb to check the enemies var and it was returning that it subtracted whoever they attacked from their REAL enemy list.
This is more for the developer how-to.


Yes, it does do the same thing, as L is empty. If L wasn't empty, though, it wouldn't be the same thing. Adding two lists is the equivalent of combining two lists. Setting one list equal to another is exactly what it sounds like.
In response to Jeff8500
I figured as much, and as for the forum stuff. I was thinking "Don't go Dev How-To!" because it wasn't really how-to sounding to me. But I guess it does seem a bit How-To'ish now that I think about it.
Choka wrote:
Alright...I just want to know. Would these to do the same thing:
> var/list/stuff = list()
> //...
> var/list/L = new()
> L += stuff// Would that do the same as this
> //...
> L = stuff//Or will that actually mean refer to that variable as this.


Couldn't you test this yourself? You could do both methods and output the list to see what happened? It's self-explanatory when the situation is re-created and tested.
In response to Choka
Copy() the list in a variable, if you need to mess with it without messing with the real one.
mob/var/list/ListThing

//somewhere in the code:
var/list/TempList = ListThing.Copy()

I heard "L-=O" is faster than "L.Remove(O)", but there's probably no real difference. It's less to type, so I do it. Same goes for Add().
In response to Kaiochao
Add() and Remove() are actually quite a bit slower than += and -=. Add() and Remove() are really only useful when you're working with multiple list items.

list.Add(item1,item2,item3,...)
list.Remove(item1,item2,item3,...)


And due to the extra processing this method creates it'll end up slower than other methods, the difference in speed really depends on usage.
In response to Nadrew
So Add() us faster than multiple +='s?

List.Add(item1,item2,item3...)

//vs

List += item1
List += item2
List += item3
//...
In response to Spunky_Girl
It's easier to type, but apparently Add() as a proc is slower than +=.
In response to Kaiochao
So is that a "no, Add() is not faster than multiple +='s"?
In response to Nadrew
What about

mylist+=list(item1,item2,item3)

?
In response to Spunky_Girl
Spunky_Girl wrote:
So Add() us faster than multiple +='s?

Generally yes. Multiple +=s probably execute a function multiple times, Add() runs just once and adds everything in its arguments by looping through them. You could create a list with the wanted items to add at once with +=, but that's pretty pointless (unless you already have a list anyway, of course) since you end up calling new /list() anyway, might as well just call Add().
In response to T3h P3ngu1n
Like what Kaioken said, it's most likely slightly slower than the Add() proc.
In response to Spunky_Girl
Hopefully you've just meant to write "slower" instead, since that's what I noted/meant. >_>
In response to Kaioken
Yeah. I tested them earlier, and +=list() stuff is slower than Add.
In response to Jeff8500
Yes, it does do the same thing, as L is empty. If L wasn't empty, though, it wouldn't be the same thing. Adding two lists is the equivalent of combining two lists. Setting one list equal to another is exactly what it sounds like.

Well not quite the same. Adding the contents of one list to a new one would make a copy. Setting L to stuff would make L reference the stuff list. If you make a copy and add an item to L, stuff will still contain the same things. If however you assign stuff to L then adding to L will also add to stuff since they are both referencing the same list.