Can't call a function directly in my class, but i can call this same function with timer.performwithdealy

Hi,

my “main.lua” create an instance of my “Action.lua” class,

“init” it and

call the “start” function

like below :

display.setStatusBar( display.HiddenStatusBar ) local classes = require ("src.classes.Action") local myClass = classes:new() myClass:init(20, 3000) myClass:start() 

my class “Action.lua” is define like below :

local Action = {} local Action\_mt = {\_\_index = Action} function Action:new() return setmetatable({},Action\_mt) end function Action:init(speed, pauseTime) self.speed = speed self.pauseTime = pauseTime self.actionTimer = nil self.name="MyName" print("class init") end function Action:doAction() print("doAction") print(self.name) end function Action:start() print("start") self.actionTimer = timer.performWithDelay( 10, function() self:doAction() end,0) end function Action:stop( ... ) -- body end return Action

My problem is :

if i call directly the  “doAction()” function in the  “start()” function,

function Action:start() print("start") self.doAction() end 

 i have got this error ->

attempt to index local ‘self’ (a nil value)

[…]src/classes/Action.lua:19: in function ‘doAction’

But  if i call the “doAction()” function with “timer.performwithdelay” in the “start” function, 

it’s works… 

print(self.name) return me “myName” … 

Why ?

Thank you

It’s a dot vs. colon issue. In your performWithDelay call, you’re using self:doAction() (with a colon), while in your direct call, you’re using self.doAction() (with a dot). Without a colon, there isn’t a self argument unless you explicitly specify it.

  • C

thank you !

i need some glasses ^^

It’s a dot vs. colon issue. In your performWithDelay call, you’re using self:doAction() (with a colon), while in your direct call, you’re using self.doAction() (with a dot). Without a colon, there isn’t a self argument unless you explicitly specify it.

  • C

thank you !

i need some glasses ^^