bg.call("OnObjectEnterZone", { zone = zone, object = object })
I think this might be causeing a problem.
onObjectEnterZone (and onObjectLeaveZone are triggered for every object on every zone in every script
I realized this, which is why I was so careful to gate everything behind if/then checks (that are themselves as efficient as possible --- i.e. a simple boolean flag set by something else, or a check for a tag, rather than a function that has to iterate through a bunch of objects to determine true or false).
In the case of bg.call("onObjectEnterZone"), that should only ever trigger if Z.isBattlegroundZone(zone) returns true, and only if the phase of the game (held in gameState) is "PLAY" (thanks to the if not S.isInPhase("PLAY") then return end gate earlier in the function) --- there are only three battleground zones, and Z.isBattlegroundZone(zone) is just a wrapper around a simple check to see whether zone.guid is contained in the G.zones.battleground table.
The zone listeners work perfectly well during play, no performance issues there. It's only when I activate the zones by setting their global event listeners to the functions that there's a delay of a second or two
Can you elaborate this? What do you mean?
Certainly. During play, when onObjectEnter/LeaveZone is triggered through normal gameplay (e.g. by dropping a card or token into the zone), there are no performance issues. The lag spike occurs when I trigger my ActivateZones() function, which is the partner to DeactivateZones():
function DeactivateZones()
onObjectEnterZone = nil
onObjectLeaveZone = nil
end
function ActivateZones()
onObjectEnterZone = Z.onObjectEnterZone
onObjectLeaveZone = Z.onObjectLeaveZone
end```
I use these functions to toggle the zone event listeners off temporarily when I'm running through a sequence of automated functions that would otherwise cause the zone events to fire multiple times unnecessarily. (E.g. when setting up the game, I move a bunch of objects around programmatically, and found I'd run into performance issues there because those movements would often trigger a whole bunch of zone enter/leave events (cards being dealt from a central deck would "sweep through" a bunch of zones on their way to a hand zone, causing each zone passed through to fire both `onObjectEnterZone` and `onObjectLeaveZone`). Or, when cleaning up the play space at the end of the game, simply deleting all of the tokens on the board would cause every zone's `onObjectLeaveZone` event to fire multiple times, once for each token --- so I `DeactivateZones()`, then delete the tokens, then `ActivateZones()`)
What I can't understand is why simply setting the `onObjectEnterZone`/`onObjectLeaveZone` variables would "do" anything that would cause a lag spike. (They don't trigger any events when I fire `ActivateZones()`, that was one thing I was able to test.)