#### Archived

This topic is now archived and is closed to further replies.

# A little help on an efficiency question

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

## Recommended Posts

Continuing work on my Text Based RPG: I''m going to cut out a lot of what I''ve been doing that applies to this, as I think basics will illustrate my problem pretty easy. I made my attributes into a class, so that both the player and any monsters can have attributes. Instead of making an accessor and setting function for each attribute (I.E. Attributes.GetStrength() and Attributes.SetStrength(int str), for each stat), i created one set of functions, and pass which stat i''m using. I.E. psuedocode: #define STRENGTH 1 #define DEXTERITY 2 Attributes.GetAttrib(int attrib){ switch(attrib){ case 1: //set the str variable break; case 2: //set the dex variable break; } } //get my str: cout << Attributes.GetAttrib(STRENGTH); The idea seems sound to me. Instead of having 10 functions for 5 stats, i now have 2 functions, and pass which stat. However, In order to figure out which stat i''m accessing at any given moment, I have to have the switch statement, which seems a little overkill, and making me wonder if perhaps 10 functions aren''t better, or if i''m just clueless about a better way. I plan on doing something similar with skills, and if i have, say, 20 skills, i''d rather not have 40 functions, but a long switch statement doesn''t seem much better. Is there a better way someone can show me? Or is the way i''m doing it pretty good? _________________________________ Current Project: Text Based RPG Currently learning C++. Familiar with ASP, VBScript, VB and javascript

##### Share on other sites
You could store all of the character attributes in an array and reference the index into the array instead of using a switch every time.

#define STRENGTH 1
#define DEXTERITY 2

int attributes[2];

// Now attributes[0] can be strength and attributes[1] can dexterity

Attributes.GetAttribute(int Attribute) { return attributes[Attribute]; }

I should probably be working now...

##### Share on other sites
stu_pb''s way is better in my opinion... Arrays are a nice little feature for stuff like that, I am pretty partial to using them whenever I can. You could also use enum''s though...

THIS book rocks.

3D gaming Haven Opening soon.

##### Share on other sites
Yeah, that's what I was going to suggest. Simply use an array like that, and use an index.

    #define MAX_ATTRIBUTES 2#define STRENGTH 0#define DEXTERITY 1class Attribute_C{private: long Attributes[MAX_ATTRIBUTES];public: Attribute_C(void) {//Initialize everything to 0  for (int i=0;i<MAX_ATTRIBUTES;++i)   Attributes[i]=0; } long Get(int which) {  return Attributes[which]; }; void Set(int which, long value) {  Attributes[which] = value; };};Attribute_C Attributes;Attributes.Set(DEXTERITY,15);Attributes.Set(STRENGTH,15);long tmp;tmp = Attributes.Get(STRENGTH);tmp = Attributes.Get(DEXTERITY);

That is a very simple class to do what you are talking about, without the need for an if statement. Also, in code, always use the NAME and not the number, this way if you change the define at any point in time, you don't break any thing .

--- Edit ---
Added my source tags so it'd format properly.

[edited by - Ready4Dis on April 28, 2003 5:22:24 PM]

##### Share on other sites

Obviously. Hah. Dunno why i was bothering to try and keep them in their own individual variables, when an array makes so much more sense.

Thanks for the feedback. Worked flawlessly, and is easier on the eyes.

_________________________________
Current Project: Text Based RPG

Currently learning C++.
Familiar with ASP, VBScript, VB and javascript

##### Share on other sites
ready4dis, long is the same as int on a 32-bit compiler.