Associating events with C++ classes

edited October 2012 in Help request
I do have a specific problem to solve, but the question itself is rather a "best practices" type of thing.

I have a C++ class that wraps an orxOBJECT which has an animation, and a spawner. Creating an instance of this class creates an orxOBJECT, and sets it's user data as a pointer to the class.

This class has a static member variable (STL set) of instances of the class. The reason for tracking instances is for event handlers. It appears that event handlers cannot be defined on a per-object basis, only on a 'category-of-event' basis. My event handler is a static method of the class will check to see if the user data for the event recipient/sender matches an instance of the class, in which case it processes the event. If not, it "passes" on the event and lets a different handler deal with it.

This works just fine with animation events since the event recipient is the orxOBJECT, and looking up the user data is trivial.

For spawner events, the event recipient is the spawned object, while the event sender is the spawner object itself. There's no simple way of determining whether the spawner belongs to the object wrapped by my class, or if it's some other random spawner.

It's easy enough to write code to specifically look to see if the wrapped object has a "Spawner" attribute, and set that objects user data. However, I'm thinking it's really unlikely I'm the first person that has come across this problem, and I was wondering if there is a better way? I don't want to write boilerplate code for every subclass (obviously).

Most of what I've described above is encapsulated in a base class that only has virtual stub methods for specific events (e.g. animation end), but has generic code for event handlers (e.g. animation event handler). What I'd like to do is have the base class automagically inspect the created orxOBJECT, and add user data information to any child objects (other orxobjects, spawners, etc) so that class event handlers can quickly and accurately determine if they need to react to a particular event or pass it on.

Comments

  • edited October 2012
    epoulsen wrote:
    It's easy enough to write code to specifically look to see if the wrapped object has a "Spawner" attribute, and set that objects user data. However, I'm thinking it's really unlikely I'm the first person that has come across this problem, and I was wondering if there is a better way? I don't want to write boilerplate code for every subclass (obviously).
    Well, actually what you are doing with your wrapper has (mostly) already been done for you in Scroll. More on this later.

    If you want to know which object owns a spawner, you can do it like this:
    orxOBJECT *pstOwner = orxOBJECT(orxSpawner_GetOwner(pstSpawner));
    

    Another detail that can also help you with your tasks is that all orxSTRUCTURE-based structures (ie. orxOBJECT, orxSPAWNER, orxANIM, orxFX, etc...) are all referenced internally by orx and their collection can be accessed at all time using the orxStructure_* API.
    Most of what I've described above is encapsulated in a base class that only has virtual stub methods for specific events (e.g. animation end), but has generic code for event handlers (e.g. animation event handler). What I'd like to do is have the base class automagically inspect the created orxOBJECT, and add user data information to any child objects (other orxobjects, spawners, etc) so that class event handlers can quickly and accurately determine if they need to react to a particular event or pass it on.
    You might want to base your work on Scroll instead then. Scroll is a thin C++ layer that sits on top of orx. It adds quite a few things actually: per-object events, C++ object/config section bindings, level/map concept and an embedded level editor. All my own current projects are using Scroll and I recommend all C++ users to at least give it a try.

    Also, Acksys wrote two tutorials on how to setup Scroll and get started with it.

    You can also have a look at how I use it in Mushroom Stew, for example, though it's not using very recent versions of Scroll/orx.

    Don't hesitate if you have any questions, as usual!

    PS: Here's a forum post you might find interesting on the topic: https://forum.orx-project.org/discussion/2505
  • edited October 2012
    I'll download it tonight.

    Thanks again!
  • edited October 2012
    My pleasure!
Sign In or Register to comment.