I'm trying to implement a simple class in Lua, but I'm getting the following error when I try to access a method of a class:
data/lua/startup.lua:11: attempt to call method 'LoadBG' (a nil value)
I can't seem to figure out what I am doing wrong. From what I can tell, I am defining everything in the same way as it shown
here, but I'm getting that error. Here's my relative code:
--c_UIElement.lua
UIElement={}
UIElement_mt={}
function UIElement:create(posX,posY,width,height,parentEle)
-- Create the new instance of the UIElement
local new_inst={
pointer=UIElement_Create(posX,posY,width,height,parentEle),
x=posX, y=posY,
w=width, h=height,
parent=parentEle,
bg="",
border=""
}
setmetatable(new_inst,UIElement_mt) -- Set the metatable for the UIElement
return new_inst
end
--Load the background for the element from filename
function UIElement:LoadBG(filename)
UIElement_LoadBG(self.pointer,filename)
end
--Set the border for the element
function UIElement:SetBorder(border)
UIElement_SetBorder(self.pointer,border)
end
UIElement_mt.__metatable=false
UIElement_mt.__newindex=function(ele,key,value)
error("Invalid index: \"" + key + "\"")
end
--startup.lua
dofile("data/lua/c_UIElement.lua")
ele=UIElement:create(10,10,120,100,nil)
ele:LoadBG("images/ui/bg.png")
ele:SetBorder("wood")
Help please? I'm still pretty new to Lua, so it's probably just something stupid that I forgot.
EDIT: Darn, no source tags for Lua?
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html