[Solved] Calling a class function from another class

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

User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post by hurstshifter »

Bakkon wrote:
hurstshifter wrote:
I was think this too. I'd try letting the player class hold an array of bullet objects. Then create a fireBullet function for the player. Just grab the first bullet in the array that's currently bDead and shift its properties around. You'll have access to private player variables and then adjust the bullet with mutator functions in its own class.
I'll definitely give this a shot. But I'm still not content with the fact that calling this function from a separate class is not working.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post by hurstshifter »

And lo, on the 6th hour of coding there was triumph. Bullets flew over yonder with much speed and accuracy. And there was much rejoicing, yayyy.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: [Solved] Calling a class function from another class

Post by Innerscope »

Out of curiosity, did you end up changing your original design? Or did you stick with passing a player object?
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: [Solved] Calling a class function from another class

Post by hurstshifter »

Innerscope wrote:Out of curiosity, did you end up changing your original design? Or did you stick with passing a player object?

I went with the array of bullet objects within the player class. Definitely a better way to go with a simple game such as this. I then created a shoot() function in player that is passed a reference to one of the elements of that array of bullets. The bullet class is now simply an SDL_Rect and 2 integer variables. Next step, collision detection & block removal. Thanks so much for everyone's help.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Calling a class function from another class

Post by Bakkon »

hurstshifter wrote:And lo, on the 6th hour of coding there was triumph. Bullets flew over yonder with much speed and accuracy. And there was much rejoicing, yayyy.
Awesome, man. I'm currently working on a schump myself and I'm around the same stage as you. Always feels good to see stuff moving around the way it should. :)
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: [Solved] Calling a class function from another class

Post by Innerscope »

I went with the array of bullet objects within the player class. Definitely a better way to go with a simple game such as this.
Good move. Also I wouldn't consider this a hindrance to what you're learning. It's generally poor practice/design to call a function of a class from another class. Although, for that purpose you might want to look into delegation. (basically, using a contained instance of a class within a class, to do function calls)
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: [Solved] Calling a class function from another class

Post by K-Bal »

If Player was a singleton you could have declared player::getprect() as static. Not a good design, though.
Post Reply