How can I remove an event if it is executing? [solved]

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

Toge
Just popping in
Just popping in
Posts: 12
Joined: Thu Jan 22, 2009 21:59

How can I remove an event if it is executing? [solved]

Postby Toge » Sat Mar 21, 2009 19:11

Hi.

My code is:

Code: Select all

...

button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       button->removeAllEvents();
       // I want to subscribe another event to this button in this place
}



When I click the button, I get a segmentation fault.


Thanks.





Edit: I solved it. I override the old event by the new event

Code: Select all

...

button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::newHandler, this));
}





Edit 2:

Finally, I needed to use a "deadlist". When a Button was clicked, its event was pushed back in the list. The list was updated (cleared) every frame.
Last edited by Toge on Tue Mar 24, 2009 21:28, edited 2 times in total.

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

Postby CrazyEddie » Sat Mar 21, 2009 23:14

Hi,

The 'removeAllEvents' function actually removes the event objects themselves as opposed to subscribed handlers :D

I'm also not sure if the 'fix' is what you want - since it leaves the original handler function connected also - so every time it's clicked, you get another copy of the second handler added; it will subsequently be invoked multiple times ;)

The current 'correct' method to disconnect an event subscriber is to save the connection object returned when you subscribe, and later call the 'disconnect' member function on that connection to disconnect.

HTH

CE.

Toge
Just popping in
Just popping in
Posts: 12
Joined: Thu Jan 22, 2009 21:59

Postby Toge » Sun Mar 22, 2009 09:04

You are right. When I click button, multiple events are executed. Im go to use "disconnect". Thanks.

Toge
Just popping in
Just popping in
Posts: 12
Joined: Thu Jan 22, 2009 21:59

Postby Toge » Sun Mar 22, 2009 10:07

I have this code now:

Code: Select all


Event::Connection connection;

...

connection = button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       connection->disconnect();
}



However, the program ends with segmentation fault. Is it possible that a Event disconnects itself?

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

Postby CrazyEddie » Sun Mar 22, 2009 11:42

Hi,

I should have spotted the deliberate mistake last night, though it was late and I was knackered :lol:

Currently there are issues when disconnecting event subscribers from within a subscriber attached to the same event priority queue. The main reason this is the case is because it messes up the STL iterator used to iterate over subscribers.

What you might have to do is maintain a 'dead list' of connections to be removed at the end of the frame (or something).

Sorry about the confusion :?

CE


Return to “Help”

Who is online

Users browsing this forum: No registered users and 15 guests