libgdx / gdx-ai

Artificial Intelligence framework for games based on libGDX or not. Features: Steering Behaviors, Formation Motion, Pathfinding, Behavior Trees and Finite State Machines
Apache License 2.0
1.2k stars 242 forks source link

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

Closed MonsterOfCookie closed 7 years ago

MonsterOfCookie commented 8 years ago

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

davebaol commented 8 years ago

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

MonsterOfCookie commented 8 years ago

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.