librelist archives

« back to archive

Unsubscribing *this from event callback

Unsubscribing *this from event callback

From:
Asmodehn Shade
Date:
2014-10-29 @ 02:08
Hello,

I am using an old version of entityx, however I wanted to let you know
about this because I didnt find obvious method in entityx to do what i
wanted even in the recent one...

I recently came upon a situation where I want to subscribe and unsubscribe
to events from time to time...
Basically it s useful to me in a way that allows me to implement
continuation on events :
I start a task that will call event A later.
I start a task that will start after A has been triggered, then following A
events will be ignored...

So, in clear code, I would like to :

void It()
{
    //do stuff
}

void queueIt()
{
        if (!m_queued)
        {
            //queue It for after an event happens.
            event_manager->subscribe<Event>(*this);
            m_queued = true;
        }
}

void receive( const Event& evt)
{
       // event is received we can stop queueing
       m_queued = false;
       event_manager->*unsubscribe*<Event>(*this);
       //do it now.
       It();
}

Any advice on how to do this properly ( without breaking the current
callback I am on ? ) ?
I would expect this just as inverse of subscribe...
Of course I could make some guards into the receive method, but that is a
useless call, and can duplicate code...

Or any better way to implement continuation on events ?

Cheers,
--
AlexV

Re: [entityx] Unsubscribing *this from event callback

From:
Alec Thomas
Date:
2014-10-29 @ 03:25
Huh,  

I’m pretty surprised that I hadn’t already implemented that. I’ll take a 
look tonight, and backport it to the 0.x branch.

Alec  


On Wednesday, 29 October 2014 at 1:08 pm, Asmodehn Shade wrote:

> Hello,
>  
> I am using an old version of entityx, however I wanted to let you know 
about this because I didnt find obvious method in entityx to do what i 
wanted even in the recent one...
>  
> I recently came upon a situation where I want to subscribe and 
unsubscribe to events from time to time...
> Basically it s useful to me in a way that allows me to implement 
continuation on events :
> I start a task that will call event A later.
> I start a task that will start after A has been triggered, then 
following A events will be ignored...
>  
> So, in clear code, I would like to :
>  
> void It()
> {
>     //do stuff
> }
>  
> void queueIt()
> {
>         if (!m_queued)
>         {
>             //queue It for after an event happens.
>             event_manager->subscribe<Event>(*this);
>             m_queued = true;
>         }
> }
>  
> void receive( cons t Event& evt)
> {
>        // event is received we can stop queueing
>        m_queued = false;
>        event_manager->unsubscribe<Event>(*this);
>        //do it now.
>        It();
> }
>  
> Any advice on how to do this properly ( without breaking the current 
callback I am on ? ) ?
> I would expect this just as inverse of subscribe...
> Of course I could make some guards into the receive method, but that is 
a useless call, and can duplicate code...
>  
> Or any better way to implement continuation on events ?
>  
> Cheers,
> --
> AlexV

Re: [entityx] Unsubscribing *this from event callback

From:
Alec Thomas
Date:
2014-10-29 @ 12:17
Turns out this is a bit more involved than I had hoped. The signal wraps 
the raw method in a std::function, so there’s no way to lookup the signal 
by its original function address. I’ll need to add a mapping to track the 
signals.  


On Wednesday, 29 October 2014 at 2:25 pm, Alec Thomas wrote:

> Huh,  
>  
> I’m pretty surprised that I hadn’t already implemented that. I’ll take a
look tonight, and backport it to the 0.x branch.
>  
> Alec  
>  
> On Wednesday, 29 October 2014 at 1:08 pm, Asmodehn Shade wrote:
>  
> > Hello,
> >  
> > I am using an old version of entityx, however I wanted to let you know
about this because I didnt find obvious method in entityx to do what i 
wanted even in the recent one...
> >  
> > I recently came upon a situation where I want to subscribe and 
unsubscribe to events from time to time...
> > Basically it s useful to me in a way that allows me to implement 
continuation on events :
> > I start a task that will call event A later.
> > I start a task that will start after A has been triggered, then 
following A events will be ignored...
> >  
> > So, in clear code, I would like to :
> >  
> > void It()
> > {
> >     //do stuff
> > }
> >  
> > void queueIt()
> > {
> >         if (!m_queued)
> >         {
> >             //queue It for after an event happens.
> >             event_manager->subscribe<Event>(*this);
> >             m_queued = true;
> >         }
> > }
> >  
> > void receive( cons t Event& evt)
> > {
> >        // event is received we can stop queueing
> >        m_queued = false;
> >        event_manager->unsubscribe<Event>(*this);
> >        //do it now.
> >        It();
> > }
> >  
> > Any advice on how to do this properly ( without breaking the current 
callback I am on ? ) ?
> > I would expect this just as inverse of subscribe...
> > Of course I could make some guards into the receive method, but that 
is a useless call, and can duplicate code...
> >  
> > Or any better way to implement continuation on events ?
> >  
> > Cheers,
> > --
> > AlexV
>