Can't use "require" for some reason?

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
Posts: 2
Joined: Fri Oct 18, 2019 6:40 pm

Can't use "require" for some reason?

Post by pakutto » Fri Oct 18, 2019 7:03 pm

I was beginning a simple game to test the waters of LOVE and Lua (I'm a beginner) - and I was told about the wonders of "require" function. It's very useful for shortening those super-long "" commands (typing "" every time becomes a bit tedious).

To make this simple, I'm going to take a chunk of code as an example.
I tried my main.lua file, down in my love.draw function, I used to have the typical ", 1, 1)" in my love.draw function - and it worked just fine.

Then, to shorten that, I made a separate file called "wrapper.lua" - which contains the following code:

Code: Select all

function BGColor(r, g, b), g, b)
Then, in my main.lua file, I have

Code: Select all

require "wrapper"
then, my draw function:

Code: Select all

function love.draw()
	BGColor(1, 1, 1)
Suddenly when I try to run the game - it gives an error, and says:
"attempt to call global 'BGColor' (a nil value)"

HOWEVER, when I replace my "require" function with all the code in "wrapper.lua" instead - it works wonderfully.

Yes, "wrapper.lua" is in the same folder as "main.lua".
And yes, I tried require ("wrapper") with the parenthesis instead - which also didn't work.
And just in case, I also renamed wrapper.lua to something random (lookadis.lua) in case it was the file name for some weird reason, but that didn't work either.

Can anyone let me know what I'm doing wrong?

User avatar
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can't use "require" for some reason?

Post by raidho36 » Fri Oct 18, 2019 11:13 pm

I tried doing this and it worked fine. It might help if you attach a minimal bundle that reproduces the problem. In the process of compiling it, you might find a solution, too.

User avatar
Party member
Posts: 2310
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can't use "require" for some reason?

Post by pgimeno » Sat Oct 19, 2019 5:31 am

Chances are that BGColor etc. are local. The scope of local variables doesn't extend beyond the file they are declared in.

I use this template (save it as ns.lua; usage is in the comment):

Code: Select all


Abbreviate LÖVE namespaces as locals

Written by Pedro Gimeno Fortea, donated to the public domain.

Usage (copy-paste this line to every file needing the namespaces):

local la,le,lfs,lf,lg,li,lj,lk,lm,lmo,lp,ls,lsys,lth,lt,lw,ld,lto,lv = require'ns'()


return function() return, love.event, love.filesystem, love.font,,
  love.image, love.joystick, love.keyboard, love.math, love.mouse,
  love.physics, love.sound, love.system, love.thread, love.timer, love.window,, love.touch,
This abbreviates the LÖVE namespaces, for example as lg, so you would write: lg.setBackgroundColor(r, g, b) which is shorter already.

Posts: 40
Joined: Fri Apr 04, 2014 3:59 am

Re: Can't use "require" for some reason?

Post by JJSax » Mon Oct 21, 2019 5:02 am

along with the other comments, make sure that your love folder in your appdata folder doesn't have a file by the same name.

Just fyi on how to get there just in case, (assuming you're using windows) click start and type "%appdata%" (no quotes). Then go to your love folder if available followed by your game. If there is a wrapper.lua there it will try to load and chances are it doesn't have that function in there. Spent way too long trying to figure that one out before.

Post Reply

Who is online

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