## Questions about require, calling tables from files.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

### Questions about require, calling tables from files.

Hey, I have some require questions.

Do I have to assign different data files to local names? Like:

Code: Select all

local player = require 'player'
Best case would be if I could just

Code: Select all

require 'player' 
In love.load and then for example call player[1];

Code: Select all

function love.update()
player[1].x = 1
end

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: Questions about require, calling tables from files.

No you don't have to assign require-calls to local variables, but you should do so.

When you call require, then the code in the required file is executed. If this code defines a global variable, then it will also exist in your normal program.

However, in most cases, it is bad practice to define global variable in external code files. That way different libraries could get coupled or break each other.

Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

### Re: Questions about require, calling tables from files.

I think if you use require, you need to return a value from the end of the lua file?

Also if you use require, I think you will get same object as a return value such as:

Code: Select all

-- something.lua
return { moo = 'yo' }

-- main.lua
local a = require 'something'
a.moo = "test" -- change the moo in the table to 'test'
local b = require 'something'
print(b.moo) -- prints test instead of yo

I bumped into this issue feature when I tried to reset an object to the default values by just re-requiring it.

So it is good to know if you need to require the same file multiple times for whatever the reason, it only creates one instance.

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

### Re: Questions about require, calling tables from files.

Muris wrote:I think if you use require, you need to return a value from the end of the lua file?

Also if you use require, I think you will get same object as a return value such as:

Code: Select all

-- something.lua
return { moo = 'yo' }

-- main.lua
local a = require 'something'
a.moo = "test" -- change the moo in the table to 'test'
local b = require 'something'
print(b.moo) -- prints test instead of yo

I bumped into this issue feature when I tried to reset an object to the default values by just re-requiring it.

So it is good to know if you need to require the same file multiple times for whatever the reason, it only creates one instance.
Both correct, except for that you don't need to return anything (but usually want).

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

### Re: Questions about require, calling tables from files.

Thanks, I try to be really ordered this time when making a game. Right now I'm requiring data for my player:

Code: Select all

require 'player'
require 'graphics'
In my player I have:

Code: Select all

player = {
{x, y, controls = {w,a,s,d}}
{x, y, controls = {w,a,s,d}}
}

local player = require 'player'

which then look like:

Code: Select all

data = {
{x, y, controls = {w,a,s,d}}
{x, y, controls = {w,a,s,d}}
}
So I can refere to:

Code: Select all

player.data[1].x = 1

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Questions about require, calling tables from files.

That's still not quite right. What you'll want to do is localize the player inside player.lua then return it at the end as though it were a function on its own.

Code: Select all

-- player.lua
local player = {
{x = 0, y = 0, controls = {'w','a','s','d'} }
{x = 0, y = 0, controls = {'up', 'left', 'down', 'right'} }
}

return player

Then, from main.lua, you can do as you please.

Code: Select all

-- main.lua
local player = require 'player'

player[1].x = 100


Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

### Re: Questions about require, calling tables from files.

Kingdaro wrote:That's still not quite right. What you'll want to do is localize the player inside player.lua then return it at the end as though it were a function on its own.

Code: Select all

-- player.lua
local player = {
{x = 0, y = 0, controls = {'w','a','s','d'} }
{x = 0, y = 0, controls = {'up', 'left', 'down', 'right'} }
}

return player

Then, from main.lua, you can do as you please.

Code: Select all

-- main.lua
local player = require 'player'

player[1].x = 100

I'll try that, when I was away I had an idea that requiring lua files with data without setting it to a local variable may not be bad however setting something like bump.lua or libraries using a local variable I totally see the point.

Would you explain the benefit of assigning the returned table to a local variable?

Is this in order to become ordered while making architecture preventing crashes, conflicts?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Questions about require, calling tables from files.

It's to try and prevent hidden dependencies. Suppose your modules all assign to global variable, then you have no idea what modules are loaded when looking at a specific module. So if module A depends on module B, but doesn't require it, and module C requires module B, then it works out if module C just happens to be loaded before module A. But suppose module C is no longer needed, or it is changed to not need module B, or it is decided for some reason module A needs to be loaded before module C, then it all breaks. That's why it's better to make your dependencies explicit, and assign all modules to local variables.

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

### Re: Questions about require, calling tables from files.

So when assigning files and libraries to local variables the do not depend on each other in the same way?