• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by AronChan · Nov 27, 2013 at 04:29 PM · c#serializationxmldata storage

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)

Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image violence · Nov 27, 2013 at 05:02 PM 0
Share

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.

avatar image AronChan · Nov 27, 2013 at 05:05 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AronChan · Nov 27, 2013 at 05:27 PM 0
Share

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?

avatar image MrVerdoux · Nov 27, 2013 at 05:31 PM 0
Share

From what I know, queries are faster, I don´t know precisely how the overall behaves.

avatar image

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.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AronChan · Nov 27, 2013 at 04:52 PM 0
Share

Im not sure its a good idea but i would prefer to keep this happening behind the scenes without using gameObjects or prefabs.

avatar image

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 ;)

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AronChan · Nov 28, 2013 at 01:17 AM 0
Share

Im afraid the PlayerPrefs approach is not suitable to my project. Thanks alot for weighing in though :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Making my XML file multiline 0 Answers

IsolatedStorageException: Could not find a part of the path 0 Answers

XML parsing c# in unity 1 Answer

XML reading and writing on Windows IOT 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges