how do I load lua 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
xThomas
Prole
Posts: 16
Joined: Tue May 02, 2017 2:43 am

how do I load lua files?

Post by xThomas »

I know its a dumb question but I've been looking it up and don't understand how to do this :x

I've tried

love.filesystem.load("button.lua()")
love.filesystem.load("button.lua")
love.filesystem.load("button.lua"())
love.filesystem.load(button.lua)
love.filesystem.load("class.lua")

etcetera

--=====

In a folder, I have three lua files: main.lua, button.lua, class.lua

I run it by dragging and dropping on Love2d. I don't really know what I'm supposed to do.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: how do I load lua files?

Post by grump »

xThomas wrote: Mon Oct 16, 2017 1:18 amlove.filesystem.load("button.lua")
That should work and return a chunk you can execute:

Code: Select all

local button, errmsg = love.filesystem.load("button.lua")
assert(button, errmsg) -- prints error message if an error occured
button() -- execute the lua file
 
This works, but what you probably want is the require function:

Code: Select all

-- load button.lua from the current folder, execute it and assign the result to button
local button = require("button")
That's how you usually load modules, and it even makes sure the file gets loaded only once.
Last edited by grump on Mon Oct 16, 2017 2:23 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: how do I load lua files?

Post by zorg »

Have you read the wiki page on [wikilove.filesystem.load[/wiki] yet? It clearly explains everything.

Also, you don't need to use that if you don't want to, you can always just require in files:

Code: Select all

local someModule = require(path.to.somemodulewithoutextension)
-- or if it contains globals only, though it's bad practice:
require(someothermodulewithoutextension)
Do note that you need to use dots in require, and you need to not write the file extension.

But if you do want to use l.fs.load, then:

Code: Select all

local Button = love.filesystem.load('button.lua')()
is the quickest solution, though it's not the safest method; the wiki page explains it in more detail.

(Also curse you grump, if there hadn't been a keyboard shortcut that logged me out, my post would have been the first : P)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
xThomas
Prole
Posts: 16
Joined: Tue May 02, 2017 2:43 am

Re: how do I load lua files?

Post by xThomas »

Thx guys it works now :) ps I did check the wiki, I am just extraordinarily bad at understanding documentation
brogrammer
Prole
Posts: 15
Joined: Tue Oct 17, 2017 11:26 pm

Re: how do I load lua files?

Post by brogrammer »

use require not loadfile
Post Reply

Who is online

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