Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Game engine design, proper class instances handling  (Read 5108 times)

0 Members and 1 Guest are viewing this topic.

phoekz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Game engine design, proper class instances handling
« on: April 12, 2011, 03:38:26 pm »
Hey,

I'm creating basic game from scratch using C++, SFML & Box2D. What I like to know is, how do you properly summon new object (like an instance of a class), how do you store it and how do you delete it from both game and memory?

I have tried having something like ObjectHandler which sub-class Object inherits from. ObjectHandler creates new instances of Object using "new" and adds them immediately to Vector. This obviously works when you add stuff in, but when it comes to destroying them, it starts getting really confusing. Basically I have no way of controlling those objects ever again if I just go like "Object * Box = new Object" all the time, so I'm looking for better solution.

Thank you in advance :).

trojanfoe

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Game engine design, proper class instances handling
« Reply #1 on: April 12, 2011, 03:46:41 pm »
Your current solution is fine - it just sounds like you need a bit of practice using vectors.  This is worth the effort as you will have to use them, or a similar collection, at some point so it's best to get to know them.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Game engine design, proper class instances handling
« Reply #2 on: April 12, 2011, 04:38:15 pm »
It's obvious you are not yet comfortable with C++.
Learn the language a bit more before you try to create a graphical game.

First and foremost, avoid using pointers and new. It's enough just having a vector like this:
Code: [Select]
std::vector<Object> objects;

Second you can remove them by providing an index like this:
Code: [Select]
objects.erase( objects.begin() + index );

This index can be seen as an ID for each object.

Also, you had objects inherit from ObjectHandler and ObjectHandler creates new objects? That's not where you want to use inheritance. Object should be it's own base-class.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

phoekz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Game engine design, proper class instances handling
« Reply #3 on: April 12, 2011, 05:13:14 pm »
Quote from: "Groogy"
It's obvious you are not yet comfortable with C++.
Learn the language a bit more before you try to create a graphical game.

First and foremost, avoid using pointers and new. It's enough just having a vector like this:
Code: [Select]
std::vector<Object> objects;

Second you can remove them by providing an index like this:
Code: [Select]
objects.erase( objects.begin() + index );

This index can be seen as an ID for each object.

Also, you had objects inherit from ObjectHandler and ObjectHandler creates new objects? That's not where you want to use inheritance. Object should be it's own base-class.


Yeah I'm not definitely comfortable with C++ yet since I have been doing this for only for a month.

I'm aware of erase, but it doesn't free much memory at all, because my objects have pointers inside of them. Perhaps I can just remove them for now and just pass them as references. I'm just scared of memory leaks and stuff like that, so I thought I should improve my design. Perhaps I'm just trying to over-complicate it too much...

About inheritance, I thought it might be good idea to have inheritance because ObjectHandler holds a pointer to Game class, which has b2World and RenderWindow etc. and I obviously need them for drawing and syncing, however when I think about what you suggested, you're absolutely right. It might be much simpler to just have Object as a base class.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Game engine design, proper class instances handling
« Reply #4 on: April 12, 2011, 05:18:31 pm »
Doing things overcomplicated is very common. If something seems to be hard to do, then rethink because your probably doing something wrong. When you think about it, most stuff are pretty easy.

Don't worry for anything like memory leaks or the like. If you don't use the "new" keyword, you will never have any memory leaks.

The same with pointers, if you avoid using pointers you won't encounter any problems with pointers. A recommended easy approach( but CPU consuming ) is that you pass around handles/ID's instead of pointers and a centralized system where you retrieve references to the instances that are associated with that handle/ID.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Game engine design, proper class instances handling
« Reply #5 on: April 12, 2011, 06:04:50 pm »
Quote from: "Groogy"
Don't worry for anything like memory leaks or the like. If you don't use the "new" keyword, you will never have any memory leaks.
I fully agree with this. A lot of people make their daily C++ life to their own hell because they always use the low-level features instead of the powerful abstractions C++ provides. For instance, new and delete is rarely necessary, and if it is, it can mostly be encapsulated.

A few hints for productive C++ code:
  • Use RAII and avoid manual memory management wherever possible.
  • Deal with std::string instead of char arrays, prefer STL containers over raw C arrays.
  • Don't believe any performance myths without questioning them and avoid premature optimization. This includes overuse of inline or register keywords, avoidance of virtual functions and "clever tricks" like x<<1 instead of 2*x. Be aware that the compiler is able to perform many optimizations better than you.
  • Don't believe any OOP myths like "global functions are not object-oriented", "use inheritance to avoid code duplication" or "programming in OOP means to put everything in classes".
  • Make use of classes to gather related data and functionality and of inheritance to create specialized types and object hierarchies.
  • Keep dependencies low and local, avoid global variables or singletons, hide implementation details. Different classes should know as few as possible about each other. Exchange information through a small interface.
  • Use C++ abstraction mechanisms and combine them. Examples are compile-time (templates, function and operator overloading, typedef, ADL) and runtime polymorphism (virtual functions).
  • Don't reinvent the wheel for functionality already available in the standard library (or maybe in Boost).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

phoekz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Game engine design, proper class instances handling
« Reply #6 on: April 12, 2011, 06:39:27 pm »
They don't say "Sometimes simplest solution is the best" for nothing, and it definitely applies right here. I got my stuff working immediately when I stopped using pointers and the new keyword.

Thanks for hints guys, I will most certainly try to make good use of them.

 

anything