- Home /
Best practice to store and load a specific set of objects?
Hi all
Im wondering what the best way to store a specific set of objects always available to my game. Im making a card game and i want to be able to randomly generate a deck of 100 cards from some sort of stored list of predefined cards that will stick with the player for the entirety of the game. I want to be able to save the deck after changes are made to it and also have several different decks of cards for several different players.
How would i go about creating this dataset of individual cards that can be put into decks when the game is running?
Should i make an XML file containing information about each card?
Should i hardcode the cards into one of my Classes?
What would be the best practice approach to this?
Thanks in advance :)
(Edited the question to better reflect what i actually wanted to ask xD)
Just as an aside, if what you are really using is a standard deck of cards you still wouldn't need to permanently store their contents in a file. You know the 4 suits, you know each suit has 2-10, jack, queen, king, and an ace. You can easily automate the creation of each one of those cards and put them into a list at startup. Just my 2 cents, its really only applicable if youre using a system in which the deck you are using is ordered similarly to a standard 52 card deck. I feel like that's what I would do.
Yea perhaps my example was bad since it doesnt reflect the game im actually working on. the 52 card deck would contain random cards. Perhaps 5 of a kind or 2 of a kind. Updating the question now
Answer by MrVerdoux · Nov 27, 2013 at 05:17 PM
For small data (probably not your case) you can use player preferences. http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
I have also used xml for saving data, it doesn´t seem like a bad way to go, although I don´t consider myself an expert.
The best option (to me) is using SQlite (which I am pretty sure is available thanks to .NET). It´s without a doubt the fastest way organize your data, given that you know how to use it... I didn´t like it at first but I had to learn it in my job and now I can´t help but respect it.
Im a little concerned about the performance but i assume that it is actually quite good taking into considderation that it is basicly just an advanced form of serialization?
From what I know, queries are faster, I don´t know precisely how the overall behaves.
Answer by Owen-Reynolds · Nov 27, 2013 at 04:39 PM
A relatively simple "Unity" way is to make a "cardHolder" gameObject, with the cardHolderScript on it, then set it all in the Inspector (drag over all the textures, set values... .) For multiple scenes, make it a prefab.
The biggest risk is that it you can lose chunks of data if you, say, change array names (like renaming filled Texture2D[] CardFaces;
.
Technically, this is the "use a file" approach, since Unity is writing all of this data somewhere. Probably in a more compressed format than XML (or not) and not readable by you.
Im not sure its a good idea but i would prefer to keep this happening behind the scenes without using gameObjects or prefabs.
Answer by fred_gds · Nov 27, 2013 at 11:53 PM
Well I would create an Array of ints for each player. So the length of the array would be the number of cards and the number the int[| returns would be assigned to specific card.
So then you could just use PlayerPrefsX (http://wiki.unity3d.com/index.php?title=ArrayPrefs) with the command PlayerPrefsX.SetIntArray().
And then when you can load the deck later on again using PlayerPrefsX.GetIntArray() and just make the process you did to assign the cards backwards.
It's easy and you don't have to store a huge amount of data.
I also think that it is faster then xml serialization ;)
Im afraid the PlayerPrefs approach is not suitable to my project. Thanks alot for weighing in though :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
IsolatedStorageException: Could not find a part of the path 0 Answers
Making my XML file multiline 0 Answers
PlayerPrefs v. XML File for Saving Data? 1 Answer
Invalid Encoding Specification Xml 1 Answer