[KNOWN ISSUE] Listeners are not called in a consistent order

User avatar
Rahkiin
Former GIANTS employee
Posts: 753
Joined: Tue Jan 17, 2017 12:45 pm

[KNOWN ISSUE] Listeners are not called in a consistent order

Post by Rahkiin »

When testing our mod, we had an inconsistency issue.
For many reasons, within our mod, the loading order of our components matters. There are dependencies, especially in a mod such as ours.
Many classes add new listeners to the game, like weatherChanged, dayChanges, hour/minute Changed. We even made a couple of new ones ourselves.

We do rely on the order these are called, they need to be deterministic. But they don't appear to be.
I looked into it and printed g_currentMission.environment, I found the lists of listeners, and they are filled by using the function as a key, where 'function' is the actual listener function. I guess this allows very fast removal as well.
However, this list can't be iterated using ipairs(), and pairs() does not guarantee the order. See http://lua-users.org/wiki/ForTutorial (could not find a better link atm).

To fix this, it is possible to use table.insert(listeners, function) instead, iterate using ipairs(), and to remove it use table.remove after finding the index. I know this might be slower than using a hashmap, but it does keep it deterministic.

- PC/Mac/PS4/XB1: All
- Vehicle/Tool/Model/Object: Engine
- Issue: Non-deterministic listeners
- Single- or Multiplayer: Both
- Steps to reproduce: Run some code 1000000s of times :p

EDIT: this bug actually causes unreproducible bugs in the game and mods. We might have hit it today again as well.