cannot use object from a other lua file
Posted: Sun Oct 20, 2013 11:12 am
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
This are my lua files, i am using love2d 0.8.0:
Main.lua
screenmanager.lua
MenuScreen.lua
Code: Select all
Main.lua:5: attempt to index global 'ScreenManager' (a nil value)
Main.lua
Code: Select all
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
Code: Select all
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
Code: Select all
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