Reloading Files during Runtime

General discussion about LÖVE, Lua, game development, puns, and unicorns.
pilleman
Prole
Posts: 5
Joined: Wed Jun 12, 2013 8:16 pm

Reloading Files during Runtime

Post by pilleman »

Hello everybody,

in the moment i'm trying to build a little labyrinth game and I've been thinking about a "Reload-Function" to edit the game settings while the game is running.
So is it possible, to press a "refresh-button" during the game and reload updated files that are included in the main.lua (for example the config.lua), to change the movement of the player or the structure of the labyrinth?

I know i just could restart the Programm after editing the files, but I really want to try it this way.

Maybe someone of you has already dealt with a problem like this?
I would be very greatful for any help!


Regards :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Reloading Files during Runtime

Post by micha »

I haven't tried it but, you can open and execute code with

Code: Select all

love.filesystem.load(filename)()
The file has to be in the save directory for this, so this is probably most useful in development.

In this file you would probably redefine a function or change values of important variables.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Reloading Files during Runtime

Post by bartbes »

micha wrote: The file has to be in the save directory for this, so this is probably most useful in development.
That is not true.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Reloading Files during Runtime

Post by raidho36 »

Code: Select all

local module = require ( "module" )
-- some work here
module = require ( "updated_module" )
pilleman
Prole
Posts: 5
Joined: Wed Jun 12, 2013 8:16 pm

Re: Reloading Files during Runtime

Post by pilleman »

micha wrote:I haven't tried it but, you can open and execute code with

Code: Select all

love.filesystem.load(filename)()
yeah but can i do that with the config.lua during the runtime?
i mean i don't want to load a file, i want to reload it after i've edited it.
raidho36 wrote:

Code: Select all

local module = require ( "module" )
-- some work here
module = require ( "updated_module" )

sorry but i'm not that used to lua yet and i'm having some problems understanding your idea :(
i'm really sorry, could you maybe explain it a litte bit more detailed for me?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Reloading Files during Runtime

Post by bartbes »

pilleman wrote: yeah but can i do that with the config.lua during the runtime?
i mean i don't want to load a file, i want to reload it after i've edited it.
There is no reloading without loading ;). (Seriously though, just loading it again means you're reloading.)
raidho36 wrote:

Code: Select all

local module = require ( "module" )
-- some work here
module = require ( "updated_module" )
Unless you're suggesting he changes the filename every single time, this won't work.
pilleman
Prole
Posts: 5
Joined: Wed Jun 12, 2013 8:16 pm

Re: Reloading Files during Runtime

Post by pilleman »

ok so this it what i've done so far:

Code: Select all

function love.keypressed(key)
   .
   .
   .
   elseif key == "x" then
	love.filesystem.load(conf.lua)
    end
end
but i get the following error code when i press x on the keyboard:

Code: Select all

Error

main.lua:85: attempt to index global 'conf' (a nil value)

Traceback

...
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Reloading Files during Runtime

Post by micha »

Try this:

Code: Select all

love.filesystem.load('conf.lua')()
Two things: The filename is a string. And after the love.filesystem.load you have to put two brackets, the first one to load the chunk and the second one to run it.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Reloading Files during Runtime

Post by raidho36 »

Unless you're suggesting he changes the filename every single time, this won't work.
You guys have some serious problems with reading pseudocode and using your imagination.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Reloading Files during Runtime

Post by Plu »

Which part is the pseudo-code? Because the require statement doesn't work like that. It will not load the same file twice.

As Bartbes said; the only way you can use require to reload the file is if you keep changing the name, otherwise the call will be ignored. Which sounds like reasonable advice to give. Unless you meant the require function was also pseudo-code, anyway. Which wasn't really clear from your post.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests