Good day all. I am trying to write a c++ rpg game, learning c++ as I go. As I understand it, the example below is a basic class, and I can access my variables with dot notation. Along with the class functions or methods if you wish. Thats all fine and good. I have the "stats" for over 250 monsters now, and will probably have around 325 by the time I'm finished. So I need to set this class up to handle that many monsters, each having their very own stats. Could someone please explain how this is done? Maybe a small example to help me get it started? Tips, pointers? I have watched some videos on "C++ tutorials" on youtube. They set up a class, and access it through main directly... I doubt this is how it is done in the real world of programming. In the real world, I would imagine you can access the class through other functions, that are called from main. So another question would be after you make an instance of your class in main, can you have other functions access it? Please pardon my swiss cheese education in C++. I would appreciate any wisdom. Thank you.

class monsters
{
public:
string mnam; // name
int level; // level
int ac; // monsters armor class rating if any
int gender; // monsters gender
int min; // minimum it will hit for
int max; // max it will hit for
int gold; // gold monster has
int hits; // hit points
int mexp; // experience reward
int weap; // weapon monster has if any
string mdesc; //description

private:

void monattack(){} // routine monster attacks you
void monrun(){}// moster can run if hurt bad enough
void monfol(){}// monster may follow if you run
void mondeal(){} // monster makes deal for it's life when hurt badly
void mondies(){} // awards gold and experience
};

Recommended Answers

All 3 Replies

yes you can acces the class as long as you pass it. a simple way to demonstrate this would be

bool AttackMonster(monsters * monster, int indexOfMonster);

int main()
{
	bool result = false;
	int index = 0;
	monsters monter[325];
	// load the array with all thge differnt monster types
	LoadMonsters(monster);
	// later on they get attacked so call attack function
	result = AttackMonster(monster, index)
	// do more stuff
	return 0;
}

hope I'm making sense here.

Thanks.

So by your line

monsters monter[325];

I take it, you read an array into the class just as you would a normal array, like so...

class monsters
{
public:
string mnam[325]; // name
int level[325]; // level
int ac[325]; // monsters armor class rating if any
int gender[325]; // monsters gender
int min[325]; // minimum it will hit for
int max[325]; // max it will

... and so on, and then maybe something like....

mnam[325]={"orc","skeleton warrior",blah blah","blah blah",...
level[325]={1,1,1,2,.....};
ac[325]={0,0,1,2,9,3...};
blah blah blah...
};

Is that about it? Am I on the right track?

i would go a different way. i would use the first code that you posted and have each monster be its own instance of the monster class. then i would either have a file with all the different monsters and load the array with that or just hard code it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.