lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thursday 07, Jonathan wrote:
> I would like to be able to change the following lua function at run time:
>
> function new_foo()
>     local foo_update = function()
>     -- do something
>     end
>
>     return { foo_update = foo_update }
> end
>
> foo.foo_update() -- foo was created during initialization
>
> Is there any way to change foo_update during run time? If not, then is
> there another way to create objects which allows me to change them at run
> time?

You could use "dofile" to re-load the code that you want to update.

foo.lua:
function foo_update()
  print("foo version 1")
end
return { foo_update = foo_update }

main.lua:
-- initialize foo object.
foo = dofile("foo.lua")
foo.foo_update() -- call first version.
-- make changes to foo.lua
foo = dofile("foo.lua") -- load new version
foo.foo_update() -- call new version.


-- 
Robert G. Jakabosky