using objects in arrays

Started by
4 comments, last by nmi 18 years, 10 months ago
I'm looking for advice on using the least memory for switching game levels that are objects. If I put them in an array, I would be allocating memory for all the levels. I could use enum with a switch statement and allocate memory and then delete when it returns, but I don't know if that's the best way. There could be as many as 20 or 30 levels, maybe more. Anyway, any ideas would be appreciated. Language is c++, level will have a run() function.
Advertisement
Wouldn't each level be "described" externally in a file.

Whenever a new level began you would read the information, enemy types, number of enemies, terrain etc., from the file and create the appropriate game objects from it.

I don't think holding all information about each level in memory is the way to go, but I might be wrong.
Gary.Goodbye, and thanks for all the fish.
Quote:Wouldn't each level be "described" externally in a file.

Well, I was going to use inheritance from a sort of generic level class. Of course, some information about the level would be saved that changes, but I was going to create each object and then run it. Does sound a little strange, but I already started it that way.
Quote:Original post by fireside
Quote:Wouldn't each level be "described" externally in a file.

Well, I was going to use inheritance from a sort of generic level class. Of course, some information about the level would be saved that changes, but I was going to create each object and then run it. Does sound a little strange, but I already started it that way.


It does not sound entirely strange. There are several games out there, that I am sure that use that kind of level strategy, World of Warcraft for instance. When you get close to the next zone, it will start loading that zone from a file into memory and remove the zone that you were in previously when you are far enough away that it knows that you wont be coming back for a while.

I can actually see this being used in Super Mario Brothers on the NES. Complete Continous Scrolling from world 1-1 - Bowser in 8-4 based on proximity to the next world level. Not sure how the timer would work in SMB but I guess it could be like those racing games that once you reach a checkpoint you get more time added to the clock.

Of course, I have no idea what kind of game you are working on but I do not see anything entirely wrong with it.
inheritance for each different level sounds a bit foolish. Typically you create a single level object and change its data according to some kind of file.
You can use smart pointers and an LRU garbage collector, to remove unused data from memory, and load it on demand (via the smart pointer), if it is accessed.

This topic is closed to new replies.

Advertisement