Page 1 of 2

Questions about require, calling tables from files.

Posted: Wed May 06, 2015 6:53 pm
by Bindie
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

Re: Questions about require, calling tables from files.

Posted: Wed May 06, 2015 7:29 pm
by micha
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.

Re: Questions about require, calling tables from files.

Posted: Wed May 06, 2015 8:28 pm
by Muris
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.

Re: Questions about require, calling tables from files.

Posted: Wed May 06, 2015 9:27 pm
by s-ol
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).

Re: Questions about require, calling tables from files.

Posted: Wed May 06, 2015 9:31 pm
by Bindie
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}}
}
To make it readable should I instead maybe:

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

Re: Questions about require, calling tables from files.

Posted: Thu May 07, 2015 3:45 am
by Kingdaro
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

Re: Questions about require, calling tables from files.

Posted: Thu May 07, 2015 3:31 pm
by Bindie
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?

Re: Questions about require, calling tables from files.

Posted: Thu May 07, 2015 4:50 pm
by Robin
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.

Re: Questions about require, calling tables from files.

Posted: Thu May 07, 2015 7:13 pm
by Bindie
So when assigning files and libraries to local variables the do not depend on each other in the same way?

Re: Questions about require, calling tables from files.

Posted: Fri May 08, 2015 3:09 am
by cohadar
I think this is current best practice:
http://hisham.hm/2014/01/02/how-to-writ ... ule-world/