- Home /
Storing game data for game attributes?
I am creating a turn based card battle game, where the player fights the computer using a deck of cards with a series of unique attributes and values. Think of the likes of Magic the Gathering, Pokémon cards, Top Trumps, etc. Currently I have the following base class set up defining what a card is. From this I have another script for each and every type of unique card that use this class. However, these individual classes are really rather small, 3 or so lines.
I am curious to know if there is a more efficient or better way to handle storing this information. I've looked at playerPrefs, and to be honest I'm not really sure how I use them to store the information for each card and then reuse the information where I need to. Also I'm not really sure where to use the PlayerPrefs.
I would really appreciate a hand or a point in the right direction.
public class Card{
private int _cid; //Card ID
private string _ctype; //Card type
private int _scost; //Supply cost
//public GameObject celticPrefs;
//Setting up initial values for the above variables.
public Card()
{
_cid = 0;
_ctype = "Empty";
_scost = 0;
}
//Setters a getters
#region
public int Cid
{
get{return _cid;}
set{_cid = value;}
}
public string Ctype
{
get{return _ctype;}
set{_ctype = value;}
}
public int Scost
{
get{return _scost;}
set{_scost = value;}
}
#endregion
}
//different types of cards which I have broken up again into a new class ...
public class WarriorCards : Card {
public WarriorCards()
{
Ctype = "warrior";
}
}
public enum WarriorAttribute{ Dmg, Hp, Effect, MoraleCost }
... //Unique Card class
public class Warrior1 : WarriorCards{
public Warrior1()
{
Cid = Cid+1;
Scost = 1;
}
}
Answer by whydoidoit · Feb 20, 2014 at 05:11 AM
You could create a class based of ScriptableObject which allows you to create "assets" in the project for the item. On each asset you can then use the inspector to set values.
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Card : ScriptableObject {
#if UNITY_EDITOR
[MenuItem("Window/Create Card")]
public static void CreateCardMenuItem() {
var instance = ScriptableObject.CreateInstance<Card>();
var filename = AssetDatabase.GenerateUniqueAssetPath("Assets/Card.asset");
AssetDatabase.CreateAsset(instance, filename);
}
#endif
private int _cid; //Card ID
private string _ctype; //Card type
private int _scost; //Supply cost
//public GameObject celticPrefs;
//Setting up initial values for the above variables.
public Card()
{
_cid = 0;
_ctype = "Empty";
_scost = 0;
}
//Setters a getters
#region
public int Cid
{
get{return _cid;}
set{_cid = value;}
}
public string Ctype
{
get{return _ctype;}
set{_ctype = value;}
}
public int Scost
{
get{return _scost;}
set{_scost = value;}
}
#endregion
}
You could put these scriptable objects into an array or any other form of drag and drop reference and they behave like a normal project item. You have a menu item in the Window menu to make a new one - often you will just Duplicate the first one you make and change the values.
Your answer
Follow this Question
Related Questions
PlayerPrefs save after quit? 2 Answers
Is PlayerPrefs data persisted after app upgrade from ios AppStore? 1 Answer
Saving public variable data set in inspector? 1 Answer
Pretty complicated 0 Answers
is it safe to save info in PlayerPrefs? 2 Answers