libgdx/gdx-ai

Removing a Telegram listener during discharge will cause other listeners to be ignored

MonsterOfCookie opened this issue · 2 comments

Issue details

If you have two listeners to a message, and the first one removes itself upon receiving that message, the other listener will not get the message delivered.

I believe it is because of the way the for loop is constructed in the discharge method

    Array<Telegraph> listeners = msgListeners.get(telegram.message);
    if (listeners != null) {
        for (int i = 0; i < listeners.size; i++) {
            if (listeners.get(i).handleMessage(telegram)) {
                 handledCount++;
            }
        }
    }

Reproduction steps/code

Create two listeners to the same message
Have the first remove itself as a listener during the onMessageRecieved call
The second listener will not get the call

Version of gdx-ai and/or relevant dependencies

1.8.0

Mmmmm... I see your problem, but can't find a super-easy fix.
I'd say you have 2 options:

  • Can't you just use Gdx.app.postRunnable to unregister the listener on next rendering cycle? This workaround should solve the issue in your code. 👀
  • PR welcome 😄

Yea, postRunnable is what I went with, along with refactoring my states so I can avoid removing as well.

I couldn't see an easy fix straight away either, otherwise I would have PR'd :)

I will have a think on it though.

Thanks for making an awesome extension.