Refresh - without reloading the file!
Posted: Sat Aug 27, 2011 11:13 pm
If anyone has ever used Corona SDK's software, they know that they can modify and edit their code and then re-run it WITHOUT closing and re-opening the file/program. This is a HUGE thing if you want to test code quickly. Here is the code that will make it so you can do this
Refresh Code:
Two things to be aware of. Refresh is calling another function, which is a "safe" loading function. If you entered something incorrectly in your new saved copy of main.lua, this function will not let you run it. Essentially you cannot crash your program just by trying to refresh the code, and it will tell you what's wrong with your new code. If it is safe to run, then it will execute and lay itself over the old code you have. However, this will not prevent you from doing something stupid (such as trying to call a function that was never defined in your code). This is a syntax check.
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::
OR for those who prefer a quicker shortcut method, use this code with or without the console and debug.debug() function:
In the console, while in debug mode, type "refresh()" and then "cont" to resume the game or use the shortcut defined using ctrl+r (I defined it as the left control + r, but use whatever you wish) and it will run. Here's a summary of what this will do:
When this is saved as the main.lua and then "refresh" is used, the function love.load() will be overwritten, perhaps with something new, but it does not execute because the code itself does not tell it to. If you wanted to, you could always run the debug and type "love.load()" to re-execute the function, but I would not recommend that. Here's the comparison:
When this code is saved and then "refresh" is used, Universe WILL become a brand new table, and all the old values will be lost. To avoid this, you can define your globals in the love.load function or through other functions, but not explicitly in main.lua .
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.
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.