- Home /
Creating custom objects?
I'm looking to create a card trading game similar to Magic The Gathering.
I want to create a way to be able to easily make a card. So that in the Start() function I can simply initialise all my cards. Each card will need to have a unique ID for reference (Probably the objects name), to later be put into an array.
In my head, I know what I need to do, I'm just having an issue translating it into code as this is my first solo project.
After a bit of research I think it has to be done this way, and I just want it confirming by someone a bit more in the know.
So if I create a function..
Function Card () {
var name : String; var attack : int; var defence : int;
}
Then in Start(), I can initilise all my cards by using.... var c0001 = new Card(); c0001.name = "Red Creature"; c0001.attack = 1;
I can then create an array called Cards[], using the objects names c0001, c0002 and so on.
Have I got this right?
I posted an asnwer for you Optimus_L, but it needs to be approved by a moderator before it is visible.
Answer by Jacob-Aldridge · Mar 06, 2012 at 11:29 PM
I believe you are on the right track, but I think what you are talking about is creating a database of cards that you can reference easily and change easily.
You could create a Serialized class and then create a MonoBehaviour with an array of that class made public. You could then simply just modify the GameObject from within Unity and add/remove cards as you need. If you wanted to get fancy, you could create an Editor class for Unity and have a decent interface for creating cards in your CardManager.
using UnityEngine;
using System.Collections;
[Serializeable]
public class Card
{
public String name = String.Empty;
public Int32 damage = 0;
public Int32 defense = 0;
}
public class CardManager : MonoBehaviour
{
public List<Card> cards = new List<Card>();
public void Start()
{
}
public void Update()
{
}
}
Answer by Kiloblargh · Mar 06, 2012 at 08:44 PM
I don't know why you think Card() should be a function. I also don't think you'd be creating new cards in the game, you'd have them all pre-designed.
So I would make each card a prefab, with a mesh and material for its appearance, and attach a "cardScript" on it that holds its stats.
var theDeck : GameObject[];
var myCards : List.<cardScript>();
var yourCards : List.<cardScript>();
function DealCards (howMany : int)
{
}
function CompareCards (first : cardScript, second : cardScript)
{
}
There's a lot more to this sort of game that you're going to run into- how you are going to save a player's cards in a way that isn't easily cheatable, how to draw a random card with a the right probability based on its rarity, etc.
Actually that was my first solution, but I got confused thinking if I created a single script called CardInfo and attached it to a card I thought it would change all the values of all the cards but after trying it turns out it works fine. ...I'm still learning =/
Thanks for the help