开发者

Lua: Table of functions

开发者 https://www.devze.com 2023-03-03 23:37 出处:网络
I am trying to store different functions开发者_JAVA百科 in a table, but somehow it won\'t work the way I thought it would. Here is my \'code\'

I am trying to store different functions开发者_JAVA百科 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消