local self.x

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.

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.

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.

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