Loading other files

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
Flammable Apple
Prole
Posts: 2
Joined: Tue Dec 31, 2024 7:42 pm

Loading other files

Post by Flammable Apple »

So, I might just be stupid but I'm working on a project and for organization I made multiple lua files. On of them, load.lua contains a function called fontSetup() and a few other functions on main.lua I've got:

Code: Select all

function love.load()
    love.filesystem.load("load.lua")
    love.filesystem.load("draw.lua")
    love.filesystem.load("variables.lua")
    
    fontSetup()
    physicsSetup()
    windowSetup()
end
Later I call fontSetup() and it returns "nil".

I might just be stupid or not looking hard enough but I can't figure out why love.filesystem.load("load.lua") won't work.
User avatar
pgimeno
Party member
Posts: 3706
Joined: Sun Oct 18, 2015 2:58 pm

Re: Loading other files

Post by pgimeno »

Flammable Apple wrote: Tue Dec 31, 2024 7:53 pm So, I might just be stupid but I'm working on a project and for organization I made multiple lua files. On of them, load.lua contains a function called fontSetup() and a few other functions on main.lua I've got:

Code: Select all

function love.load()
    love.filesystem.load("load.lua")
    love.filesystem.load("draw.lua")
    love.filesystem.load("variables.lua")
    
    fontSetup()
    physicsSetup()
    windowSetup()
end
Later I call fontSetup() and it returns "nil".

I might just be stupid or not looking hard enough but I can't figure out why love.filesystem.load("load.lua") won't work.
Hi, welcome to the forums.

In order for your file to define the functions you want, you need to run it. But as stated in the love.filesystem.load documentation, "Loads a Lua file (but does not run it)." (bolding added).

Just write it like this:

Code: Select all

    love.filesystem.load("load.lua")()
    love.filesystem.load("draw.lua")()
    love.filesystem.load("variables.lua")()
Note the () at the end. Since it returns a function, adding () forces a call to the function, which effectively runs the file. Alternatively, you could do it like this:

Code: Select all

    local load = love.filesystem.load("load.lua")
    load()
and similarly for the other files, but it's simpler if you just append the (), isn't it?
Flammable Apple
Prole
Posts: 2
Joined: Tue Dec 31, 2024 7:42 pm

Re: Loading other files

Post by Flammable Apple »

Okay, thank you.
User avatar
dusoft
Party member
Posts: 752
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Loading other files

Post by dusoft »

Just use the standard require function.
https://www.lua.org/pil/8.1.html

Code: Select all

require "load"
etc.
Post Reply

Who is online

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