ID:171253
 
I have an NPC in my game, and he has to have a deck of about 40 cards. The only method I can think of doing this is this:

mob
NPC
icon = 'Users.dmi'
icon_state = "NPC"
var/obj/A = new /obj/Cards/Neutral/Peasant()
var/obj/B = new /obj/Cards/Neutral/Knight1()
var/obj/C = new /obj/Cards/Neutral/Defence1()
var/obj/D = new /obj/Cards/Neutral/Peasant()
var/obj/E = new /obj/Cards/Neutral/Knight1()
New()
Cards.Add(A,B,C,D,E)


This is obviously not the best way, but I don't know how else to do it! That uses lots of code, and that's for only 5 cards!

Note: If you want more than 2 of an object in the deck you can just use a letter twice (e.g. Cards.Add(A,A,A,B,C,D,E)) because then when the NPC plays more than one A's, only one appears on the board because they are all the same object!

How should I do this?

~Ease~
Ease wrote:
I have an NPC in my game, and he has to have a deck of about 40 cards. The only method I can think of doing this is this:

> mob
> NPC
> icon = 'Users.dmi'
> icon_state = "NPC"
> var/obj/A = new /obj/Cards/Neutral/Peasant()
> var/obj/B = new /obj/Cards/Neutral/Knight1()
> var/obj/C = new /obj/Cards/Neutral/Defence1()
> var/obj/D = new /obj/Cards/Neutral/Peasant()
> var/obj/E = new /obj/Cards/Neutral/Knight1()
> New()
> Cards.Add(A,B,C,D,E)

This is obviously not the best way, but I don't know how else to do it! That uses lots of code, and that's for only 5 cards!

Note: If you want more than 2 of an object in the deck you can just use a letter twice (e.g. Cards.Add(A,A,A,B,C,D,E)) because then when the NPC plays more than one A's, only one appears on the board because they are all the same object!

How should I do this?

~Ease~

To my understanding, you'll need to define the cards at one point or another. If the cards the NPC has have to be specific, I'm not sure there is a much better way of doing it. What you may want to do it have the list hold the name of the card, not the actual card. When the card is played, then create the object.(I hope I'm not losing you here...) That way you can have the same name 2 or more times in the list, but have a different obj for each.
Depending on how everything is set up, you could try and adapt the following snippet to suit your needs.
mob/npc/New() // when a new npc is created
var/list/cardlist = typesof(/obj/cards) - /obj/cards // make a new list of card types, and store them as cardlist
for(var/i = 1, i <= 40, i++) // cycle through 40 cards
var/cardtype = pick(cardlist) // choose a random card type
Cards.Add(new cardtype) // add a new instance of that card to the list


In theory, that should pick 40 random cards of type /obj/card, and add them to the npc's card list. But since i dont know how your game is structured, i could only provide a brief sample on how to add random cards.
In response to Lazyboy
That will work for making a random deck, thank you very much! =D

~Ease~
In response to Jik
Nope, you didn't lose me there, I understood! =D Thank you very much!

~Ease~
mob/npc/New(list/starting_deck)
for(var/C in starting_deck)
for(var/index = 1 to starting_deck[C])
Cards+=new C
mob/npc
npc1/New()
var/list/starting_deck=list(\
/obj/cards/Neutral/Peasant = 2,\
/obj/cards/Neutral/Knight1 = 2,\
/obj/cards/Neutral/Defence1 = 1\
)
..(starting_deck)
npc2/New()
var/list/starting_deck=list(\
/obj/cards/Neutral/Knight1 = 1,\
/obj/cards/Neutral/Defence1 = 4\
)
..(starting_deck)

Assossiative lists put to good use once again. The number that each type is set to is the amount of that card type that will be in the deck. So /obj/cards/Neutral/Peasant = 2 will put two Peasants in the deck while /obj/cards/Neutral/Defence1 = 1 will put 1 Defence in the deck.

It saves a bit on the typing, and shortens it up even moreso when multiple cards of a type are needed.

I stashed the card-making procedure in the base mob/npc type so it could be refered to with ..() instead of retyping that part over and over. And I put the list definition in New so you don't have an extra list object hanging around for every different type of npc, rather having one only while it is created then it is ditched.