I am trying to write a game, with a custom screen manager, I just started to make it. But i ran into a problem. My ScreenManager table / class is not recognized. It gives me the error
require "ScreenManager"
require "MenuScreen"
function love.load()
manager = ScreenManager.new()
screen = MenuScreen.new("Menu")
manager.addscreen(screen)
end
function love.draw()
manager.draw()
end
function love.update(dt)
manager.update()
end
local ScreenManager = {}
ScreenManager.__index = ScreenManager
function ScreenManager.new()
local self = setmetatable({}, ScreenManager)
self.screens = {}
self.currentScreen = nil
return self
end
function ScreenManager.addscreen(screen)
self.screens[self.getScreensSize()] = screen
if(self.getScreensSize() == 0) then
self.currentScreen = self.screens[0]
end
end
function ScreenManager.update(dt)
end
function ScreenManager.getScreensSize()
count = 0
for _ in pairs(self.screens) do count = count + 1 end
return count
end
function ScreenManager.draw()
if(self.currentScreen ~= nil) then
self.currentScreen.draw()
end
end
local MenuScreen = {}
MenuScreen.__index = MenuScreen
function MenuScreen.new(title)
local self = setmetatable({}, MenuScreen)
self.title = title
self.buttons = {{image = love.graphics.newImage("images/buttons/start_button.png"), x = 200, y = 200},
{image = love.graphics.newImage("images/buttons/exit_button.png"), x = 200, y = 300}}
return self
end
function MenuScreen.update(dt)
end
function MenuScreen.draw()
for k,v in pairs(self.buttons) do
love.graphics.draw(v.image, v.x, v.y)
end
end
That doesn't work because in screenmanager.lua you define ScreenManager as "local". Either you make it a global (not recommended) i.e. change the first line of screenmanager.lua to:
Or you make screenmanager.lua return a reference to ScreenManager (I would do that, leads to faster, less error-prone, more maintainable code). That means adding a return statement at the end of screenmanager.lua
local ScreenManager = {}
ScreenManager.__index = ScreenManager
function ScreenManager.new()
local self = setmetatable({}, ScreenManager)
self.screens = {}
self.currentScreen = nil
return self
end
function ScreenManager.addscreen(screen)
self.screens[self.getScreensSize()] = screen
if(self.getScreensSize() == 0) then
self.currentScreen = self.screens[0]
end
end
function ScreenManager.update(dt)
end
function ScreenManager.getScreensSize()
count = 0
for _ in pairs(self.screens) do count = count + 1 end
return count
end
function ScreenManager.draw()
if(self.currentScreen ~= nil) then
self.currentScreen.draw()
end
end
return ScreenManager
local ScreenManager = require "ScreenManager"
local MenuScreen = require "MenuScreen"
function love.load()
manager = ScreenManager.new()
screen = MenuScreen.new("Menu")
manager.addscreen(screen)
end
function love.draw()
manager.draw()
end
function love.update(dt)
manager.update()
end
I think you should read a Lua tutorial/book before you continue working on your game. Your problems are not LÖVE-specific but result from a lack of general Lua knowledge.
"self" does not magically appear. Either you pass it explicitly: