ID:272681
 
I'm trying to make a deck editor for a game I'm working out and am having trouble making a list of all the cards in the game. I'm abit new to this so this may not be worded quite right but what I want to know is how to make a list that will list all the card objects for the game when the game is loaded. For example: Lets say I have

obj
Card/Card1
icon = Card 1.dmi'
obj
Card/Card2
icon = 'Card 2.dmi'

How would I make a list that has these objects in it when the game is loaded?
world/New()
Cards=list() // initialize the cards list
for(var/a in typesof(/obj/card)) // search for every typepath under obj/card
var/obj/D = new a // create an object with that type
Cards += D // add the object to the cards list
..()

var/list/Cards


Hope I was clear enough.
In response to Andre-g1
Thank you, thank you so very much, that was exatly what I was looking for, now I can continute with this game, and once again thank you.
In response to Andre-g1
If I remember right, /obj/Card counts as a type of /obj/Card, so you might want to exclude it from the newly created objs.
In response to Jon88
Yeah, that's correct. typesof(Type) also returns Type in the list, which is often not wanted.
var/list/all_cards = new
world/New()
var/list/card_paths = typesof(/obj/card)
card_paths -= /obj/card //remove the extra path
for(var/X in card_paths)
all_cards += new X

Depending on what exactly the OP is doing, having just the list of the types might be enough, which is better if possible, of course, since you're not using tons of objs permanently in the background. For example, you could do this easily if what you want is e.g. giving a player a bunch of randomly-chosen cards, or something.
In response to Kaioken
You don't even need the extra line to remove the item you can do this:
var/list/blah = typesof(/obj/blah)-/obj/blah
In response to Nadrew
You don't need the extra line, but it's more efficient, as the + operator needlessly initializes a new list while the existing one can be used. Complete code compactness isn't required if you're not working on a 4k contest project. =P
In response to Kaioken
cant you just do this
for(var/a in typesof(Type)-Type)
blah

thats what I do when I do stuff like this or is your method still more efficient
In response to NightJumper88
That's exactly what Nadrew just said.

NightJumper88 wrote:
cant you just do this

Yes, you can do this. It's just a little slower than my preferred method, but you could prefer this one in your opinion. Unless you're removing multiple items from the list like I often have in the example below, in which case you should really have the code less compact and not use an operator that creates a new list when you don't need a new list. (For multiple removals I also use the Remove() proc with multiple arguments)
obj/equipment
weapon
sword
iron_sword
staff
iron_staff
//...
armor
cuircass
shield
//...
proc/get_random_weapon_path()
var/list/L = typesof(/obj/equipment/weapon)
L.Remove(/obj/equipment/weapon,/obj/equipment/weapon/sword,
/obj/equipment/weapon/staff)
return pick(L)

It can become even more nested than that. >_>
In response to Kaioken
the operator might be a little slower but your doing the exact same thing by creating a list variable its just your doing it yourself. typesof() creates a list so your creating a list and then assigning a list to that list so your making two list and then removing from the list. My method which is slightly different than Nadrews cause he creates a list also. So if the "-" operator creates a list then im creating a total of two list, same as you, but my method is a lot more compact and less to type.

Now Im not sure how how exactly the difference between "-=" or "-" but from what youve said it sounds like the main difference is creating a list first, but again its all personal preference
In response to NightJumper88
Using the - operator, if used on a list, returns a new list without the object or whatever you're subtracting from the list.
In response to NightJumper88
NightJumper88 wrote:
the operator might be a little slower but your doing the exact same thing by creating a list variable its just your doing it yourself. typesof() creates a list so your creating a list and then assigning a list to that list so your making two list and then removing from the list. My method which is slightly different than Nadrews cause he creates a list also. [...]

Well, no, see, you don't really understand how the code works. Your code produces 2 lists, one returned from typesof() and another from '-', just like Nadrew's. The difference is that Nadrew stores the list into a variable first (if for anything but demonstration). It looks like you're of the wrong belief that doing something like:
var/list/L

...creates a list on its own, or contributes to a list being created. That's wrong. Typecasting, or defining a variable's type, is done for compilation purposes only, mainly error checking at that - it doesn't affect anything at runtime, and does not alter the variable's value. The above L variable has no value (null value).


Now Im not sure how how exactly the difference between "-=" or "-"

Andre pretty much answered it; '-' creates a copy of the list you're using, then removes the item you've supplied from it and returns that new list, while '-=' removes the item from the actual list it's used on.
The functionality of '-' does have uses, just different from the one here. Consider lists where you don't want (or can't) to actually modify the original list, but still want to include different items in the list you're using:
obj/item/Item_Teleporter
var/turf/destination
Use()
set src in usr.contents
if(src.destination)
for(var/obj/item/O in usr.contents-src)
//move everything except the Item Teleporter
O.Move(destination)

Since '-' is creating a new list, it doesn't remove the item from the actual contents, so the player doesn't lose it.
In response to Kaioken
Another benefit of using - alone is that you can remove multiple items without the need for multiple -= uses or calling Remove().

var/list/somelist = typesof(/obj/blah)-/obj/blah-/obj/blah/bleh
In response to Nadrew
_> <_< That's not a benefit, that's much worse, since you create a new list for every removal (so you have 1+Items lists created when you only need 1). If already it should be done through List - list(item1,item2,item3) which only takes a total of 2 lists.
In response to Kaioken
Try profiling it, you'll find you're quite mistaken.
In response to Nadrew
You mean the compiler actually optimizes it when you use '-' multiple times consequently? That's great! I assume only one new list is created in such a statement.
My bad, then; now I'll know I can use '-' multiple times when I want to remove multiple items in an appropriate situation. I'm still going to use Remove() instead as appropriate when I don't need a new list, of course, since I don't mind typing some more in the source and care more about the execution, but to each his own. Thanks for the enlightenment. =)