cecho("" .. room.name .. ". [" .. room.num .. "] [<" .. getEnvironmentColor(room.env) .. ">" .. getEnvironmentShortName(room.env) .. "] [" .. getCleanAreaName(room.area) .. "]")Just a complete mess, right? The Lua function string.format() alleviates some of this by using a handy token syntax. It can also be a little bit of a pain though - who wants to wrap every string they write in another nested function?
getmetatable("").__mod = function(a, b)
if not b then
return a
elseif type(b) == "table" then
return string.format(a, unpack(b))
else
return string.format(a, b)
end
end
local my_name = "Ben"But now you can do:
local my_age = "twenty"
echo("Hello, my name is " .. my_name .. ". I am " .. my_age .. " years old.")
local my_name = "Ben"Although a tiny bit tricky to get used to at first, this new method can greatly save writing time and improve readability. It works anywhere you'd use a string. In addition, string.format() is a highly optimized function implemented in ANSI C, and though most users won't notice a difference, it'll be a little easier on your system where memory usage is concerned.
local my_age = 20
echo("Hello, my name is %s. I am %d years old." % {my_name, my_age})
local my_divine = "The Goddess %s" % {"Iosyne"}And thanks to just how chill Lua is with indenting/linebreaking your functions for readability's sake, you can rewrite that monstrosity at the beginning of this post as...
enableTrigger("%s" % {my_trigger})
etc. etc.
cecho("%s. [%d] [<%s>%s] [%s]" % {Still a lot to track, but damn if it isn't easier to manage.
room.name, room.num, getEnvironmentColor(room.env), getEnvironmentShortName(room.env), getCleanAreaName(room.area)
})
Comments