Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm hard-coding my levels and coding each level in its own class...The problem is that I need to have a pointer that points to the class of the current level. I don't know how I would go about doing this. I ideally want to have a vector that stores the levels, and I'm working on a level managing class to load, update, draw, and unload the levels in.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

If your level classes have a common Base class you can just create a pointers to it:

class Level {
    virtual void func1()
    //...
    virtual void funcN()
}

class Level1: public Level {
    virtual void func1()
    //...
    virtual void funcN()
}

class Level2: public Level {
    virtual void func1()
    //...
    virtual void funcN()
}

Vector<*Level> levels;
levels.append(new Level1())
//...
levels.append(new LevelN())
share|improve this answer
    
So, a pointer can point to inherited classes of its class? –  Adam Jan 15 '12 at 9:22
    
Sure. en.wikipedia.org/wiki/… –  OCyril Jan 15 '12 at 10:07
add comment

It would be a bad design to have separate classes for each of the level and will also duplicate your code. Why can't you change the level specific values before starting a new level (have them stored in as some constant or in text file).

share|improve this answer
    
I could do it that way with the way my code's going...But the reason I did it this way in the first place was because there were some problems in my game that I only figured out how to solve through putting each level in its own class. –  Adam Jan 15 '12 at 9:36
    
Yeah @Adam, saurabh.s is right. If you couldn't find a way to solve it with just one class, try asking a question on how to do that, people may help you :) If you find a way to do it with just one class, it might make your postwork a looooot easier. –  Gustavo Maciel Jan 15 '12 at 10:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.