Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

local self.x

edited July 2013 in General Posts: 105

When I have a class and i want a class variable to be local, i cant run the code. Why? Have i missed something?


function test:setup()
local self.x=400

function test:draw()
sprite("Platformer Art:Block Grass",self.x,400)

I load it from main but it generates an error.



  • edited July 2013 Posts: 305

    This is the explanation of local var
    And in this case, you could not use local, just self.x. Because self designed current instance of object and .x is a sugar for self["x"].

    If you know javascript, local correspond at "var" and self at "this".

    ... I'm not good for a real explanation.

  • IgnatzIgnatz Mod
    Posts: 5,396

    self.x will be different for each instance of the class, ie each "test" object has its own x, but it is not local, because not only can you use self.x in any of Test's functions (as long as they are defined with a colon), but you can get hold of it from outside the class, eg

    print(a.x) --> prints 400

    Nevertheless, I think it is exactly what you want in this case.

  • Posts: 2,161

    self.x is already local because self is local. It is a local reference to the calling instance.

  • Posts: 105


Sign In or Register to comment.