• 11
• 27
• 9
• 20
• 31

# Using Lookup Tables

This topic is 2883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm programming a game in MSVC++10 and was wondering what would be the best method of implementing a constant lookup table with the data known at compile time. This table will hold information regarding skills used by characters (around 200 or so), for example:
ID		Name		Power	Type		Effect		MP
FIREBALL	"Fireball"	50	FIRE		BURN		12
LIGHTNING	"Lightning"	75	ELECTRIC	NOEFFECT	14
BLAZE		"Blaze"		100	FIRE		BURN		25
HYPNOTIZE	"Hypnotize"	0	TIME		SLEEP		10
...
(etc.)
The ID column holds a unique enum value I use throughout my code to refer to the specific skill. The name is a string representation of the skill, used for output. Power is an integer used for damage calculations. Effect is another enum value I use to refer to the effects a skill can generate; since some skills have the same effect, it is easier to have a seperate effect value rather then refering to the effect via the ID, in addition to giving me greater flexibility. Finally the MP column is an integer value used to refer to the skill's MP cost. The most important thing is that the implementation is fast. This is because the AI I'm using for my game simulates all possible move selections it can make every time it gets a turn, and uses the move that gives the highest possible chance of victory. Because the number of simulations grows exponentially, each one needs to execute very fast. So at every point while programming the game I am always trying to optimize for speed. The ID of the skill will always be known when retrieving information (in other words it will never be necessary to get all skill IDs that have the 'BURN' effect), so I would think storing it as an array of structs with the index being the ID of the skill would be the fastest implementation (memory usage isn't a big concern). The next most important thing is that the data is able to be easily viewed and edited. My current implementation is to use functions that have large switch statements within them and return a value based off the skill ID passed to it. But I am unsure of the speed of this, and would also prefer an implementation where I can view the data all in one concise table, like the one I posted above. If my previous guess that storing the skills as an array of structs was correct, then I guess my question is if there is a way to transform a table into this data structure at compile time in MSVC++10? I appreciate the help, thanks.

##### Share on other sites
A structure and an array fits your needs.

struct Skill {char *Name;int  Power;int  Type;int  Effect;int  MP;}struct Skill skills[] = { { "Fireball", 50, FIRE, BURN, 12 }, { "Lightning", 75, ELECTRIC, NOEFFECT, 14 },{ "Blaze", 100, FIRE, BURN, 25 }, { "Hypnotize", 0, TIME, SLEEP, 10 } };

Put that declaration into the relevant source file, use extern in a header file if you need to access it in other source files. Use enums for fire, burn, electric etc. Create the enum for HYPNOTIZE, BLAZE, LIGHTNING, FIREBALL etc. starting with 0 so that you can use the ID as the index into the array. If for some reason you need to start the enum with a number other than 0, subtract the starting number from each ID so that you can use the difference as the index into the array.

##### Share on other sites
What you have will work. It is not extensible, but it will work.

To allow it to grow, that file needs to be outside the game. It needs to be kept in data.

You can easily keep this sort of thing in a simple data file. Some people prefer "flat files", just simple raw data. Other people prefer to stick it in XML or something that can be read by data. Either is fine.