Refresh Code:
Code: Select all
local function load(filename)
local ok, chunk, result
ok, chunk = pcall( love.filesystem.load, filename ) -- load the chunk safely
if not ok then
print('The following error happened1: ' .. tostring(chunk))
else
ok, result = pcall(chunk) -- execute the chunk safely
if not ok then -- will be false if there is an error
print('The following error happened2: ' .. tostring(result))
else
print('The result of loading is: ' .. tostring(result))
end
end
end
function refresh()
load("main.lua")
end
I have also come across the debug.debug() function which has been a great help to me in figuring out what my program is doing. This is an important tool. Here's how this program works:
1. You NEED to be able to access this function, which can be done through debug.debug() which you can access through a code like this::
Code: Select all
function love.keypressed(key, unicode)
if key == "rctrl" then
debug.debug()
end
end
Code: Select all
do
local pressed = {}
function love.keypressed(key, unicode)
pressed[key] = true
if key == "r" then
if pressed["lctrl"] then
refresh()
end
elseif key == "rctrl" then
debug.debug()
end
end
function love.keyreleased(key, unicode)
pressed[key] = nil
end
end
- A. Changes all the code that you are running (anything that is a global variable and defined explicitly in main.lua)
B. Will NOT change existing globals previously defined but not defined explicitly in main.lua (such as through love.load() )
C. If a function was previously defined when you ran the program, but not defined in the new main.lua file, it will still exist after the refresh (which may cause actual problems if your new code depends on the old code but doesn't contain the old code, or may just take up memory). This can be remedied by nil-ing the global if you feel it's a problem.
Code: Select all
function love.load(arg)
Universe = {}
end
function love.keypressed(key, code)
if key == "rctrl" then
debug.debug()
elseif key == "t" then
table.insert(Universe, love.timer.getTime())
end
end
Code: Select all
Universe = {}
function love.keypressed(key, code)
if key == "rctrl" then
debug.debug()
elseif key == "t" then
table.insert(Universe, love.timer.getTime())
end
end
Last piece: for those who use love.run() in their code, changing this function using refresh does not change its usage. If you wish to permanently change this function, you must reload the program.