- Home /
Noob question about best practice in managing game data
Hi, I'm new to Unity, and I'm planning to make a game that contains a lot of characters with lots of information on each. So, I would like to know what is the best practice to manage the characters data? Should I turn each of them into specific objects and take them scene to scne with the DontDestroyOnLoad method? Should I keep a single object with all the characters' data in the game and use it to instantiate a prefab when a need them? Should I keep saving the data in the hard drive and load it in the generic prefab of personages when I need them? Or some other technique?
I'm not sure, because I see problems in all the technique I've listed above.
1) The first one seems to me to not represent the idea of game model in Unity 2) The second seems to not represent the basic idea of the technique of object orientation, which says that each object must take care of itself. 3) The third one seems to me that it would make a bad optimized game. And moreover it would make my life very difficult, because in some moments of the code I would need to compare data of all the characters at the same time.
Anyway, does anyone have a suggestion?
@Imp$$anonymous$$ontezuma Have you looked into ScriptableObjects? This is a very handy tool. A quick search on google will get you a Unity Api and $$anonymous$$anual links on the first 2 links.
Answer by pigaroos · Feb 17, 2019 at 09:02 PM
Im pretty sure you can make a preload scene, and store all your information there. Check this out and see if it is what you are looking for: https://stackoverflow.com/questions/35890932/unity-game-manager-script-works-only-one-time/35891919#35891919
This is an interesting technique. But unfortunately it doesn't solve all my problems ^^". $$anonymous$$ainly because I was thinking of working with asset bundle to add new characters to my game as $$anonymous$$ods. And as far as I know, the way to do this is to add them to the game as prefabs.
And if you read the last message, forget about that, I was thing about a instance of a object inside a prefab (probably impossible to do, I know) So... I guess I need to think more about this problem ^^ "
Anyway. I think the solution 1) is out of the question, so I go for solution 2, which is actually very similar to the one you recommended. So thank you, This topic gave me an idea of how in general people solves this kind of problem in Unity.
Answer by Cornelis-de-Jager · Feb 17, 2019 at 09:32 PM
@ImpMontezuma None of the above! Infact the best way I found is to simply store them in folders inside your assets. "But wiat! How can that be? You can't access your assets IN GAME!!" ~ But thats where you are wrong! Simply pop them into the Resource folder and Bobs your aunt (can't assume gender these days)! All you need to do is Resources.Load<GameObject> ("My Path")
or Resources.LoadAll<GameObject>("My Path")
and you can have it accessible from any script. BOOM - Link to doco!
But now you must be thinking: "FOOL, the property data of the characters change and needs to be retianed between sessions!" Well fear not! Your C#/Unity mastery is but a few lines of code away - Introducing... JSON.
In-fact, JSON is so good to use the Unity Peeps made a step by step tutorial on how to do it RIIIIIIIIIIGHT here.
May the odds be ever in your favor.
Well, in fact that tecnique is exactly was I saying about "keep saving the data in the hard drive and load it in the generic prefab of the characters when I need them"... And worse, after thinking hard about it, this technique doesn't solve the problem itself, because I would still have to use one of the other two technique to work with this deserialized json data anyway :( Hahaha I didn't think such a simple problem would create such confusion in my $$anonymous$$d ^^ " That's because, as I said, at some point in the code, I would have to compare the date of all the characters at the same time. But thank you for your answer.