Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - Add function calls with parameters to a stack and call them later

Tags:

lua

lua-table

I know that I can store function references in tables and call them with parameters like described in the first answer here

Lua - Execute a Function Stored in a Table

But I need to store the parameters in the table for each call. How can I do that?

To explain what I want to do. I want to write a steering behaviors class. If you are calculating the steering force you can call different functions like seek(target) or pursuit(target).

I want to be able to "collect" all function calls and execute them at the end (loop over the table and execute each function with the stored parameter) or cancel everything.

Is that possible?

like image 664
burli Avatar asked Dec 16 '25 12:12

burli


1 Answers

Another (possibly cleaner) alternative:

function xxx(s1,s2,s3)
  print(s1,s2,s3)
end

t = {}
t[#t+1] = { xxx, {'a','b','c'}}
t[#t+1] = { xxx, {'x','y','z'}}

for _,f in ipairs(t) do
  f[1](table.unpack(f[2]))
end
like image 170
tonypdmtr Avatar answered Dec 19 '25 05:12

tonypdmtr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!