questions about using "require"

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
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

questions about using "require"

Post by sanjiv »

Often, when I 'require' another file, I create a load function in that file, within which I define everything else that I need. I then run that load function in love.load.

Is that dumb? When I 'require' something from another file, is that already the same as running the contents of that required file in the love.load() function?

Code: Select all

require ('otherFile')
function love.load()
     otherThing:load()
end

Code: Select all

otherThing={}
function otherThing.load(otherThing)
...
end
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: questions about using "require"

Post by substitute541 »

Usually, functions do not get run unless they are called, and require only includes your file, it does not run all of it's contents (it's "require" not "runFile").
Currently designing themes for WordPress.

Sometimes lurks around the forum.
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: questions about using "require"

Post by scutheotaku »

You could simply place all of the stuff you want to load at the top of the file you are "requiring", i.e. not in a function.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: questions about using "require"

Post by Inny »

It's not dumb at all, though it depends on what you're trying to accomplish. Here's a few models. For the sake of simplicity, I'm going to assume each module modifies the global table which is fine for game-specific code.

1. Initialize Module when Requiring it

Code: Select all

-- other.lua
other = {}
function other.doSomething()
  --
end

do
  -- Module initializing code here
end
The trick to this is that the modules have to be required in order of dependencies. Meaning, if the Foo module uses code in the Bar module, then Bar has to either be required first, or in Foo there needs to be a require("Bar") line. This is completely normal and most people do this. Circular Dependencies are a headache to deal with, though.

2. Modules require explicit initialization

Code: Select all

-- other.lua
other = {}

function other:init()
  --
end

function other:doSomething()
  --
end
In this model, which yours is like, the modules don't spark a whole load of execution from a require statement. This is also pretty normal, and Libraries tend to take on this pattern. 'init' is the convention, but 'load' is also fine. Order the require lines aren't as important, and you don't really need to put the requires in each module. The init calls do matter. In the previous example, Bar:init would have to be called before Foo:init. Also, putting all of those init calls in the love.load function makes it easier to reset the game.

3. Super Clean Reusable Module Ninja Library Code
This is how library writers do their code. Do this if you want to share code with others:

Code: Select all

-- other.lua
local other = {}

function other:init()
  --
end

function other:doSomething
  --
end

return other
This one does NOT modify the global table. For library writers that's taboo. For instance, lets say you needed code from Mumbly Joe's util.lua, and you needed code from Super Steve's util.lua, and both modified the global table 'util'. Then you have a namespace collision, and have to directly edit the files. Instead, the module that requires the library module is responsible for the global table. See:

Code: Select all

-- main.lua
steves_util = require('3rdparty.steve.util')
joes_util = require('3rdparty.joe.util')

function love.load()
  steves_util:init()
  joes_util:init()
end

One thing to point out about the code I've been writing, I've been using the colon (:) operator to create an implied self in the module functions. Others will disagree with me on this, and say you should use the dot (.) operator. They can chime in with what they think, and they're probably right. My opinion is that the point of the colon operator is to signal that the functions are going to access modify the table they're apart of, so it makes sense when the module is serving as a Singleton.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: questions about using "require"

Post by Robin »

Inny wrote:My opinion is that the point of the colon operator is to signal that the functions are going to access modify the table they're apart of, so it makes sense when the module is serving as a Singleton.
FWIW, I tend to prefer modules without hidden state, so then dotted functions make more sense. (That usually also means you don't have problems with circular dependencies.)
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: questions about using "require"

Post by bartbes »

substitute541 wrote:require only includes your file, it does not run all of it's contents
But it does.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: questions about using "require"

Post by Inny »

Robin wrote:FWIW, I tend to prefer modules without hidden state, so then dotted functions make more sense. (That usually also means you don't have problems with circular dependencies.)
Agreed. Like if I keep a bunch of assorted math and string munging functions in a util module, there's no shared state, so the util table is just a collection of functions and uses the dot notation.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests