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

### Can't use "require" for some reason?

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 "love.graphics" commands (typing "love.graphics" 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 "love.graphics.setBackgroundColor(1, 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)
love.graphics.setBackgroundColor(r, g, b)
end

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)
end

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?

raidho36
Party member
Posts: 1932
Joined: Mon Jun 17, 2013 12:00 pm

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

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.

pgimeno
Party member
Posts: 1896
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

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

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.audio, love.event, love.filesystem, love.font, love.graphics,
love.image, love.joystick, love.keyboard, love.math, love.mouse,
love.physics, love.sound, love.system, love.thread, love.timer, love.window,
love.data, love.touch, love.video
end

This abbreviates the LÖVE namespaces, for example love.graphics as lg, so you would write: lg.setBackgroundColor(r, g, b) which is shorter already.

JJSax
Prole
Posts: 28
Joined: Fri Apr 04, 2014 3:59 am

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

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.

### Who is online

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