Question

I was trying to access a member of 'self', see following:

function BasePlayer:blah()
    --do blah
end
function  BasePlayer:ctor(tape)      --constructor.
    self.tape = tape                 --'tape' is a lua table, and assigned to 'self.tape'.
    dump(self.tape)                  --i can dump table 'self.tape',obviously it's not nil.
    self.coreLogic = function(dt)    --callback function called every frame.
        echoInfo("%s",self)          --prints self 'userdata: 0x11640d20'.
        echoInfo("%s",self.tape)     -- Here is my question: why self.tape is nil??
        self:blah()                  --even function too??
    end
end

So my question is, in my callback function 'coreLogic', why 'self.tape' is nil but self is
vaild?
And functions can't be called either.
I'm really confused:S

Was it helpful?

Solution

When you define a function using the :, the implicit parameter is created on its own. When you are defining the function coreLogic, you'd need to pass it as the first argument:

self.coreLogic = function( self, dt )

and that would be the same as:

function self:coreLogic(dt)

self in itself doesn't exist.


Basically,

function BasePlayer:blah()

is same as writing:

function BasePlayer.blah( BasePlayer )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top