Page 1 of 1

filesystem.load not working?

Posted: Tue Sep 23, 2014 1:49 am
by Blank
Hi there. My name is Blank, and I recently found LÖVE and I really thought it looked neat and simple, seeing as I have no past experience with LUA at all, and have only coded in GML. I am currently following a tutorial on youtube for LÖVE and I came across this problem with the code. I swear I have written everything correctly thus far, and I don't understand why this is not working.

I have set up an entities file for all of my projects objects, and have them loaded into the game when needed. Like the tutorial, I made a box object, suitably calling it 'box.lua' and giving it some properties.

part of main.lua

Code: Select all

function love.load()
	require ('entities')
	love.graphics.setBackgroundColor( 198,241,255 ) -- Red / Green / Blue
	imagePlayer = love.graphics.newImage("sprites/mero1.png")
	imageTiles = love.graphics.newImage("tiles/testtiles.png")
	
	local boxEnt = ents.Create( "box", 128, 128)
end
As you can see, I have 'boxEnt' loading my object from my 'entities.lua' file.

part of entities.lua

Code: Select all

ents = {}
ents.objects = {}
ents.objpath = "objects."

local register = {}
local id = 0

function ents.Startup()
	register["box"] = love.filesystem.load( "objects.box.lua" )
end

function ents.Derive(name)
		return love.filesystem.load( ents.objpath .. name .. ".lua" ) ()
end
The game loads fine, but like the tutorial, I have set up the script to throw me an error message in the command window if it does not load, and upon loading the game it tells me "Error: Entity box does not exist!" even though it DOES exists.


I have checked my filenames, directory names and everything and I just can't seem to figure this out.

Re: filesystem.load not working?

Posted: Tue Sep 23, 2014 6:59 am
by bartbes
It seems you forgot to call the function returned by load when trying to load objects.box.lua, is that the problem?

Re: filesystem.load not working?

Posted: Wed Sep 24, 2014 8:43 am
by artofwork
1st problem is you never call ents.Startup() in love.load

Code: Select all

function love.load()
	require ('entities')
	love.graphics.setBackgroundColor( 198,241,255 ) -- Red / Green / Blue
	imagePlayer = love.graphics.newImage("sprites/mero1.png")
	imageTiles = love.graphics.newImage("tiles/testtiles.png")
	ents.Startup()
	local boxEnt = ents.Create( "box", 128, 128)
end

2nd problem is you need to change this

Code: Select all

register["box"] = love.filesystem.load( "objects.box.lua" )
to this

Code: Select all

register["box"] = love.filesystem.load( "/objects/box.lua" )
Now you get an error which is a good thing because it tells you that the change you made found what you were looking to load

Code: Select all

entities:26: attempt to call an upvalue 'register' (a table value)
To fix that you need to change this

Code: Select all

local ent = register(name) ()
to this

Code: Select all

local ent = register[name] ()
Then you get a new error

Code: Select all

entities: 13: attempt to call a nil value
In order to fix that we look at 13 which is

Code: Select all

return love.filesystem.load( ents.objpath .. name .. ".lua" ) ()
so we leave 13 alone, but make changes to the thing assigned to ents.objpath

from this

Code: Select all

ents.objpath = "objects."
to this

Code: Select all

ents.objpath = "/objects/"
Now we get a new error

Code: Select all

/objects/box.lua: 19: attempt to perform aithmetic on local 'dt' (a nil value)
We look at 19 in box.lua and see

Code: Select all

self.y = self.y + 32*dt
The problem isn't in 19 of box.lua its on line 18 of main.lua


The reason we know the error is in on line 18 of main.lua is because of the code on box.lua line 18

Code: Select all

function ent:update(dt)
main.lua line 18

Code: Select all

	ents.update(dt)
So we change line 18 of main from this

Code: Select all

	ents.update(dt)
to this

Code: Select all

	ents:update(dt)

And there are no more errors, Good Luck!