Sign in to follow this  
dziki0roman

c++, generic character, class character

Recommended Posts

imagine this... there is a character (animal, humen or something else). each character has few atributes(height, width and so on) but most important each character has its class. each class modifies atributes of character such a situation you can see in WoW, Baldurs Gate (and probobly many other games). because im still learnig programming, as a lesson i'd like to make small character generator but i dont know how to add class(and changes done by this class) to this char. im thinking of putting definition of each class in somekind of file (xml structure is good for it, but its not important now). any help will be very welcome

Share this post


Link to post
Share on other sites
Well, the first part of what you're talking about sounds a lot like inheritance. Basically you make a basic class that has all of the properties, member functions etc that EVERY class will have. Then you can make classes to add onto or modify those settings. The final example on this page shows inheritance being used.

As far as loading them from a file though, the only way I can see that working is to make a couple classes. First a class that was your character, that contained whatever constants you have among all the classes. You'd also have a collection of objects from a second property class. The property class would have a name, value, whatever else you want it to have. Then you can check a value for your character by looping through the list of properties that it has until you find it.

As a side note, if all you want is a list of properties, and the properties only need a name and a value, you could probably pretty easily just make a vector or other simple collection with a bunch of std::Maps in it. The drawback there is all of the values would need to be of the same type (ie. all ints, all chars).

Share this post


Link to post
Share on other sites
Quote:
Original post by dziki0roman
imagine this...
there is a character (animal, humen or something else).
each character has few atributes(height, width and so on) but most important each character has its class.
each class modifies atributes of character
such a situation you can see in WoW, Baldurs Gate (and probobly many other games).

How does this 'class' modify attributes? Are the behaviors of these different character types different? Or do they only contain different values?

If the differences are purely the values of their height, width, etc. then writing a single class should suffice - you simply create several instances, filled with different values, based on your definition file (xml or whatever).

Quote:
because im still learnig programming, as a lesson i'd like to make small character generator but i dont know how to add class(and changes done by this class) to this char.

What exactly do you mean with 'generating'? Do you want to write some character data to screen (or to a file)? Or create character objects for use in an actual game? What approach is best for you depends a lot on how you plan to use these characters.

Share this post


Link to post
Share on other sites
this atributes are mainly numbers and they contain different values.
>How does this 'class' modify attributes? Are the behaviors of these different >character types different? Or do they only contain different values?

it looks like you've wrote. but i dont know why it didnt work. i'll try work on it again.
>If the differences are purely the values of their height, width, etc. then >writing a single class should suffice - you simply create several instances, >filled with different values, based on your definition file (xml or whatever).

i'd like to create character objects to use it later. but so far i wasn't able to see changes done by class to any character.
>What exactly do you mean with 'generating'? Do you want to write some >character data to screen (or to a file)? Or create character objects for use >in an actual game? What approach is best for you depends a lot on how you plan >to use these characters.

Share this post


Link to post
Share on other sites
If the behavior of the characters is the same but they contain different data, you only need 1 class.


class Character{

public:

Character();

int GetHeight();
int SetHeight(int value);
int GetWeight();
int SetWeight(int value);

int m_height;
int m_weight;

}



etc.

Then for a character generator (for example), you would create the object with the default constructor and attach the functions to sliders or some form of input on the generators interface.


Character person;

person.SetHeight(value);


Share this post


Link to post
Share on other sites
There is no reason to treat players and NPCs any differently.
The only thing different between animals and humans is the ability to communicate (talk, trade, etc.), even though it should still be possible for some kind of person affiliated with nature or something like that.

So really, I see no reason to be using inheritance.
It's not like there is any kind of ad-hoc polymorphism going on (specifying different implementations of virtual functions depending on the derived type).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this