Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Timer.delayedCall parameters nil — Gideros Forum

Timer.delayedCall parameters nil

AlaaEddineAlaaEddine Member
edited October 2013 in Bugs and issues
Hello, I have this code

GameScene = gideros.class(Sprite)


function GameScene:init()
Timer.delayedCall(15,self.test,{param1 ="p1", param2 = "p2"})
end


function GameScene:test(args)
print(args.param1)
print(args.param2)
end

when I run it I have an error "attempt to index local 'args' (a nil value)".
but when I change it like follows it works

GameScene = gideros.class(Sprite)
function GameScene:init()
Timer.delayedCall(15,test,{param1 ="p1", param2 = "p2"})

end

function test (args)
print(args.param1)
print(args.param2)
end

any suggestion??

Comments

  • function test(args)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Yes that is the way Lua OOP works
    if you define method through colon (:), it means it accepts first argument as self automatically.
    So you can do stuff like that:
    local s = Sprite.new()
    s:setPosition(100, 100)
    Which underneath is actually this:
    local s = Sprite.new()
    Sprite.setPosition(s, 100, 100)
    Note the difference with calling function with colon : or dot .

    So the correct way to handle this situation would be either like this:
    function GameScene:init()
        self.param1 = "p1"
        self.param2 = "p2"
        Timer.delayedCall(15,self.test,self)
    end
     
     
    function GameScene:test()
        print(self.param1)
        print(self.param2)
    end
    or like this:
    function GameScene:init()
        Timer.delayedCall(15, function()
            self:test({param1 ="p1", param2 = "p2"})
        end)
    end
     
     
    function GameScene:test(args)
        print(args.param1)
        print(args.param2)
    end
  • Thank you, I understand the difference now
Sign In or Register to comment.