Error while trying to call a class method: attempt

2020-04-16 06:36发布

I'm creating a lua script that should run on the TI-Nspire calculator. The problem is that while running my script I get the error Attempt to index local 'self' (a nil value) when the button:activate() method is called. The parser says the error is in the 8th line in the code below. The problematic code is as follows:

button = class(view)

function button:init()
    self.selected = false
end

function button:activate()
    self.selected = true
end

I call the activate function like this:

item = button()
local action = "activate"
local arguments = {}
item[action](unpack(arguments))

I am aware the class() function doesn't exist in "stock" Lua, it's a function available in the TI-Nspire Lua implementation. You can find its definition and usage here.

1条回答
够拽才男人
2楼-- · 2020-04-16 07:02

obj:methodname(args) is sugar for obj.methodname(obj,args). So, if you want to use the syntax item[action](unpack(arguments)), you need to use item[action](item,unpack(arguments)). Otherwise, try item:activate(unpack(arguments)) if you can use method explicitly.

查看更多
登录 后发表回答