- Home /
Arrays or database?
Hello everyone,
This is more a design question that asking for code. In my game (something like an RPG) I'm planning to have a large amount of items, weapons, armor and so on.
I've designed the item class, and I want the constructor to only take an ID to assign everything else, so i could call Weapon someWeapon = new Weapon(25) and assigns all the other class properties such as name or description from somwhere just by its ID. (Item is abstract and Weapon inherits from it, that's why I swapped names)
So my dilemma is wether to use a class holding all the needed data in Arrays which are initialised at the start of the game or to use some kind of database to hold all the needed values (Let's say, an XML database, or anything else you would recommend).
I don't have that low-level knowledge of how the OS works so I don't really know which is better, but I'd bet that the first one (the array method) is faster in execution time, but has a much higher memory usage than just loading a single Item whenever needed from a database.
I'm trying to keep everything as optimised as possible as I'm aiming into android platform.
Thank you very much!
Answer by MarkFinn · Dec 27, 2012 at 10:01 AM
For purposes of Optimization you will be able to squeeze much more performance out of simple class constructs like arrays (or even dictionaries, though there is a cost over arrays) than over a generic ease-of-use, control and security item such as a database plugin.
Long story short, databases will be easier to update, control, debug and monitor. Home grown simple data structures will be more fine-tune performance friendly.
You pays yer money and you makes your choices...
Ok, seems it is as I expected, I think i'll go on with the class constructs way of doing it.
Answer by whydoidoit · Dec 27, 2012 at 10:14 AM
While I agree with @MarkFinn I also think that this would also be a good use of a class derived from ScriptableObject - where you could effectively create assets out of your class and fill them in using the Inspector. ScriptableObject subclasses can be added to your project and can be referenced by scene and project items alike. I use this extensively for similar concepts to those you are creating. The benefit is that you have an inbuilt editor in the inspector and many ways to assign other project items that would be a lot of typing and resource lookups from code.
I think this might be a good idea, i'll definetly use it. Thank you very much
Yeah, its really powerful. But be aware that it is subject to Unity Serialization rules and hence Dictionaries will not be saved (the same as if it were a $$anonymous$$onoBehaviour). Arrays and Lists are fine as are subclasses that are marked [Serializable].
Answer by 1337GameDev · Feb 13, 2013 at 05:23 AM
I recommend hash tables. Or a linked hash map. I use this (the following site's implementation of linked hash map for c#) for prefab database creation and it seems to work fine. It works very similar to java's implementation of linked hash maps.
http://scottgarland.com/Post/Display/LinkedDictionary_for_C
You can store scripts (or strings of the values joined by a dilimitter and split later) and then retrieve these stored values later. The only downside to this is that working with strings (if done a lot in a frame) can cause performance slowdowns. If you only instantiate objects at start() of game, or every so often, it shouldn't be that big of a problem. Users also expect a game to take a little bit to load, so having a little slowdown (but not over 20 secs) at startup isn't going to cause people to dislike your game if playing it runs fine.
A linked hash map seems very logical here as you want to retrieve something by a key (which is very fast to hash and retrieve). Hope this helps steer you in a direction.