Reputation: 97
I have created a render Lua file, and created multiple objects, I have copied a small snippet of the file. This is the Object (class) that I am having issues with. When the newBox
function is created the this:show()
is causing the error in the Box:show()
when the function registerBox()
is called. An attempt to call nil
However I have played around with the syntax a bit (stretching one line into multiple lines to figure what part of the line is causing the error) I know for a fact that the function is not causing the attempt to call nil
, it is the this.x
or any of the this.<var>
Am I not passing the variables correctly? Remember this is only a snippet, all of the functions called are left out so I don't have to post 750 lines of code.
Also I have commented on a few things to help you understand what I mean a little more, and so you don't need to read all of the code line by line as much.
-- Box Class
function newBox(x,y,z,w,h,t,b,c)
local this = setmetatable({}, Box)
this.x = x
this.y = y
this.z = z
this.w = w
this.h = h
this.t = t
this.b = b
this.c = c
this:show()
this.v = true
return this
end
This is called when a new object is created for example local obj = newBox( ... )
function Box:render()
rasterBox(this.x,this.y,this.w,this.h)
renderBox(this.x,this.y,this.w,this.h)
end
This renders the box, don't worry this is all working fine...
function Box:move(x,y,z)
this:hide()
this.x = x
this.y = y
if z then
this.z = z
end
this:show()
end
function Box:resize(w,h)
this:hide()
this.w = w
this.h = h
this:show()
end
function Box:pattern(t,b,c)
this:hide()
this.t = t
this.b = b
this.c = c
this:show()
end
The code above should not have any issues, SHOULD...
function Box:show()
registerBox(this.x,this.y,this.z,this.w,this.h,this.t,this.b,this.c) -- CALL NIL ERROR
this.v = true
this:render()
end
The function above is called when the object is created > this:show()
it is NOT the RegisterBox
function it is the actual this.[var]
parameters.
Below is the rest of the code. not sure if the code below is causing any issues though.
function Box:hide()
unregisterBox(this.x,this.y,this.z,this.w,this.h)
this.v = false
this:render()
end
function Box:getPosition()
return this.x,this.y,this.z
end
function Box:getPattern()
return this.t,this.b,this.c
end
function Box:getSize()
return this.w,this.h
end
function Box:destroy()
this:hide()
this = nil
end
Upvotes: 0
Views: 325
Reputation: 13859
As @user1095108 say, there is no this in lua, hidden, this-like, parameter is called self
All OOP-like mechanic is greatly described here, try to play with easy examples, to understood, how it works, it's pretty easy. Shortly, colon is a syntax sugar for extra param at function declaration, or call time. Param is called self, and it's an object, at left part of function call. Reference to it is passed to function.
Also it useless to do
function Box:destroy()
this:hide()
this = nil -- Here you assign nil to local variable, passed as parameter.
end
If you wanna free some object, you should ensure, that all references to it are unreferenced, including an object from caller. Arguments will be free automatically, cause they are local
Upvotes: 1