function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = function (c, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end})
end
i have this class function that makes a class decoration.
I'm not quite sure where the error is coming from (I'm not all that familiar with the metatable, etc. stuff). However I can see one thing that's wrong:
Error: [string "main.lua"]:24: attempt to index local 'self' (a nil value)
stack traceback:
[string "main.lua"]:24: in function 'update'
[string "main.lua"]:33: in function 'update'
[string "boot.lua"]:314: in function <[string "boot.lua"]:302>
[C]: in function 'xpcall'
BlackBulletIV's suggestion fixes it and a black screen is rendered.
Error: [string "main.lua"]:24: attempt to index local 'self' (a nil value)
stack traceback:
[string "main.lua"]:24: in function 'update'
[string "main.lua"]:33: in function 'update'
[string "boot.lua"]:314: in function <[string "boot.lua"]:302>
[C]: in function 'xpcall'
BlackBulletIV's suggestion fixes it and a black screen is rendered.
function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = function (c, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end})
end
player = class()
function player:__init()
self.x = 320
self.y = 240
self.hspeed = 0
self.vspeed = 0
self.hp = 0
end
function player:update()
self.x = self.x + self.hspeed
self.y = self.y + self.vspeed
self.vspeed = 0
self.hspeed = 0
end
p = player()
function love.update(dt)
p:update()
end
I've created a test folder in the same folder where love.exe lives, and put the above code in a main.lua file inside test. I'm then able to run it fine by either dragging test over love.exe or via the command line: love test.