1
\$\begingroup\$

I intend to use a level-entity-components hierarchy in my C++ game engine where a "level" is just a container for entities. However, since I'd like to setup all the levels at once (and thus all entities and components) I've stumbled upon a memory management dilemma: since only one level can be run at a time I need somehow to delay an initialization of other level's entities. How can I accomplish that?

I've considered a solution where all the levels derive from some Level base class and are initialized in the constructor when needed, but that would require creating a class with just the constructor in it for each level.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why would you "set up all levels at once"? Why can't you initialize a level when it is actually run? \$\endgroup\$
    – Philipp
    May 27, 2016 at 22:57
  • 1
    \$\begingroup\$ Hard-coding assests into engine is a terrible idea, even more so if using inheritance to do that. \$\endgroup\$
    – wondra
    May 27, 2016 at 23:16

1 Answer 1

1
\$\begingroup\$

I need somehow to delay an initialization of other level's entities

Why not add an init() and a terminate() methods to your Level class that does just that? init() populates your level when you're ready to play it, and terminate() cleans everything up when you're done with it.

You might want to consider changing your architecture to have Level created only when you need it: with that, you put the initialization code in the constructor, and the termination in the destructor. This would be more in line with RAII's principles.

\$\endgroup\$
1
  • \$\begingroup\$ The first paragraph made me cringe, the second one relieved me. \$\endgroup\$
    – Quentin
    May 30, 2016 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .