diff --git a/src/accessories/TokenArranger.ttslua b/src/accessories/TokenArranger.ttslua index e167d79c..ab40d7a2 100644 --- a/src/accessories/TokenArranger.ttslua +++ b/src/accessories/TokenArranger.ttslua @@ -20,7 +20,6 @@ inputParameters.tab = 2 -- variables with save function local tokenPrecedence = {} -local latestLoad = "XXX" local percentage = false -- variables without save function @@ -42,7 +41,6 @@ local TOKEN_NAMES = { function onSave() return JSON.encode({ tokenPrecedence = tokenPrecedence, - latestLoad = latestLoad, percentage = percentage }) end @@ -52,10 +50,12 @@ function onLoad(saveState) if saveState ~= nil and saveState ~= "" then local loadedData = JSON.decode(saveState) tokenPrecedence = loadedData.tokenPrecedence - latestLoad = loadedData.latestLoad or "XXX" percentage = loadedData.percentage else loadDefaultValues() + + -- grab token metadata from mythos area + Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2) end createButtonsAndInputs(true) @@ -63,7 +63,6 @@ function onLoad(saveState) -- context menu items self.addContextMenuItem("Load default values", function() - latestLoad = "XXX" loadDefaultValues() updateUI() layout() @@ -90,9 +89,6 @@ function onLoad(saveState) "Yellow") layout() end) - - -- grab token metadata from mythos area - Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2) end -- delete temporary tokens when destroyed @@ -383,11 +379,6 @@ function onTokenDataChanged(parameters) local currentScenario = parameters.currentScenario or "" local useFrontData = parameters.useFrontData - -- only update if this data is new - local info = currentScenario .. tostring(useFrontData) - if latestLoad == info then return end - latestLoad = info - -- update token precedence for key, table in pairs(tokenData) do local modifier = table.modifier diff --git a/src/core/MythosArea.ttslua b/src/core/MythosArea.ttslua index fe891893..c39f999a 100644 --- a/src/core/MythosArea.ttslua +++ b/src/core/MythosArea.ttslua @@ -41,37 +41,28 @@ end -- TTS event handler. Handles scenario name event triggering and encounter card token resets. function onCollisionEnter(collisionInfo) - if not collisionEnabled then - return - end + if not collisionEnabled then return end local object = collisionInfo.collision_object + if object.getName() == "Scenario" then - local updateNeeded = false local description = object.getDescription() - -- detect if orientation of scenario card changed (flipped to other side) - if object.is_face_down == useFrontData then - useFrontData = not useFrontData - updateNeeded = true - end - - -- detect if another scenario card is placed down + -- detect if a new scenario card is placed down if currentScenario ~= description then currentScenario = description - updateNeeded = true fireScenarioChangedEvent() end - -- trigger update if a change was detected and push new data - if updateNeeded then - local metadata = JSON.decode(object.getGMNotes()) or {} - if not metadata["tokens"] then - tokenData = {} - return - end - tokenData = metadata["tokens"][(useFrontData and "front" or "back")] - fireTokenDataChangedEvent() + local metadata = JSON.decode(object.getGMNotes()) or {} + if not metadata["tokens"] then + tokenData = {} + return end + + -- detect orientation of scenario card (for difficulty) + useFrontData = not object.is_face_down + tokenData = metadata["tokens"][(useFrontData and "front" or "back")] + fireTokenDataChangedEvent() end local localPos = self.positionToLocal(object.getPosition()) @@ -81,6 +72,19 @@ function onCollisionEnter(collisionInfo) end end +-- TTS event handler. Handles scenario name event triggering +function onCollisionExit(collisionInfo) + if not collisionEnabled then return end + local object = collisionInfo.collision_object + + -- reset token metadata if scenario reference card is removed + if object.getName() == "Scenario" then + tokenData = {} + useFrontData = nil + fireTokenDataChangedEvent() + end +end + -- Listens for cards entering the encounter deck or encounter discard, and resets the spawn state -- for the cards when they do. function onObjectEnterContainer(container, object)