I've wrangled a crazy way to have your cake and eat it too.
do
local state = 0
function render()
drawLine(0,5,0, 0,5,50*math.sin(state))
state = state + math.pi * 0.03
end
end
To fiddle with the "state" variable from the outside:
name,val = debug.getupvalue(render, 1)
print(name) -- state
print(val) -- state's value
debug.setupvalue(render, 1, 0) -- set state to 0
To find out how many upvalues are available:
dt = debug.getinfo(render)
print(dt.nups)
If you want to redefine render without disturbing state, you need to backup state and make a new closure with the new definition of render and the restored state:
do
local savedstate = {debug.getupvalue(render,1)}
local state = savedstate[2]
function render()
local h = 5
drawLine(0,h,0,50 * math.sin(state), h,0)
state = state + math.pi * 0.06
end
end
I don't think its possible (or I don't know how) to redefine a function inside a closure. So just make a new closure and restore its state.