Skip to content

Instantly share code, notes, and snippets.

@Losiel
Created February 9, 2022 14:19
Show Gist options
  • Save Losiel/61ee5e2a0e5f94dd32a646908e40cdf9 to your computer and use it in GitHub Desktop.
Save Losiel/61ee5e2a0e5f94dd32a646908e40cdf9 to your computer and use it in GitHub Desktop.
Misc functions for Lua
-- a bunch of random functions for Lua
-- allows a string to explore a table
-- you pass it the string and the table you allow it to explore
-- it formats the string like "You have {points} points!" (equivalent to "You have " .. t.points .. " points!")
-- it also can explore tables inside the table, like "Hi {player.name}!"
-- or a numerical index "Hi {0}" "Hi {player.0}"
local function format(str, t)
return string.gsub(str, "{(.+)}", function(s)
local value = t
for k in string.gmatch(s, "([^%.]+)") do
value = value[tonumber(k) or k]
end
return tostring(value)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment