Getting data from an actor

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Getting data from an actor

Post by ismetteren »

The way i have my actor/gameobject system in my game working, is that the actors themself only stores some data (position, health, sprite and so on). They does not update themself nor does they draw themself. I have gamelogic and rendere classes to do that.

So i can pretty easily acces a actor from the actor vector. The problem is, that i am using polymorphisem(i am pretty sure that is spelled wrong) to store different kind of actors in the same vector. That means, if i want to access a value specific to a certain kind of actor, i can't do it!

I am pretty sure im not the only one who is structuring my program in this way. So, how do you solve this problem?
Image ImageImage Image
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Getting data from an actor

Post by Scoody »

Doesn't the baseclass contain the common functions for retrieving those values?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Getting data from an actor

Post by MarauderIIC »

You could cast them.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Getting data from an actor

Post by ismetteren »

Scoody wrote:Doesn't the baseclass contain the common functions for retrieving those values?
No, the base class does not have a getHealth, getScore, getPosition, get(everything that i ever might make an acotr that contained).

Cast. Can i cast from a baseclass to a child class, if i can, How? just a normal cast?
Image ImageImage Image
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Getting data from an actor

Post by Scoody »

This should do the trick:
childptr = dynamic_cast<child*>(baseptr)
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Getting data from an actor

Post by ismetteren »

Scoody wrote:This should do the trick:
childptr = dynamic_cast<child*>(baseptr)
Really?

Im gonna try that.

Thanks :D

.:Edit:.
i have read a little about it, and that dynamic_cast thing is really neat :D
Image ImageImage Image
Post Reply