So I am writing an item system for unity C#, and I have this base item class that I extend upon for different types of items, for example I would extend the base class and add a variable for attack and attack speed, and a function for attacking for a weapon class.
But my issue is I am writing a database for the items, and I want to store these different classes into one big list, but when I make a list of Base Items, and I pass a weapon item, it cleaves away the attack function and the attack variables, and basically turns the weapon item into a base item, rendering it useless.

- 33
- 5
-
If you make the list as a List
you don't know for certain that the item will be a weapon. You'd need to retrieve the item from the list, cast it to weapon, then you'll have access to all of your weapon functions. – Dave Greilach Aug 15 '16 at 17:47 -
If I store a weapon in the list of List
and will it still store the other functions and such? – Teh Cosmic Sloth Aug 16 '16 at 15:24 -
I just that for a bow item, and tried to access the attack damage that I know is public after, and after doing this: Debug.Log((BowItem)(itemDatabase[ind]).itemAttack); It said 'BaseItem' does not contain a definition for 'projectile' and no extension method 'projectile' accepting a first argument of type 'BaseItem' could be found (are you missing a using directive or an assembly reference?) – Teh Cosmic Sloth Aug 16 '16 at 15:30
-
You need to rearrange your brackets slightly: `Debug.Log(((BowItem)itemDatabase[ind]).itemAttack);` Note the brackets are around `(BowItem)itemDatabase[ind]`, rather than just `itemDatabase[ind]` – RoadieRich Aug 25 '16 at 14:36
-
THANK YOU, I still have to test it fully, but so far it seems that it will work!!! THX! – Teh Cosmic Sloth Aug 30 '16 at 00:56
-
IT WORKS!!!! THANK YOU THIS TOOK HOURS!!! – Teh Cosmic Sloth Aug 31 '16 at 00:04
3 Answers
There are two main ways of doing this in a relational database, which is what most common database systems are.
You can have different tables for each type of Item, so
Weapon
s have one table,Potion
s have another, and theItem
table is a list of(Name, Type, Id)
tuples, telling you which item in which table to look at to get its exact details. This will require one table for each class of item, but if you are careful, you only need a few.Have an
Item
table, which stores an Item Type (again). You also have an "Attribute" table, which has columnsItemId, Attribute, Value
So if you have a sword with speed 5 and damage 10, and a potion that heals 3, you would have the following rowsItem ==== Id | Name | Category 1 | "Sword" | "weapon" 2 | "Healing Potion" | "potion" Attribute ========= ItemId | Attribute | Value 1 | "speed" | 5 1 | "damage" | 10 2 | "healing" | 3
It's then up to you to join
on ItemId, and pivot
the rows into columns and convert into your item's class.
Or you can use a document-based database, as the other answers suggest.

- 6,330
- 3
- 35
- 52
-
This will work too... Just be mindful of the real world scale. If you have a bunch of tables with hundreds of millions of rows and thousands of concurrent connections, your SQL will fall over while a document store with 100M docs wouldn't even break a sweat on a fraction of the hardware. – SledgeHammer Aug 15 '16 at 22:34
-
I'm writing my database as a list, is there any way to keep it like that? – Teh Cosmic Sloth Aug 24 '16 at 22:29
-
@TehCosmicSloth You'd be better served by asking a new question, without referring to "Databases" at all - the word "database" has a specific meaning, and you want a different system. – RoadieRich Aug 25 '16 at 14:38
Sounds like you want to use a document store instead of a RDBS. RDBS requires all items in a table to be of the same type. The drawback is you end up with a bunch of NULL columns for the types that don't contain all the columns which makes everything hokey. A document store is schema-less, so you only have the attributes that pertain to the type.

- 7,338
- 6
- 41
- 86