I have some simple programming experience but am new to Lua and Love2d. I created a simple counting program to get familiar with the basics. I made 3 functions:
timepiece:create () - sets a counter, called in the load function.
timepiece:increment () - adds 1 to the counter, called in update.
timepiece:draw () - displays the current value, called in draw.
The first function is good and if I use other methods to increment and draw the program works as intended. But if I try to call either of my other two functions it complains that the methods are nil values. I've tried a dozen varients and I simply can't figure out why create works but the others don't. I feel like I'm missing something very basic, so if someone could clue me in I'd appreciate it.
Here's the working version of the code with the lines that cause the crash commented out:
Code: Select all
function love.load ()
t1 = timepiece:create ()
end
function love.update ()
--t1:increment ()
t1.time = t1.time + 1
end
function love.draw ()
love.graphics.clear ()
--t1:draw ()
love.graphics.print (t1.time)
end
timepiece = {}
function timepiece:create ()
local object = {time = 1}
return object
end
function timepiece:increment ()
self.time = self.time + 1
end
function timepiece:draw ()
love.graphics.print (self.time)
end