as anything in the loop??
for(var/mob/each_mob in mob_list) // example #1
to_chat("Hello, [each_mob]!")
for(var/mob/each_mob as anything in mob_list) // example #2
to_chat("Hello, [each_mob]!")
for(var/mob/each_mob as() in mob_list) // example #3
to_chat("Hello, [each_mob]!")
in the example, mob_list is a list type.
Let’s say you have these items in a list:
- mob_reference: [0x1000]
- object_reference: [0x2000]
- mob_typepath: “/mob/ghost”
- null: null
- mob_reference: [0x5000]
The first example will only take #1 mob_ref[0x1000] and #5 mob_ref[0x5000]
eventually, the loop will only process 2 times.
This is because “var/mob/each_mob” kinda does type check. So…
for(var/mob/each_mob in mob_list) // example #1
to_chat("Hello, [each_mob]!")
is identical to:
for(var/i in mob_list) // alt version of example #1
if(!istype(i, /mob))
continue
var/mob/each_mob = i
to_chat("Hello, [each_mob]!")
as both of these will do the same: only #1 mob_ref[0x1000] and #5 mob_ref[0x5000].
So, specifying a type path in a loop is handy when you want to grab wanted items and filter unwanted items when a list is complex with multiple stuff.
-
mob_reference: [0x1000]
This passesistype()
because it’s an existing reference type ingame. -
object_reference: [0x2000]
This is an existing reference type ingame, but this is not/mob
typepath. Fails. -
mob_typepath: “/mob/ghost”
This is “typepath” which doesn’t really exist ingame, only tells the path of an mob. Fails. -
null: null
Obviously fails. -
mob_reference: [0x5000]
This passesistype()
because it’s an existing reference type ingame.
Thus, you get only two.
the second/third example will take everything because of as()
and as anything
This means:
for(var/mob/each_mob as anything in mob_list)
to_chat("Hello, [each_mob]!")
is identical to:
for(var/i in mob_list)
var/mob/each_mob = i
to_chat("Hello, [each_mob]!")
as both of these will do the same.
These will grab everything in mob_list
into var/mob/each_mob
, but it will possibly cause a runtime.
This should be used with an exact purpose with an expected result.
WARNING
This is bad usage of type filtering loop.
for(var/mob/each_mob in some_list)
pass()
for(var/obj/each_obj in some_list)
pass()
for(var/turf/each_turf in some_list)
pass()
This is expensive because you’re going to run the loop 3 times, calling the list 3 times.
That should be this:
for(var/each_thing in some_list)
if(ismob(each_thing))
var/mob/each_mob = each_thing
pass()
else if(isobj(each_thing))
var/obj/each_obj = each_thing
pass()
else if(isturf(each_thing))
var/turf/each_turf = each_thing
pass()
This will run the loop once and do a thing. This is cheaper than the first thing.