Questions about require, calling tables from files.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
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.

Post 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.
Help us help you: attach a .love.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Questions about require, calling tables from files.

Post 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?
User avatar
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.

Post 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(...)
Help us help you: attach a .love.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Questions about require, calling tables from files.

Post by Bindie »

That's cool. I can return functions as well. I'll add a local to the external files. Awesome.
Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests