I've got a class, WeaponFrame
, that has an int member, weapon_frame_id
, that I intend to use as a way to identify the weapon, and I use that compare against other WeaponFrame
instances.
The problem is that I want to pull the weapon from the global list of WeaponFrames and equip it on an Actor
. As the designer, I might know that weapon_frame_id #2
is the bow weapon I want, but what is the best way to programmatically identify them?
If I assign an ID to the WeaponFrame
on instantiation, I am unable to rely on the value being identical the next time the game is run, unless I assign the value to an enum or int then. That might work though.
I'm using C++11, would I just create a namespace somewhere, and enumerate all the weapon types,
enum class WeaponFrameIDs {
Sword = 1,
Bow = 2, //repeated a hundred-plus times
};
or would I make a ton of const integers in a header somewhere, even if effectively an enum that way too:
namespace WeaponFrameIDs {
const int Sword = 1;
const int Bow = 2;
}
I could also use strings mapped to an integer, but I'm losing compile-time verification (and I can't use constexprs with VS2013), and run the risk of duplicating entries. However it supports dynamic assignment to the list:
std::unordered_map<std::string, int> WeaponFrameIDs {
{"Sword", 1},
{"Bow", 2},
};
Is there an alternative that I'm missing? What is the best way to write a system for identifying weapons in a game?
If it's relevant, here's an example situation I'm trying to solve via code. I am not using a visual editor where I can drag a weapon onto an actor.
void set_up_actor()
{
Actor actor;
WeaponFrame* weapon_frame = AllWeaponFrames.at(WeaponFrameIDs::Bow);
actor.weapon_frame = weapon_frame;
};
Points I'm looking to avoid:
- Filling an entire header with enums
- Needing to recompile the entire project because I changed that header
- Losing the ability to verify the id at compile time
- Eventually add user-generated ids. This conflicts a lot with the compile-time verification.
I'm trying to solve the best way to pull the Weapon from a list, whether it's by ID, index, specially mapped key or something else.
- If I use a literal ID,
actor->weapon_id = 1
, I don't know what weapon is tied to the ID of 1. - If I use an enum, or a const int, to represent the
1
, I need to recompile the game for each of the new weapons I add. - If I change the id type to a string, I lose the ability to programmatically validate the string against the existing list of weapons.