Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append to function lua

I have a function I want to add to dynamically as the program runs.

Let's say I have function Foo:

function foo()
    Function1()
    Function2()
    Function3()
end

and I want to change Foo() to:

function foo()
    Function1()
    Function2()
    Function3()
    Function4()
end

later in the program. Is there any way to do this?

like image 281
user3762069 Avatar asked Jun 21 '14 03:06

user3762069


1 Answers

Just do it. The code that you wrote works fine. Functions in Lua can be redefined as desired.

If you don't know what foo does, you can do this:

do
  local old = foo
  foo = function () old() Function4() end
end

Or perhaps it is clearer to use a table of functions:

local F={ Function1, Function2, Function3 }

function foo()
  for i=1,#F do F[i]() end
end

Latter, do

F[#F+1]=Function4

and you don't need to redefine foo.

like image 178
lhf Avatar answered Sep 21 '22 15:09

lhf