ID:159847
 
For instance, If I wanted to create an instance of every fruit defined in the code. I can use typesof() to get a list of every fruit, but how do I create an instance of each of them? Every time I try I either get an error or the instance simply comes out as 'fruit' rather than 'apple' or 'orange'.
mob/verb/CreateFruit
for(var/obj/O in typesof(/obj/Fruit))
new O(src.loc)


Untested, but the general concept will work.
I don't have access to Byond at the moment so I can't test this, but I can imagine it would be something like:
for(var/fruit in (typesof(/obj/fruit) - /obj/fruit))
var/obj/fruit = new fruit(src)

You might need to use text2path in there as well, can't recall at the moment...
It sounds like the problem you're having is that typesof() includes the root (/obj/fruit) and you're creating that rather than any of the others.
If what I posted above doesn't work (highly likely), seeing what you've attempted to do might be worthwhile also.

Edit: Shucks, Mizukouken beat me to the punch
In response to Mizukouken Ketsu
for(var/obj/O in typesof(/obj/fruit/))
var/obj/OO = new O(src.loc)
world << OO.name


This is what I tried after reading your post. I get no output at all.
In response to Karrote
I don't believe you can use var/obj/O in the for loop, as the typesof() returns the paths rather than not objects. Make it var/O and see if that works.
In response to Ephemerality
Changing to var/O did it. Thanks.
In response to Karrote
Ephemerality's code was more correct, despite not compiling due to a tiny mistake. At any case though you should read about and look up the instructions (in this case for(), typesof(), and perhaps new() if needed) involved in code given to you, rather than just immediately copy or imitate it.

The problem in that code is that typesof() returns a list of type paths, and an object filter for object instances of type /obj is used in the for() loop, meaning it will only iterate (execute) through /obj instances in that list - but there are none (as there are only type paths), so it doesn't run at all.
You may also want to exclude the /obj/fruit type itself from the list; typesof() will include it in the returned list as well, which may not be desired if it's merely an ancestor/'category' type intended for inheritance and not actual usage. Ephemerality demonstrated this as well, although I'd prefer to remove it from the list with the -= operator.

EDIT: Ah, Ephemerality beat me to it. =P