0

I want to access the engine from inside my eventReceiver object. They are fellow members of the game class, but how do I reach it?

// game.h
class game
{
    public:
        game();
        3dEngine* engine;
        eventReceiver* receiver;
};

// eventReceiver.h
class eventReceiver 
{
    public:
        eventReceiver () {}
        virtual bool OnEvent (const SEvent& event)
        {
            ...
            case QUIT_BUTTON_PRESSED:
>>>             engine->quit();     // how to access engine from here??
                return true;
            ...
        }
};

Should I use 'this' ? I don't understand why receiver can't see the engine.

1
  • You have a lot of unnecessary stuffs in your post. Narrow it down. Close to downvote. Jul 29, 2012 at 21:55

3 Answers 3

1

Implement the class as a Singleton and write a getter for the engine property. Accessing code could then look like:

game::getInstance()->getEngine()->quit();

I would recommend you though, that you create a quite() method in the game class itself hiding implementation details and allowing you to handle overall application shutdown and not just of the 3dEngine:

game::getInstance()->quit();

If you dont want to implement the game class as singleton you could also pass a reference/pointer of a game object to the constructor of your event handler:

class CloseButtonHandler : public eventHandler {
    game& game;

public:

    CloseButtonHandler(game& game) : game(game) {
    }

    virtual bool OnEvent(const SEvent& event){
         ...
         game.getEngine()->quit();
    }
}
4
  • So the solution to access is making it a global? (Singleton is just a fancier name for global with the additional restriction that only one instance can be alive...) Jul 29, 2012 at 23:07
  • @David Rodriguez A game class looks like a global class to me, doesnt it? Besides that Im showing a way to access it in a "normal" pattern. I just hope this isnt getting another "Singleton - A Antipattern" discussion. Jul 29, 2012 at 23:16
  • It all depends on the context... I am quite sure that in Facebook, there is no single global Game. At any rate, even if there is a single game in the application, it need not be global (it could be created in main and passed around to the types that need to use it...) Globals are almost always worse than any other option that makes do without them, as they hide dependencies (what in your application depends on game?) and make the code much harder to reason about (why and where did my game state change? anywhere?) Jul 29, 2012 at 23:19
  • @DavidRodríguez-dribeas See, your opening a book here which doesnt fit into the limited comment space on stackoverflow. A Singleton is a commonly used pattern and i wouldnt know why not to propose it. Myself dont want to forward dozens of objects for configurations etc. Of course there are other ways. Just take a glance at EJBs and CDI. Im actually tired of fighting such abstract discussions without any substance. One just dont know the context of this question, so one has to guess and im still pretty sure that a singleton pattern fits quite good into this situation. Jul 29, 2012 at 23:42
0

The eventReceiver shouldn't know anything about the engine. That's a bad design. There are a few solutions. One reasonable solution is to derive game from eventReceiver since game can clearly receive events. You can then implement the game-specific OnEvent handler in game itself. From there you can call engine->quit.

5
  • No, I dont think one has to separate that much. The game class looks actually like a singleton; atleast it seems to be a class involved in the application cycle. Therefor it would be a design issue to put logic to react to a concrete button in such a class. This is totally wrong! Jul 29, 2012 at 22:13
  • How about I create them separately in main and just pass the receiver a pointer to myGame: game myGame; \n eventReceiver receiver (&myGame); ? Jul 29, 2012 at 22:15
  • @Paranaix: I would say that it's entirely reasonable to code the Quit functionality in class game. After all, you are trying to exit the game !
    – MSalters
    Jul 29, 2012 at 22:41
  • @MSalters Thats not my point. I would favor such a design. But implementing the actually event handler inside the game class is wrong. A game class is not an event handler. Jul 29, 2012 at 22:42
  • @oringe: A big part of the problem is that EventReceiver is a poorly chosen name, and that in turn makes reasoning about it more complex (different people can understand different things). Say that it was a Controller (that receives and acts on events), then most people would agree that having a reference to the controlled engine would make a lot of sense... In the same way, it is not clear what Game represents in your design. Jul 29, 2012 at 23:16
0

I don't know how elegant this design is, but it works.

I just separated the receiver from the game class and gave its constructor a pointer to the instance of myGame. (Thanks to Paranaix)

class eventReceiver {
    public:
        eventReceiver (game* gameInstance) : gamei(gameInstance)
        virtual bool OnEvent (...) 
            {...
            case QUIT_BUTTON_PRESSED:
                gamei.engine->quitGame();
            ...}
    private:
        game* gamei;
}

int main() {
    game myGame;
    eventReceiver receiver (&myGame);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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