Need to load files more than once. HOW?

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
User avatar
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

Need to load files more than once. HOW?

Post by JME »

I have been stuck with this problem since over a year ago. The problem that I cannot go to files that i have left when I am running the game. When I require a file, then I want to require another file from that file. Then I want to navigate back to the last file, but I do not know how to.
I use luas require every time I navigate to a new file. If I try to require a file that is already required, the file I am on just reload itself.

In this example I want to navigate free between different files.
main.lua:

Code: Select all

function love.load()
end

function JUSTLOADTHEFILE(name)
	state = {}--one empty table here instead of one in every file. smart?
	local path = require(name)
	load()--the load function will run in the file
end

function drawsomething()
	love.graphics.setColor( 255, 111, 155, 255 )
	love.graphics.rectangle( "fill", 400, 0, 400, 600 )
end

function drawsomethingelse()
	love.graphics.setColor( 50, 90, 255, 255 )
	love.graphics.rectangle( "fill", 0, 300, 800, 300 )
end

function love.draw()
	drawsomething() --half screen pink
end

function love.mousepressed( x, y, button )
	if button == "l" then
		JUSTLOADTHEFILE("file")
	elseif button == "r" then
		JUSTLOADTHEFILE("anotherfile")
	end
end
file.lua:

Code: Select all

function load()
	love.graphics.setBackgroundColor( 255, 255, 0 )
end

function love.draw()
	drawsomethingelse() --this work
end

function love.mousepressed( x, y, button )
	if button == "l" then
		JUSTLOADTHEFILE("anotherfile")
	elseif button == "r" then
		JUSTLOADTHEFILE("main") --I think this is not going to work because it is the main.lua
	end
end
anotherfile.lua:

Code: Select all

function load()
	love.graphics.setBackgroundColor( 255, 255, 255 )
	graything = 150
end

function love.update(dt)
	graything = graything + 33*dt
	if graything > 600 then
		graything = 150
	end
end

function love.draw()
	love.graphics.setColor( 0, 0, 0, 130 )
	love.graphics.rectangle( "fill", 200, graything, 400, 300 )
end --this "graything" is just to prove that this file is loaded again instead of the required file

function love.mousepressed( x, y, button )
	if button == "l" then
		JUSTLOADTHEFILE("main")--also main.lua does not have a function called just "load"
	elseif button == "r" then
		JUSTLOADTHEFILE("file")
	end
end
I want to navigate between them as in a triangle. I think the wrong is that the "JUSTLOADTHEFILE" function is still running when I try to go back to that file I command it from.
As said I have had this problem in over a year and it is providing me from continue with learning löve.
Please help me someone.
and no, I do not know how to upload a .love file. I have tried many times.
If you understand me or not, that is not my problem.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Need to load files more than once. HOW?

Post by bartbes »

Well it doesn't help that it is bad design to override the love callbacks in every file, but I suppose someone else will come along and lecture you about that.

In your case, it seems you want to use [wiki]love.filesystem.load[/wiki] instead, if you pass that a filename, it returns a function representing that file, and that can of course be run again and again. Unlike require, it's not cached.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need to load files more than once. HOW?

Post by Robin »

bartbes wrote:Well it doesn't help that it is bad design to override the love callbacks in every file, but I suppose someone else will come along and lecture you about that.
Or overriding globals like load in each module.

Honestly I think it'd be better to do something like:

main.lua:

Code: Select all

local state

function love.load()
    switch_state 'somestate'
end

function switch_state(sname)
    state = require(sname)
    state.load()
end

function love.draw()
        state.draw()
end

function love.mousepressed( x, y, button )
        state.mousepressed(x, y, button)
end
(and add other callbacks)

somestate.lua:

Code: Select all

local M = {}
function M.load()
	love.graphics.setBackgroundColor( 255, 255, 0 )
end

function M.draw()
	--blah
end

function M.mousepressed( x, y, button )
	if button == "l" then
		switch_state 'someotherstate'
	end
end

return M -- very important
This will work just peachy. (Just remember that 'main' isn't a state ;) You can also use a library for managing states (<insert list of libraries here>), they tend to have interesting features like stacking states and more state callbacks like load().
Help us help you: attach a .love.
User avatar
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

Re: Need to load files more than once. HOW?

Post by JME »

Thank you so much for your help Robin!
It worked yes it worked!
I am just so glad because I have had that problem in a very long time and you just solved it for me.

When I tested it on another test game with images, it did not work first. The game window just closed right after I opened it. A very strange problem I have never had earlier. Then I noticed that I have to load the images from main.lua instead and that worked.

So now it is working! Thanks again for your help :ultraglee:
If you understand me or not, that is not my problem.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 8 guests