- Home /
Keeping card variables together
I have a card game where each card has four values (type, value, cost, price) they each change but each can't be dynamically assigned because each card is different. Some example cards are |Attack, 4, 7, 10| in which the card is an attack card, that deals 4 damage, costs 7 action points to cast and costs 10 dollars to buy. |Block, 6, 15, 50| is a card that blocks 6 points of damage, costs 15 ap to play and costs 50 dollars to buy. What is the best way to keep these values constant and together. I have been looking at a few ways but I'm not sure.
No, the information will be applied to a prefab when it is processed. I was thinking, when drawing a card, it will randomly select a card from the library array and put it in the player's hand. But with four bits of data (or 3 in the game, doesn't need price when playing) it makes it harder especially because the cards can't be dynamic like a deck of playing cards which I once used a 2d array of bools because both red and black card had every number assigned to it but a poison card and an attack card of the same value will have different costs. I hope that makes sense.
Answer by fendorio · Apr 04, 2014 at 10:31 AM
Create an enum to store the type
public enum CardType
{
attacking,
blocking,
whatever_else
}
Create a class to hold each cards values.
public class Card
{
public CardType type;
public int card_value, card_apCost, card_costToBuy;
Public Card(CardType type, int value, int apCost, int costToBuy)
{
card_type = type;
card_value = value;
card_pCost = apCost;
card_costToBuy = costToBuy;
}
}
Then create a Script Component for each card and give it an instance of the Card Class to store it's values?
Unsure if you wanted JavaScript, it shouldn't be too hard to translate.
Note that that data structure is pretty unsafe - unsure if you're concerned about that.
For instance you could add some encapsulation to protect the values by using getters/setters - like so:
public int card_Value { get; private set; }
Now you can only set the card_Value inside the class, so to set it from outside you'd need to add an accessor method - like so:
public void Set_Card_Value(int value)
{
//Add some validation logic if required - such as
if(value < 0)
{
value = 1;
}
else
{
card_Value = value;
}
}
Also note that if you do not add an accessor method and use the { get; private set; } getter/setter as stated above, the value is going to be immutable (a constant value).
Going on from your comment:
Create a gameObject to use as a prefab, then attach a script component to it. Let's just say it's called ScriptOne for simplicity.
public class ScriptOne : MonoBehavior
{
public Card the_instance_I_Was_Talking_About;
void Awake()
{
the_instance_I_Was_Talking_About = new Class(CardType.attacking,
10,
5,
0);
}
}
How you want to assign the values in awake i'm unsure, i'd need to know more about your game.. But yeah, now accessing that, is as simple as calling .GetComponent().
Something like this - assuming you assigned the object with a tag in this instance.
void SomeFunctionElseWhere()
{
int x = GameObject.FindGameObjectWithTag("WhateverTagYouUsed")
.GetComponent(ScriptOne)().cardValue;
}
Note that the 'ScriptOne' in the above .GetComponent should be enclosed in those triangles (Chevrons? lol), but they don't show up in the answer so I used brackets instead.
No worries! Just @me in a comment if you run into any trouble, i'll be poking around for most of the afternoon :P
One thing I'm not sure about is what you mean by give the instance of the class to the script. Sorry for the trouble, I'm still learning and this seems to be a difficult one. @fendorio
@Scobbo please consider accepting the answer if it worked for you :P
Your answer
Follow this Question
Related Questions
How to smootlhy change values in amout of time? 3 Answers
Synchronizing multiple values 1 Answer
How do I create a Key and Values Dictionary array in C# 1 Answer
Inventory system. Set multiple values/layers to one variable. 0 Answers
Tree Structure: update root element (reference) for all classes 1 Answer