Page 2 of 2

Re: Questions about require, calling tables from files.

Posted: Fri May 08, 2015 8:45 am
by Robin
Bindie wrote:So when assigning files and libraries to local variables the do not depend on each other in the same way?
No, it's just that you can keep track of your dependencies much more easily that way.

Re: Questions about require, calling tables from files.

Posted: Fri May 08, 2015 6:14 pm
by Bindie
Alright, so It's more clear.

When assigning functions to local variables using the same approach as:

Code: Select all

-- Player file:
player = {}
return player

Code: Select all

-- Main file
local player = require 'player'
Can this be done in some way?

Right now I have:

Code: Select all

-- Map generation
function mapGen(t)
   -- Make map and return t
end
Instead of:

-- Main file

Code: Select all

require 'mapGen'
How do I go about this?

Re: Questions about require, calling tables from files.

Posted: Fri May 08, 2015 6:21 pm
by Robin
First, don't forget to do this:

Code: Select all

-- Player file:
local player = {}
return player
(Note the "local".)

Second, I'm not exactly sure, but I think you want something like this:

Code: Select all

--map.lua
local map = {}

function map.generate(t)
   -- Make map and return t
end

return map

Code: Select all

--some other module
loca map = require 'map'

...

... = map.generate(...)
Or if your module only needs a single function, you could choose to do something like:

Code: Select all

--mapGen.lua
local function mapGen(t)
   -- Make map and return t
end

return mapGen

Code: Select all

--some other module
loca mapGen = require 'mapGen'

...

... = mapGen(...)

Re: Questions about require, calling tables from files.

Posted: Fri May 08, 2015 6:27 pm
by Bindie
That's cool. I can return functions as well. I'll add a local to the external files. Awesome.