Skip to content

Instantly share code, notes, and snippets.

@pfirsich
Created February 22, 2015 20:25
Show Gist options
  • Save pfirsich/51a5a51543da40a3c2fa to your computer and use it in GitHub Desktop.
Save pfirsich/51a5a51543da40a3c2fa to your computer and use it in GitHub Desktop.
Second incarnation of the tweakable-system in SudoHack
local tweakables = {}
function tweak(defaultVal, name)
if not tweakables[name] then tweakables[name] = defaultVal end
return tweakables[name]
end
local fileList = {}
function buildFileList(dir, includeDirs)
local lfs = love.filesystem
local files = lfs.getDirectoryItems(dir)
for i = 1, #files do
local file = dir .. files[i]
if lfs.isFile(file) and file:sub(-4) == ".lua" then
fileList[#fileList+1] = file
elseif lfs.isDirectory(file) then
for j = 1, #includeDirs do
if includeDirs[j] == file then
buildFileList(file .. "/", includeDirs)
break
end
end
end
end
end
function updateTweakables(includeDirs) -- will only look for *.lua files in the root directory. other directories have to be included specifically
if #fileList < 1 then -- build file list only the first time
buildFileList("", includeDirs)
end
for i = 1, #fileList do
local content = love.filesystem.read(fileList[i])
for val, name in string.gmatch(content, 'tweak%((.-), "(.-)"%)') do -- has to be exactly of the form tweak(value, "name")
tweakables[name] = tonumber(val)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment