Page 1 of 1

Refresh - without reloading the file!

Posted: Sat Aug 27, 2011 11:13 pm
by retupmoc258
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:

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
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::

Code: Select all

function love.keypressed(key, unicode)
	if key == "rctrl" then
		debug.debug()
	end
end
OR for those who prefer a quicker shortcut method, use this code with or without the console and debug.debug() function:

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
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:
  • 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.
I feel B needs some explanation. For example, suppose I have a table, Universe, that I have defined because I've been running the program and I have had the program enter stuff in through the code. Now I change the code by running refresh(). The table Universe, a global, will remain exactly the same as it was before, unless something in the main.lua file defines it explicitly. Here are two examples of main.lua code and what will happen.

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
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:

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
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.

Re: Refresh - without reloading the file!

Posted: Sun Aug 28, 2011 6:26 am
by genericdave
I'm assuming you've never heard of LICK then.

Re: Refresh - without reloading the file!

Posted: Mon Aug 29, 2011 3:09 pm
by retupmoc258
Nope. I am still pretty new to LOVE and LICK is not listed in the list of "libraries", so I didn't know it existed. I figured a tool like that would be there already if it existed. It would be even nicer if the programmers of LOVE actually implemented this into LOVE without extra stuff (such as a programming/debug mode)

Re: Refresh - without reloading the file!

Posted: Mon Aug 29, 2011 6:11 pm
by nevon
retupmoc258 wrote:Nope. I am still pretty new to LOVE and LICK is not listed in the list of "libraries", so I didn't know it existed. I figured a tool like that would be there already if it existed. It would be even nicer if the programmers of LOVE actually implemented this into LOVE without extra stuff (such as a programming/debug mode)
You mean debug.debug() ?

Re: Refresh - without reloading the file!

Posted: Tue Aug 30, 2011 12:43 am
by retupmoc258
No, talking about a runtime refreshing ability. LICK allows you to reload the code without restarting the program (such as dragging the .love file to the love shortcut or file). That was the point of this function/program was to make it so you wouldn't have to reload the file.