Archived

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

Mortanis

A little help on an efficiency question

Recommended Posts

Mortanis    122
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 this post


Link to post
Share on other sites
stu_pb    138
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 this post


Link to post
Share on other sites
DariusX    122
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 this post


Link to post
Share on other sites
Ready4Dis    180
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 1

class 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 this post


Link to post
Share on other sites
Mortanis    122
/me smacks his head.

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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
ready4dis, long is the same as int on a 32-bit compiler.

Share this post


Link to post
Share on other sites