- Home /
How to make TCG Deck (Instatiate based on Prefab)
Hello everyone! I have been trying to make the skeleton of a card game (TCG/CCG). I am having a bit of trouble wrapping my head around the deck element. So far I have a Card class which is serialized to construct all the elements of cards in the game.
Card class:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Card {
public string cardName;
public int cardID;
public int cardMana;
public int cardAttack;
public int cardDefense;
public int cardSpeed;
public CardType cardType;
public SubType cardSubType;
public Faction cardFaction;
public int cardGems;
public GameObject cardPrefab;
public enum CardType{
Summon,
Chant,
Equipment
}
public enum SubType{
Creature,
Structure,
Armor,
Weapon
}
public enum Faction
{
Neutral,
Chaos,
Water,
Earth,
Wind,
}
public Card(string name, int id, int mana, int attack, int defense, int speed, CardType type, SubType subType,
Faction faction, int gems)
{
cardName = name;
cardID = id;
cardMana = mana;
cardAttack = attack;
cardDefense = defense;
cardSpeed = speed;
cardType = type;
cardSubType = subType;
cardFaction = faction;
cardGems = gems;
cardPrefab = Resources.Load<GameObject>("Prefabs/" + name);
}
}
In addition to this, I have created a CardDB class which manages all of the cards in the game. I will reproduce its code here:
Card database:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CardDB : MonoBehaviour {
public List<Card> cards = new List<Card>();
void Start(){
cards.Add (new Card ("Annoyed Knight", 0, 1, 2, 2, 3, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
cards.Add (new Card ("Infuriated Bear", 1, 3, 5, 4, 4, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
cards.Add (new Card ("Ballistic Bear", 2, 4, 5, 6, 5, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
}
}
I'm wondering how to add these items to list in which I can call the prefab that they reference and instatiate it when it is drawn. This is what I currently have for the deck script:
Deck:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class PlayerSummonDeck : MonoBehaviour {
public List<Card> playerSummonDeck = new List<Card>();
public CardDB database;
void Start ()
{
database = GameObject.FindGameObjectWithTag ("CardDB").GetComponent<CardDB>();
playerSummonDeck.Add (database.cards [0]);
}
}
Overall question: How do I make it so that when I click on the deck it draws the top card of the list, instatiates it as the prefab? Is there a way to reference the prefab attached to the card within the database?
I have created a prefab for each of the 3 cards referenced in the database. The script above sets it to load the prefab from the resources folder called Prefabs + name where "name" is the name of the card in the database. I would really appreciate some help with this! Thank you for looking and sorry about the wall of text.
There could be better ways to do it but i think having something like a count to index the card will be easy.
// To represent which card you are drawing from the list
// assu$$anonymous$$g that you added the list from 0 to Count is
// first to last.
int cardToDrawIndex = 0;
// Happens when you draw a card
void OnDrawDeck()
{
DrawCardAndInstantiate();
// Increment index to draw next card in list.
cardToDrawIndex++;
}
// Instantiating the gameobject.
private void DrawCardAndInstantiate()
{
GameObject.Instantiate(database.cards [cardToDrawIndex].cardPrefab);
}
If you did set up your prefab correctly it should, draw the first card (cardToDrawIndex) and then increment it then instantiating the prefab you referenced from the cardDB as a gameobject
Edit: Using a stack could also be better than a list
Thank you for your suggestion. I'm going to try to implement it within the next few days. I think I was just conceptualizing the problem the wrong way. Thank you very much!
Answer by IHackedDeath · Sep 07, 2015 at 04:03 AM
Hi MJacobsen186,
I had a quick skim over your code and problem, I thought I would add what I thought would help you out a bit.
With your cards, I would have them in a script list, then out of that list make a new list with all the cards that the user currently owns.
With all the cards that is in the list that the user owns, give them a menu that they can choose what cards are in their deck, and from that list you use that as their deck.
At the start of their turn, the list is randomized and then it instantiates prefabs of the id's from the cards on top like what MewEight suggested.
This is my personal opinion anyway, you can use this, or similar or even your own that you figure out :)
I hope this helps.
Kind Regards,
IHackedDeath.
i Really liked this list. it cleared up the process i needed for my scrambled $$anonymous$$d. just need to work on executing it now.
Your answer
Follow this Question
Related Questions
Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers
How do I Instantiate a prefab in a list? 0 Answers
Destroying assets is not permitted to avoid data loss. 0 Answers
Game Object is Instantiating on same position 2 Answers
Having multiple objects fire prefabs in different times C# 0 Answers