I've been working on something similar as well. I've made it slightly more robust. The biggest problem with what i have is that values doesn't get reset to nil if you removed them from the code and reload.
Say a = 5 is in the program when you start it.
love.graphics.print(a) -- will print 5.
Remove a = 5 . Save file.
love.graphics.print(a) -- will still print 5, "a" should now be "nil".
Close the love program and run again.
love.graphics.print(a) -- will crash because "a" is a "nil" value.
So hidden bugs might occur if you don't restart the program regularly.
A few pluses about the current implementation.
--It creates a "Push" file you can insert values into.
--It uses a protected function call, "pcall" so the program doesn't crash if you write an error. Instead it prints to console.
--Easy resetting of certain values if you don't set it in a function
Code: Select all
--main.lua
-- free values like this gets reset when the file is saved
a = 40
function love.draw( )
--stuff on screen
a = a + 0.1
love.graphics.line(0,a,100,a)
love.graphics.print("apple pie , "..applePIE.." , "..tostring(a))
love.graphics.print(love.filesystem.getLastModified("main.lua"),0,14)
end
function love.timer.sleep() end -- is bugged
function love.update( )
-- loading files should be wraped into functions.
if LastHotModified ~= love.filesystem.getLastModified("main.lua") then
LastHotModified = love.filesystem.getLastModified("main.lua")
ok , hotData = pcall(love.filesystem.load,"main.lua") -- Load program
if ok then
print("Loaded")
ok,err = pcall(hotData) -- Execute program
if not ok then
print("Execute error: "..err)
end
else
print(hotData)
end
end
-- Bellow is a push file so you can insert variables into the program
if LastPushModified ~= love.filesystem.getLastModified("push.lua") then
LastPushModified = love.filesystem.getLastModified("push.lua")
pushData = love.filesystem.load("push.lua")
pushData()
love.filesystem.write("push.lua" , "\n--[["..love.filesystem.read("push.lua"))
print("Load Push")
end
end
function love.keypressed( key )
if key == "o" then
love.system.openURL("file://"..love.filesystem.getSaveDirectory())
end
if key == "l" then
load()
end
if key == "escape" then
love.event.quit()
end
if key == "f" then
forceData = love.filesystem.load("main.lua")
forceData()
end
end
-- A place to store values, Does NOT automaticlly get updated when file is reloaded.
--
function load( )
applePIE = 7
scancode = " "
end
-- What happens below here only execute when the love file is opened
if loadFirst == nil then
loadFirst = 0
load()
love.filesystem.write("push.lua" , "\n--[[ Anything bellow this line will NOT be commented out automatically ]]")
LastPushModified = love.filesystem.getLastModified("push.lua")
end
Super Simple code reloading. Note that I use "love.filesystem.load" instead of "require". "love.filesystem.load" always loads the file.
Code: Select all
--main.lua
function love.update(dt)
mainData = love.filesystem.load("main.lua")
mainData()
end