Access class instance from event handler callback?

edited April 2012 in Help request
I have an Orx event handler callback as a static member function of a C++ class. I want that callback function to be able to access an instance of the class.

Right now I store a pointer to the class instance in a global so the callback can use it. Is there a way to get rid of the global, like sending an extra parameter to the callback function?

Comments

  • edited April 2012
    Hi acksys,

    Do you have only one instance of your class or many ?

    If you have only one, the good solution (for me) is to use a Singleton Pattern (it's just a static method that return the only one instance of a class, and this class need a private constructor). There's some example of that is Scroll, if you use it (search for GetInstance() methods).

    If I'm correct, you can pass an extra parameter when you send a event (payload), but I think it's not what you want because the sender of the event doesn't care about handlers, and your instance is specific to only one handler.

    So, Singleton solution if you only have one instance ;)
  • edited April 2012
    Right now, yes, there is only one class instance. So the Singleton is an acceptable solution for the moment. Thanks for suggesting it.

    I can imagine the situation would arise where I have to do this with more than once instance of a class, though. What would be the solution in that case?
  • edited April 2012
    Hmm,

    By supposing you want to access many instances from your handler, I think a Singleton on top of them could do the tricks. Usually, in my case, I use Singleton for "manager" object and I asj him for other data if needed.

    In that case, in your handler, you can retreive the manager using the GetInstance() methods, and just add some methods in your manager to retreive your instances.

    No ?

    Hope that help ! :)
  • edited April 2012
    As faistoiplaisir pointed out, the singleton pattern is working great there.

    Now if you have more than one class instance, it all depends on how the event is linked to it, isn't it? If it's an object instance class, you probably stored it with orxObject_SetUserData() and can probably get the concerned object from the event parameters.

    It's really on a per case basis but so far I only had to deal with "managers" via singletons or via instances linked somehow via the UserData pointer stored in the orxOBJECT.

    Do you have a more concrete example?
  • edited April 2012
    The class callback I'm dealing with handles physics events with game objects. Currently these events would only happen in one context. If I implemented two player competitive support today, for example, I'd theoretically have one instance of the class for each player.

    You're right, though.. sending the instance pointer in the orxOBJECT UserData would work just fine in that scenario.
  • edited April 2012
    That's actually what I used in Scroll, some events are handled internally: that allows me to call virtual methods on objects like ScrollObject::OnCollide(), ScrollObject::OnSeparate(), ScrollObject::OnNewAnim(), etc...

    And for you own custom events, you can of course stuff anything you want in the payload. :)
  • edited June 2012
    I ended up putting that global in a namespace instead. Since the global is only accessed in its .cpp file, that's all I had to do. Keeps everything else from touching it and works ok for me :)
  • edited June 2012
    I have a singleton StandAlone class,

    Then a GameObject class

    and derived from the GameObject are my objects.

    All these object classes (e.g. Player) are put into the orx objects as SetUserData pointers.

    The GameObject class has functions I need such as
    - Update
    and
    - Collision
    (Just using these 2 right now)

    On an event I just send them through to the userdata of the object in question.

    On 'update' I send every object with userdata the Update command.
  • edited June 2012
    That's what can be done if you need the object data, but I needed to call a function of a class instance.

    BTW, take a look at Scroll! My tutorials on setup are on the wiki. It makes it very convenient to implement event handler callbacks for game objects.
  • edited June 2012
    Yeah i'll have a look at scroll later, bit of knowledge about the base library is useful, i'm stubborn like that ;)
  • edited June 2012
    Agreed. Also, the Scroll tutorials assume you have all the knowledge from the Orx tutorials.
Sign In or Register to comment.