简体   繁体   中英

Lua: Table of functions

I am trying to store different functions in a table, but somehow it won't work the way I thought it would. Here is my 'code'

fn_table = { aFun1=print, aFun2=self:getSpeedLevel, aFun3=.... }

The problem now is that I can do this with the built in functions like print , assert and so on but it wont work with the other functions I've got.

I get the error: "... function arguments expected near '}'

Is it possible to store these function as well?

aFun2 = self:getSpeedLevel is a syntax error and that is what Lua complains about. Try aFun2 = getSpeedLevel or aFun2 = self.getSpeedLevel (assuming that self is a table). The Object-Oriented Programming chapter in the PiL book has more examples of functions being stored in tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM