remove usage of "_G"

This commit is contained in:
Chr1Z93 2022-11-24 17:32:29 +01:00
parent 3cf51936f2
commit 5bda3f431b

View File

@ -2,8 +2,8 @@
-- made by: Chr1Z -- made by: Chr1Z
-- description: for easier managing of the chaos bag (adding / removing tokens) -- description: for easier managing of the chaos bag (adding / removing tokens)
information = { information = {
version = "1.4", version = "1.5",
last_updated = "12.11.2022" last_updated = "24.11.2022"
} }
local TOKEN_URL = { local TOKEN_URL = {
@ -44,29 +44,13 @@ local BUTTON_TOOLTIP = {
"Elder Sign", "Skull", "Cultist", "Tablet", "Elder Thing", "Auto-fail" "Elder Sign", "Skull", "Cultist", "Tablet", "Elder Thing", "Auto-fail"
} }
local tokenarranger = getObjectFromGUID("022907")
local z = { -0.778, 0, 0.75 }
local BUTTON_POSITION = { local BUTTON_POSITION = {
-- first row -- first row
{ -1.90, 0, z[1] }, -1.90, -1.14, -0.38, 0.38, 1.14, 1.90,
{ -1.14, 0, z[1] },
{ -0.38, 0, z[1] },
{ 0.38, 0, z[1] },
{ 1.14, 0, z[1] },
{ 1.90, 0, z[1] },
-- second row -- second row
{ -1.90, 0, z[2] }, -1.90, -1.14, -0.38, 0.38, 1.90,
{ -1.14, 0, z[2] },
{ -0.38, 0, z[2] },
{ 0.38, 0, z[2] },
{ 1.90, 0, z[2] },
-- third row -- third row
{ -1.90, 0, z[3] }, -1.90, -1.14, -0.38, 0.38, 1.14, 1.90
{ -1.14, 0, z[3] },
{ -0.38, 0, z[3] },
{ 0.38, 0, z[3] },
{ 1.14, 0, z[3] },
{ 1.90, 0, z[3] },
} }
-- common button parameters -- common button parameters
@ -76,12 +60,28 @@ buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 300 buttonParameters.width = 300
buttonParameters.height = 300 buttonParameters.height = 300
local UPDATING = false
local tokenarranger
local name
local tokens = {}
function onLoad() function onLoad()
-- create buttons for tokens -- create buttons for tokens
for i = 1, #BUTTON_POSITION do for i = 1, #BUTTON_POSITION do
buttonParameters.position = BUTTON_POSITION[i] local funcName = "buttonClick" .. i
buttonParameters.click_function = attachIndex("button_click", i) self.setVar(funcName, function(_, _, isRightClick) buttonClick(_, _, isRightClick, i) end)
buttonParameters.click_function = funcName
buttonParameters.tooltip = BUTTON_TOOLTIP[i] buttonParameters.tooltip = BUTTON_TOOLTIP[i]
buttonParameters.position = { x = BUTTON_POSITION[i], y = 0, z = 0 }
if i < 7 then
buttonParameters.position.z = -0.778
elseif i > 12 then
buttonParameters.position.z = 0.75
end
self.createButton(buttonParameters) self.createButton(buttonParameters)
end end
@ -90,6 +90,8 @@ function onLoad()
printToAll("Chaos Bag Manager v" .. information["version"] .. " by Chr1Z", "Orange") printToAll("Chaos Bag Manager v" .. information["version"] .. " by Chr1Z", "Orange")
printToAll("last updated: " .. information["last_updated"], "White") printToAll("last updated: " .. information["last_updated"], "White")
end) end)
tokenarranger = getObjectFromGUID("022907")
end end
-- get chaos bag from scripting zone and description -- get chaos bag from scripting zone and description
@ -117,17 +119,8 @@ function getChaosBag()
return chaosbag return chaosbag
end end
-- helper function to carry index
function attachIndex(click_function, index)
local fn_name = click_function .. index
_G[fn_name] = function(obj, player_color, alt_click)
_G[click_function](obj, player_color, alt_click, index)
end
return fn_name
end
-- click function for buttons -- click function for buttons
function button_click(obj, player_color, alt_click, index) function buttonClick(_, _, isRightClick, index)
chaosbag = getChaosBag() chaosbag = getChaosBag()
-- error handling: chaos bag not found -- error handling: chaos bag not found
@ -140,7 +133,7 @@ function button_click(obj, player_color, alt_click, index)
end end
token = TOKEN_NAMES[index] token = TOKEN_NAMES[index]
if alt_click then if isRightClick then
-- error handling: no matching token found -- error handling: no matching token found
if #tokens == 0 then if #tokens == 0 then
printToAll("No " .. name .. " tokens in the chaos bag.", "Yellow") printToAll("No " .. name .. " tokens in the chaos bag.", "Yellow")
@ -152,7 +145,7 @@ function button_click(obj, player_color, alt_click, index)
guid = tokens[1], guid = tokens[1],
position = self.getPosition(), position = self.getPosition(),
smooth = false, smooth = false,
callback_function = remove_callback callback_function = removeCallback
}) })
else else
-- spawn token (only 8 frost tokens allowed) -- spawn token (only 8 frost tokens allowed)
@ -165,7 +158,7 @@ function button_click(obj, player_color, alt_click, index)
type = 'Custom_Tile', type = 'Custom_Tile',
position = chaosbag.getPosition() + Vector(0, 1, 0), position = chaosbag.getPosition() + Vector(0, 1, 0),
rotation = { x = 0, y = 260, z = 0 }, rotation = { x = 0, y = 260, z = 0 },
callback_function = spawn_callback callback_function = spawnCallback
}) })
obj.setCustomObject({ obj.setCustomObject({
type = 2, type = 2,
@ -177,18 +170,17 @@ function button_click(obj, player_color, alt_click, index)
updateTokenArranger() updateTokenArranger()
end end
function remove_callback(obj) function removeCallback(obj)
printToAll("Removing " .. name .. " token (in bag: " .. #tokens - 1 .. ")", "White") printToAll("Removing " .. name .. " token (in bag: " .. #tokens - 1 .. ")", "White")
obj.destruct() obj.destruct()
end end
function spawn_callback(obj) function spawnCallback(obj)
obj.scale { 0.81, 1, 0.81 } obj.scale { 0.81, 1, 0.81 }
obj.setName(name) obj.setName(name)
printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White") printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White")
end end
UPDATING = false
function updateTokenArranger() function updateTokenArranger()
if tokenarranger and not UPDATING then if tokenarranger and not UPDATING then
UPDATING = true UPDATING = true