Page 1 of 1

Templates With Inheritance 0.o...

Posted: Tue May 04, 2010 10:35 pm
by XianForce
So to give a little back drop on my problem, I'm currently (attempting) to make a SHMUP and I've just made a State Machine and base State class (each templates for the type of object that it's for... States are singletons... Most of this is chunks I got from the book Programming Game AI By Example).

So I have a Singleton template, just the standard Meyer's implementation.
I have the State Machine template which holds states of it's own template type, and lastly the State template.

So in my base enemy class I have the State Machine declared as:

Code: Select all

StateMachine<Enemy>* m_pStateMachine;
Now I make a basic enemy, a Grunt, and make a state for it:

Code: Select all

class Grunt_Idle : public State<Grunt>
Then I initialize the state machine like:

Code: Select all

m_pStateMachine = new StateMachine<Grunt>(this);
Error... Can't convert parameter 1 from StateMachine<Grunt>* to StateMachine<entity_type>*... I just assumed that would work because inheritance established that "A Grunt IS AN Enemy". So when that failed, I initialized the StateMachine like this:

Code: Select all

m_pStateMachine = new Statemachine<Enemy>(this);
Alright, that works, but now I get an error when I try to set the state:

Code: Select all

m_pStateMachine->SetCurrentState(Singleton<Grunt_Idle>::GetInstance());
Another error... Can't convert parameter 1 from Grunt_Idle* to State<entity_type>*... Again, I assumed that because a Grunt is an enemy, it would work...

Here's the declaration of Grunt_Idle:

Code: Select all

class Grunt_Idle : public State<Grunt>
So I could probably just chain everything up to the base Enemy class, but my problem is, what if I need the state to access sub class specific data? So... I'm sure there's a way to accomplish it, Thanks in advance for any help!

Re: Templates With Inheritance 0.o...

Posted: Wed May 05, 2010 4:56 am
by K-Bal
A grunt is an enemy, but a State<grunt> is not a State<enemy> ;)

Sorry, I don't have a quick fix for you and I'm in a hurry. Good luck!

Re: Templates With Inheritance 0.o...

Posted: Wed May 05, 2010 5:47 am
by Live-Dimension
I know this isn't going to really help with your current issue, but templates with inheritance and singletons? Sounds a tad too complex for a state machine. Does KISS apply? (Keep It Simple Stuipd)

Singletons are generally frowned upon as well.

Anyway, you obviously cant convert X to Y without a helper function. As much as Grunt is related to Enemy, there are still differences. For example, the enemy template may have a basic attack with a claw. The Grunt then inherits them but you add a few more properties so you can change the grunt attack. Now you try to convert the grunt back into an Enemy, but what does the compiler do with those extra properties? It can't keep them as it'd no longer be an Enemy, but it can't get arid of them on its own because that could cause a lot of errors.

Re: Templates With Inheritance 0.o...

Posted: Wed May 05, 2010 9:32 am
by XianForce
Live-Dimension wrote:I know this isn't going to really help with your current issue, but templates with inheritance and singletons? Sounds a tad too complex for a state machine. Does KISS apply? (Keep It Simple Stuipd)

Singletons are generally frowned upon as well.
Yeah, I got the whole states as singletons thing from Programming Game AI by Example... Maybe that's not such a good idea hahaha
K-Bal wrote: A grunt is an enemy, but a State<grunt> is not a State<enemy>
Makes sense =D


----------

Well, since this design seems much too complicated, does anyone have a guide/tutorial on simple AI to recommend?

Re: Templates With Inheritance 0.o...

Posted: Wed May 05, 2010 4:50 pm
by eatcomics
XianForce wrote:
Live-Dimension wrote:I know this isn't going to really help with your current issue, but templates with inheritance and singletons? Sounds a tad too complex for a state machine. Does KISS apply? (Keep It Simple Stuipd)

Singletons are generally frowned upon as well.
Yeah, I got the whole states as singletons thing from Programming Game AI by Example... Maybe that's not such a good idea hahaha
K-Bal wrote: A grunt is an enemy, but a State<grunt> is not a State<enemy>
Makes sense =D

----------

Well, since this design seems much too complicated, does anyone have a guide/tutorial on simple AI to recommend?
Indeed because I will soon be doing a lot of AI programming, between my current project, and my side project (which I've barely started)

Re: Templates With Inheritance 0.o...

Posted: Thu May 06, 2010 4:47 pm
by XianForce
Haha it took me so long to realize where your text in this post was ^^

Re: Templates With Inheritance 0.o...

Posted: Thu May 06, 2010 5:07 pm
by eatcomics
I was just looking at it like WTF? but then I realized, I've done this twice in the last couple of days, but last time I fixed it...

Fixed

Re: Templates With Inheritance 0.o...

Posted: Thu May 06, 2010 5:09 pm
by XianForce
eatcomics wrote:
Fixed
almost, but not quite hahaha

Re: Templates With Inheritance 0.o...

Posted: Thu May 06, 2010 5:10 pm
by eatcomics
XianForce wrote:
eatcomics wrote:
Fixed
almost, but not quite hahaha
Yeah it is, wasn't done fixing it, I was like crap, then I was like, well typing fixed takes less time so I'll do that real quick then change it

Re: Templates With Inheritance 0.o...

Posted: Thu May 06, 2010 5:15 pm
by XianForce
eatcomics wrote:
XianForce wrote:
eatcomics wrote:
Fixed
almost, but not quite hahaha
Yeah it is, wasn't done fixing it, I was like crap, then I was like, well typing fixed takes less time so I'll do that real quick then change it
haha