Hello,
I have a set of items in a program, whose items' properties can all be defined from const values from a .h file of data. Items are ID-based, so a lot of the code revolving around items is ID-based also, like I could call Spawn(2, 5) which would spawn item of ID '2' in an amount of '5'.
It made sense to me to make item IDs defined in an enum, in an .h file somewhere.
For example:
1 2 3 4 5 6 7
|
enum
{
ITEM_ZERO = 0,
ITEM_ONE,
ITEM_TWO,
MAX_UNIQUE_ITEMS
};
|
This makes sense, because it's safer to call "Spawn(ITEM_TWO, 5)" than to call "Spawn(2, 5)" -- in case I ever decide to insert items into the enum, using the enum titles in function calls will never fail me.
Of course those IDs aren't very helpful, so what I actually have are IDs like this:
1 2 3 4 5 6 7
|
enum
{
ITEM_APPLE = 0,
ITEM_STEAK,
ITEM_WOOD,
MAX_UNIQUE_ITEMS
};
|
So all this is fine so far -- The problem arises when I decide to define item data/properties in a const array of structs, for example:
1 2 3 4 5 6 7 8
|
struct ItemData
{
char _name[20];
int _type;
float _weight;
bool _food;
char _etc;
};
|
I figured I could just make a const array of these, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
const ItemData ItemDataArray[MAX_UNIQUE_ITEMS] =
{
"Apple",
1,
0.1f,
true,
'a',
"Steak",
0,
0.42f,
true,
's',
"Wood",
7,
1.2f,
false,
'w'
}
|
The problem here is that for this const array, I have to manually keep track of my IDs from the previous enum, and ensure that my item properties in the array match up with my item IDs in my enum. Which is exactly the kind of thing I wanted to avoid by using IDs in an enum in the first place. If I decide to add a new item between item#56 and item#57, I have to find the proper spot in my data array and there's a huge amount of room for human error here. Yeah, I could comment in my array like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
const ItemData ItemDataArray[MAX_UNIQUE_ITEMS] =
{
// ITEM_APPLE ---------
"Apple",
1,
0.1f,
true,
'a',
// ITEM_STEAK ---------
"Steak",
0,
0.42f,
true,
's',
// ITEM_WOOD ----------
"Wood",
7,
1.2f,
false,
'w'
}
|
But this is still not great because once the list gets large, it's still prone to user error. Messing up one item will offset the whole thing.
My question is, is there any clever way I can set up my code so that I both define the initial enum value (i.e. starting at 0), AND the matching parameters for that item, in one const array? Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
const ItemData ItemDataArray[...] =
{
ITEM_APPLE, (value = '0' and can be used like a #define)
"Apple",
1,
0.1f,
true,
'a',
ITEM_STEAK, (value = '1' and can be used like a #define)
"Steak",
0,
0.42f,
true,
's',
ITEM_WOOD, (value = '2' and can be used like a #define)
"Wood",
7,
1.2f,
false,
'w'
}
|
I would need to know the array size beforehand, but that's not a big deal. My main goal is to have those integral values "set" and incrementing by 1, just like in an enum, so they can be used in function calls, and also as an index into the data array itself.
Is this possible? Or am I going about this in a poor way and there is a better way to do this?
Thank you : )