From 5726409e424f0ca0f441b5b899a9d03afc3dddeb Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Tue, 20 Dec 2022 20:35:33 +0100 Subject: [PATCH] save GUIDs for spawned helper objects, remove them by GUID --- config.json | 2 +- src/core/Global.ttslua | 80 +++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/config.json b/config.json index c28dcada..2a9cc134 100644 --- a/config.json +++ b/config.json @@ -22,7 +22,7 @@ }, "Lighting_path": "Lighting.json", "LuaScript": "require(\"core/Global\")", - "LuaScriptState": "{\"optionPanel\":{\"useSnapTags\":true,\"showDrawButton\":false,\"useClueClickers\":false,\"showTokenArranger\":false,\"showCleanUpHelper\":false,\"showHandHelper\":false}}", + "LuaScriptState": "{\"optionPanel\":{\"showDrawButton\":false,\"showHandHelper\":[],\"useClueClickers\":false,\"useSnapTags\":true}}", "MusicPlayer_path": "MusicPlayer.json", "Note": "", "ObjectStates_order": [ diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index c8c54782..07228924 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -804,14 +804,15 @@ function onClick_toggleOption(_, id) end self.UI.setAttribute(id, "isOn", state) - optionPanel[id] = state applyOptionPanelChange(id, state) end -- sets the option panel to the correct state (corresponding to 'optionPanel') function updateOptionPanelState() for id, enabled in pairs(optionPanel) do - if enabled then + if (type(enabled) == "boolean" and enabled) or + (type(enabled) == "string" and enabled) or + (type(enabled) == "table" and #enabled ~= 0) then self.UI.setAttribute(id, "isOn", true) end end @@ -824,14 +825,17 @@ function applyOptionPanelChange(id, state) -- option: Snap tags if id == "useSnapTags" then playmatAPI.setLimitSnapsByType(state, "All") + optionPanel[id] = state -- option: Draw 1 button elseif id == "showDrawButton" then playmatAPI.showDrawButton(state, "All") + optionPanel[id] = state -- option: Clickable clue counters elseif id == "useClueClickers" then playmatAPI.clickableClues(state, "All") + optionPanel[id] = state -- update master clue counter getObjectFromGUID("4a3aa4").setVar("useClickableCounters", state) @@ -841,41 +845,42 @@ function applyOptionPanelChange(id, state) -- delete previously pulled out tokens for _, token in ipairs(getObjectsWithTag("to_be_deleted")) do token.destruct() end - spawnOrRemoveHelper(state, "Token Arranger", {-42.3, 1.4, -46.5}) + optionPanel[id] = spawnOrRemoveHelper(state, "Token Arranger", {-42.3, 1.4, -46.5}) -- option: Show clean up helper elseif id == "showCleanUpHelper" then - spawnOrRemoveHelper(state, "Clean Up Helper", {-68, 1.6, 35.5}) + optionPanel[id] = spawnOrRemoveHelper(state, "Clean Up Helper", {-68, 1.6, 35.5}) -- option: Show hand helper for each player elseif id == "showHandHelper" then - spawnOrRemoveHelper(state, "Hand Helper", {-50.84, 1.6, 7.02}, {0, 270, 0}, "White") - spawnOrRemoveHelper(state, "Hand Helper", {-50.90, 1.6, -25.10}, {0, 270, 0}, "Orange") - spawnOrRemoveHelper(state, "Hand Helper", {-34.38, 1.6, 22.44}, {0, 000, 0}, "Green") - spawnOrRemoveHelper(state, "Hand Helper", {-16.69, 1.6, -22.42}, {0, 180, 0}, "Red") + optionPanel[id][1] = spawnOrRemoveHelper(state, "Hand Helper", {-50.84, 1.6, 7.02}, {0, 270, 0}, "White") + optionPanel[id][2] = spawnOrRemoveHelper(state, "Hand Helper", {-50.90, 1.6, -25.10}, {0, 270, 0}, "Orange") + optionPanel[id][3] = spawnOrRemoveHelper(state, "Hand Helper", {-34.38, 1.6, 22.44}, {0, 000, 0}, "Green") + optionPanel[id][4] = spawnOrRemoveHelper(state, "Hand Helper", {-16.69, 1.6, -22.42}, {0, 180, 0}, "Red") -- option: Show chaos bag manager elseif id == "showChaosBagManager" then - spawnOrRemoveHelper(state, "Chaos Bag Manager", {-67.8, 1.4, -49.5}) + optionPanel[id] = spawnOrRemoveHelper(state, "Chaos Bag Manager", {-67.8, 1.4, -49.5}) -- option: Show navigation overlay elseif id == "showNavigationOverlay" then - spawnOrRemoveHelper(state, "jaqenZann's Navigation Overlay", {-11.7, 1.4, -15}) + optionPanel[id] = spawnOrRemoveHelper(state, "jaqenZann's Navigation Overlay", {-11.7, 1.4, -15}) end end -- handler for spawn / remove functions of helper objects ---@param state Boolean Contains the state of the option: true = spawn it, false = remove it ---@param name String Name of the helper object ----@param position Vector Position of the object (where it will spawn or where it will be removed from) +---@param position Vector Position of the object (where it will spawn) ---@param rotation Vector Rotation of the object for spawning (default: {0, 270, 0}) ---@param color Color This is only needed for correctly setting the color of the "Hand Helper" +-- returns the GUID of the spawnedObj (or nil if object was removed) function spawnOrRemoveHelper(state, name, position, rotation, color) if state then - spawnHelperObject(name, position, rotation, color) - Player["White"].pingTable(position) + Player.getPlayers()[1].pingTable(position) + return spawnHelperObject(name, position, rotation, color).getGUID() else - removeHelperObject(name, position) + return removeHelperObject(name) end end @@ -883,14 +888,12 @@ end ---@param name String Name of the object that should be copied ---@param position Position Desired position of the object function spawnHelperObject(name, position, rotation, color) - if rotation == nil then rotation = {0, 270, 0} end - for _, obj in ipairs(getObjectFromGUID(BARREL_GUID).getData().ContainedObjects) do if obj["Nickname"] == name then - spawnObjectData({ + return spawnObjectData({ data = obj, position = position, - rotation = rotation, + rotation = rotation or {0, 270, 0}, callback_function = function(object) if name == "Hand Helper" then Wait.time(function() object.call("externalColorChange", color) end, 1) @@ -899,29 +902,34 @@ function spawnHelperObject(name, position, rotation, color) end end }) - return end end end --- removes the specified tool (by name) from the provided position +-- removes the specified tool (by name) ---@param name String Name of the object that should be removed ----@param position Position Position of the object -function removeHelperObject(name, position) - local search = Physics.cast({ - direction = { 0, 1, 0 }, - max_distance = 1, - type = 3, - size = {1, 1, 1}, - origin = position, - orientation = { 0, 270, 0 } - }) +function removeHelperObject(name) + -- links objects name to the respective option name (to grab the GUID for removal) + local referenceTable = { + ["Token Arranger"] = "showTokenArranger", + ["Clean Up Helper"] = "showCleanUpHelper", + ["Hand Helper"] = "showHandHelper", + ["Chaos Bag Manager"] = "showChaosBagManager", + ["jaqenZann's Navigation Overlay"] = "showNavigationOverlay" + } - for _, obj in ipairs(search) do - obj = obj.hit_object - if obj.getName() == name then - obj.destruct() - return + local data = optionPanel[referenceTable[name]] + + -- if there is a GUID stored, remove that object + if type(data) == "string" then + local obj = getObjectFromGUID(data) + if obj then obj.destruct() end + + -- if it is a table (e.g. for the "Hand Helper", remove all of them) + elseif type(data) == "table" then + for _, guid in pairs(data) do + local obj = getObjectFromGUID(guid) + if obj then obj.destruct() end end end -end \ No newline at end of file +end