Created
February 9, 2022 14:19
-
-
Save Losiel/61ee5e2a0e5f94dd32a646908e40cdf9 to your computer and use it in GitHub Desktop.
Misc functions for Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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