1

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

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Wayne Dimart
  • 85
  • 1
  • 8
  • Pure Lua does not have such a thing as `classes` and `self`. In pour Lua with You provided code sample `self` is a global table. Could You please, clarify, what framework or library are You using for that? Related questions: [texas-instruments](http://stackoverflow.com/q/7353035/1150918), sample from [Love2D](http://stackoverflow.com/q/23275262/1150918), sample from [Lua classlib](http://stackoverflow.com/q/14068944/1150918), ..., ..., ... – Kamiccolo Apr 25 '14 at 11:24
  • @Kamiccolo The implicit `self` parameter for the colon syntax is part of vanilla Lua. See [section 3.4.10 of the reference manual](http://www.lua.org/manual/5.2/manual.html#3.4.10): `The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. ` – ComicSansMS Apr 25 '14 at 12:03
  • @ComicSansMS, sorry, my bad. Misread the question. Still, what does bug me --- according [tutorial](http://lua-users.org/wiki/ObjectOrientationTutorial), dot syntax is used for contruction and methods association. – Kamiccolo Apr 25 '14 at 12:27
  • @Kamiccolo The colon syntax simply adds an implicit self parameter. That's all it does. How it's used is up to the code. – Colonel Thirty Two Apr 25 '14 at 15:40
  • I'm using [quick-cocos2d-x](http://quick-x.com/) framework. – Wayne Dimart Apr 26 '14 at 01:37

1 Answers1

3

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 )
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Did you mean to put the `function` keywords in your second section? Because those two examples aren't the same with it. – Colonel Thirty Two Apr 25 '14 at 15:41
  • Thanks for your answer, but the things are, this callback is called from C.Secondly, and the most weird thing is,I can use this method in other lua file. So now I'm gonna check it out what the hell is going wrong here. Onece I found out, I'll post here. – Wayne Dimart Apr 26 '14 at 01:36
  • 2
    OK I found the problem now. Turns out it was recycled by engine, thus you won't get member function or variable. – Wayne Dimart Apr 26 '14 at 03:18