Game Objects

Dec 23, 2015 at 11:32pm
I am struggling to understand inheritance. In the project I am working on I would to have a class called Game and then sub classes of Game that are my player ect. So when I load all my game objects into a vector in my render engine it calls the individual draw functions for each sub class. How would I do this exactly?
Dec 23, 2015 at 11:37pm
Dec 23, 2015 at 11:44pm
I would to have a class called Game and then sub classes of Game that are my player ect

That makes no sense at all to me. In what way is your player, in the game, a kind of game? That makes no sense and if you build something that makes no sense you're going to do yourself no favours at all. When you inherit, you're saying that the sub class IS a kind of base class. if you inherit player from game, you're saying the player IS a kind of game. The code doesn't care and if you mash it into compiling, it will happily compile, but you'll have created something really difficult to think about.

So when I load all my game objects into a vector in my render engine it calls the individual draw functions for each sub class. How would I do this exactly?


If you have a big vector of pointers-to-objects, you can iterate over the vector and call each object's draw function.

vector<drawable_objects*> everything_that_needs_drawing;

and later, once it's populated

1
2
3
4
for (auto object_pointer : everything_that_needs_drawing)
{
  object_pointer->draw();
}
Dec 23, 2015 at 11:53pm
Thanks for the link intergralfx.

I've over-complicated it to much, I don't quite understand inheritance. So how would I have a vector of drawable objects. What is drawable_objects*

Also for (auto object_pointer : everything_that_needs_drawing) is the same as for (unsinged int i = 0; i < everything_that_needs_drawing.size(); i++)?
Last edited on Dec 23, 2015 at 11:54pm
Dec 24, 2015 at 3:39am
 
for (auto object_pointer : everything_that_needs_drawing)

is known as a range based for loop, since C++11. It's a much simpler and cleaner way of doing a for loop, iterating through every object in a container.

http://en.cppreference.com/w/cpp/language/range-for
Last edited on Dec 24, 2015 at 3:40am
Dec 24, 2015 at 12:37pm
Okay thank you :)
Topic archived. No new replies allowed.