Templates With Inheritance 0.o...

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
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Templates With Inheritance 0.o...

Post 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!
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Templates With Inheritance 0.o...

Post 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!
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Templates With Inheritance 0.o...

Post 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.
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Templates With Inheritance 0.o...

Post 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?
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Templates With Inheritance 0.o...

Post 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)
Last edited by eatcomics on Thu May 06, 2010 5:09 pm, edited 2 times in total.
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Templates With Inheritance 0.o...

Post by XianForce »

Haha it took me so long to realize where your text in this post was ^^
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Templates With Inheritance 0.o...

Post 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
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Templates With Inheritance 0.o...

Post by XianForce »

eatcomics wrote:
Fixed
almost, but not quite hahaha
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Templates With Inheritance 0.o...

Post 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
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Templates With Inheritance 0.o...

Post 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
Post Reply