Skip to main content
gamedev.net
🔒 Locked

Character Class

Started by Protocol 0 3 replies 2.1k views
Original Post
Protocol 0
Protocol 0
Say I have a character. e.g. limbs, head, body etc. Is it a good idea to store him in a class under a function called "drawCharacter"? Then create a function called "moveCharacter" which moves the player, under the public x, y, z values. i.e. OOP. e.g

class mainCharacter
{
public:
	mainCharacter();

	GLfloat moveX;
	GLfloat moveY;
	GLfloat moveZ;

	//////////////////////////////////////////////////////////////////////

	int drawCharacter ();
	int moveCharacter();
};
Okay, there are realy a few more functions and variables than just this in my class, but this will give you an idea of what I mean. I have already created it, and it works, though is there a more efficient way? How do the more experience coders do it? Even if you think you're not experienced, I wouldn't mind knowing (for curiosity's sake)? ... I'm just wondering.
"My Knowledge Has Served Me Right. Unless It Is Serving Me Wrong".
paulecoyote
paulecoyote
You sound pretty keen to get into this OO stuff.

How you orginise your character depends on your game. You've indentified "character" as a concept, and given intuitive sounding data members.

I'd suggest having a base class all your characters derive from, that way you can just keep them in a big list or structure somewhere, and just run "draw" on each of them per frame, and using virtual and that kind of stuff it will automatically call the most derived version of that.

Eg (assuming Cop, Robber and Dame are all derived from base class Character)

Characters[] character = new Characters[4];
character[0] = new Cop();
character[1] = new Robber();
character[2] = new Dame();

...
...
...

for (i=0; i{
character.draw( ...stuff... );
}
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
kburkhart84
kburkhart84
C++(OOP like classes) in reality can be just as fast as plain C, as long as stupid mistakes are not made(not intended to offend or say that one has been made). For example, if you only can have one of a character, only have one instance in memory(a singleton class). As long as certain care is taken in the design, classes are great for organization purposes. I do tend to use them, maybe even too much. I'm no expert though.


johnb003
johnb003
it really does depend on your game engine... This is pretty similar to how I do it I suppose. My engine has a draw function an update function and an event handler for the most part. so then a character would have an update and draw function, and they would be called from the engine in a simple example... In my update I just directly set the position of the character, as opposed to having a move function to simply set the values. You can take a look at a simple example I have included with my skeletal animation library. Which btw might also be a good way to do the animation for your character if you haven't considered that yet.

Protocol 0
Protocol 0
Okay, thanks.

At the moment, I have a main character class, and then I have an enemy class - which all other enemies inherit from - saving time and size by not having to create a new class for each object.

It seems to be working good - though was unsure if I was taking the wrong/right route to game development. I know that there is no wrong/right in game development - but there are practices which develop efficiency.
"My Knowledge Has Served Me Right. Unless It Is Serving Me Wrong".

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.