SFML community forums

Help => General => Topic started by: phoekz on April 12, 2011, 03:38:26 pm

Title: Game engine design, proper class instances handling
Post by: phoekz 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 :).
Title: Game engine design, proper class instances handling
Post by: trojanfoe 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.
Title: Game engine design, proper class instances handling
Post by: Groogy 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.
Title: Game engine design, proper class instances handling
Post by: phoekz 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.
Title: Game engine design, proper class instances handling
Post by: Groogy 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.
Title: Game engine design, proper class instances handling
Post by: Nexus 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:
Title: Game engine design, proper class instances handling
Post by: phoekz 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.