# c++, generic character, class character

## Recommended Posts

dziki0roman    122
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 on other sites
EmpyrealHell    125
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 on other sites
dziki0roman    122
thanks a lot. ill try to do this again :)

##### Share on other sites
Captain P    1092
Quote:
 Original post by dziki0romanimagine 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 charactersuch 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 on other sites
dziki0roman    122
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 on other sites
dziki0roman    122
i've found great example what i want to get in angband. unfortunately, its all made in c and its uses predefined files with character classes, races and so on.
any clues how to do it in "c plus plus" or where to find simmilar example?

##### Share on other sites
TriggerB    128
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 on other sites
loufoque    93
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).