cannot use object from a other lua file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
l19htn1n9
Prole
Posts: 2
Joined: Sun Oct 20, 2013 11:05 am

cannot use object from a other lua file

Post by l19htn1n9 »

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

Code: Select all

Main.lua:5: attempt to index global 'ScreenManager' (a nil value)
This are my lua files, i am using love2d 0.8.0:
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
screenmanager.lua

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
MenuScreen.lua

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
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: cannot use object from a other lua file

Post by Hexenhammer »

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:

Code: Select all

ScreenManager = {}
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

Code: Select all

return ScreenManager
And changing the first line of Main.lua to:

Code: Select all

local ScreenManager = require "ScreenManager"
l19htn1n9
Prole
Posts: 2
Joined: Sun Oct 20, 2013 11:05 am

Re: cannot use object from a other lua file

Post by l19htn1n9 »

Thank you, it worked great, only now i get the error that i can't reference the self var.

Code: Select all

.\ScreenManager.lua:12 : attempt to index global 'self' (a nil value)
ScreenManager:

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

return ScreenManager
Main:

Code: Select all

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
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: cannot use object from a other lua file

Post by Hexenhammer »

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:

Code: Select all

function ScreenManager.draw(self)
   if(self.currentScreen ~= nil) then
      self.currentScreen.draw()
   end
end

Code: Select all

function love.draw()
   manager.draw(manager)
end
Our you use the shorthand form for OO-like syntax

Code: Select all

function ScreenManager:draw()
   if(self.currentScreen ~= nil) then
      self.currentScreen.draw()
   end
end

Code: Select all

function love.draw()
   manager:draw()
end
Not that this code is functionally identical to the one with explicit self. a:b() is always just a shorthand for a.b(a).
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests