[SOLVED] Subscribing to a global function.

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

agamemnus
Just can't stay away
Just can't stay away
Posts: 185
Joined: Sun Mar 14, 2010 04:21

[SOLVED] Subscribing to a global function.

Postby agamemnus » Fri May 07, 2010 20:14

For my wrapper to work, I need to be able to subscribe to a global function, using a pointer to a function memory location.

I cannot figure out how to do this. I can't even figure out how to subscribe to a global function...

Here is the code:

Code: Select all

void DLL_EXPORT CEGUIwindowSubscribeEvent (u32 *windowPtr, char *eventString, unsigned int *funcPtr) {
((CEGUI::Window*)windowPtr)->subscribeEvent (eventString, (reinterpret_cast<bool(*)(const CEGUI::EventArgs&)>(funcPtr)) );
}


There's no error here, but it does not seem to fire at all. The test eventString is "FrameWindow:::EventClicked".
Last edited by agamemnus on Thu May 13, 2010 20:35, edited 1 time in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Subscribing to a global function.

Postby CrazyEddie » Sat May 08, 2010 11:23

The code, as discussed the other day, should be fine. The event string is the issue, generally (but not always - for historical / compatibility reasons) the string content is the same as the C++ symbol but without the class name and without the 'Event' prefix. So, for the symbol FrameWindow::EventCloseClicked the string is simply "CloseClicked".

For info, there is no event with the symbol "FrameWindow:::EventClicked" used in the default system.

CE.

agamemnus
Just can't stay away
Just can't stay away
Posts: 185
Joined: Sun Mar 14, 2010 04:21

Re: Subscribing to a global function.

Postby agamemnus » Sun May 09, 2010 04:37

Wow.... it works! Thank you.

Now, all I need is to figure out how I can reference the event arguments and I'll be all set. :lol:

Edit: Omg, omg, omg, it works! It all works! *dances around ecstatically* Mouahahahaha.... :twisted:

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Subscribing to a global function.

Postby CrazyEddie » Sun May 09, 2010 15:49

:shock:

agamemnus
Just can't stay away
Just can't stay away
Posts: 185
Joined: Sun Mar 14, 2010 04:21

Re: Subscribing to a global function.

Postby agamemnus » Mon May 10, 2010 15:55

Hmm... how do I modify the event subscription call so that it took a data type pointer as a second parameter?

IE:

* I want to take the "this" from my "class" (we call them UDTs or user-define-types in Freebasic.. they work slightly differently) and convert it into a u32 pointer.
* Then, I want to pass it to the event subscription in such a way that my event callback function receives first a pointer to eventargs and then a pointer to the class.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Subscribing to a global function.

Postby CrazyEddie » Tue May 11, 2010 08:46

The signature for an event handler must match:

Code: Select all

bool function_name(const CEGUI::EventArgs&);

You can't add (or change) arguments or argument types.

Also be aware that a class does not have a 'this' pointer. Each instance of the class has a pointer, and within that instance the pointer is 'this'.

CE.

agamemnus
Just can't stay away
Just can't stay away
Posts: 185
Joined: Sun Mar 14, 2010 04:21

Re: Subscribing to a global function.

Postby agamemnus » Wed May 12, 2010 02:43

CrazyEddie wrote:The signature for an event handler must match:

Code: Select all

bool function_name(const CEGUI::EventArgs&);

You can't add (or change) arguments or argument types.

Also be aware that a class does not have a 'this' pointer. Each instance of the class has a pointer, and within that instance the pointer is 'this'.

CE.


You mean to say the "this" pointer is somehow static within the class instance? Or what? In Freebasic, I think, the this pointer is invisibly passed to the function... in this case, it has to be visible...

User avatar
scriptkid
Home away from home
Home away from home
Posts: 1178
Joined: Wed Jan 12, 2005 12:06
Location: The Hague, The Netherlands
Contact:

Re: Subscribing to a global function.

Postby scriptkid » Wed May 12, 2010 09:54

'This' is not static; it's different per instance (it *is* the instance). The 'invisible' pass only happens when a method calls into another method of its own class. C++ does that too.

Okay, about passing your 'this' to a class. First, i don't understand your requirement. Cegui will only trigger events for Windows, but you want to use it for your own custom classes too? Or classes which have a 1:1 relationship with a Cegui Window?

Anyhow, here is some input which might help:

What you can do is define your own eventArgs class, which is based on CEGUI::EventArgs. Then you can add new variables to that one. For example:

Code: Select all

class MyEventArgs: public CEGUI::EventArgs {
public:
  int* m_this;
}


(for exact syntax and requirements, look at CEGUI::MouseEventArgs for example).

Of course, this only works when you are firing the event yourself, otherwise Cegui will decide for its own eventArgs versions.

If this is too much hassle, or if Cegui is firing your events, you always get the Window (args->m_window). If you have a 1:1 relation with your own object, you can set its userid or data:

Code: Select all

myWindow->setUserID((int*)this);


And then in your eventhandler:

Code: Select all

int* relatedInstance = args->m_window->getUserID();


HTH.
Check out my released snake game using Cegui!

agamemnus
Just can't stay away
Just can't stay away
Posts: 185
Joined: Sun Mar 14, 2010 04:21

Re: Subscribing to a global function.

Postby agamemnus » Wed May 12, 2010 15:37

Hey, good idea. Hackish, but a good idea. Thanks. :D

User avatar
scriptkid
Home away from home
Home away from home
Posts: 1178
Joined: Wed Jan 12, 2005 12:06
Location: The Hague, The Netherlands
Contact:

Re: Subscribing to a global function.

Postby scriptkid » Thu May 13, 2010 11:13

agamemnus wrote:Hey, good idea. Hackish, but a good idea. Thanks. :D


No, it's by design to be able to do such things :)
Check out my released snake game using Cegui!


Return to “Help”

Who is online

Users browsing this forum: No registered users and 24 guests