problem accessing data in modules

Hi all,
I’m so confused. I’m trying to store some data in an external module, leveldata.lua and trying to access that data from main.lua.

My code in main.lua is:

local leveldata = require(“leveldata”)
local ChuteWidth = leveldata.ChuteWidth
print(“leveldata.ChuteWidth=”,leveldata.ChuteWidth)

My code in leveldata.lua is:

module(…, package.seeall)
local ChuteWidth = { 76, 112, 67, 66 }

However the print statement indicates that leveldata.ChuteWidth is nil.

What stupid thing am I doing wrong?
Thanks,
-Dennis
[import]uid: 108253 topic_id: 20272 reply_id: 320272[/import]

You can use different approach to this
main.lua
[lua]local ext = require(“external”)

for i,v in pairs(ext.data) do
print(i,v)
end[/lua]

external.lua
[lua]local Public = {}

Public.data = {60,40,30,20}

return Public[/lua]
[import]uid: 16142 topic_id: 20272 reply_id: 79231[/import]

Thanks darkconsoles,
Unfortunately I have a rather large amount of data that I would like to put into leveldata.lua and to recode it I think would be difficult and possibly introduce bugs.

I was hoping someone might just notice what basic error I was making in trying to access the data. It seems that I am following the documentation but apparently I am not.

I may move this thread over to the developer forum since it is much more active.
Thank you,
-Dennis
[import]uid: 108253 topic_id: 20272 reply_id: 81098[/import]

In leveldata.lua, make ChuteWidth a global instead of a local:

OLD:
module(…, package.seeall)
local ChuteWidth = { 76, 112, 67, 66 }

NEW:
module(…, package.seeall)
ChuteWidth = { 76, 112, 67, 66 }
But you should read Jon Beebe’s tutorial
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

Then your leveldata.lua would look like:

local M = {}
local ChuteWidth = { 76, 112, 67, 66 }
M.ChuteWidth = ChuteWidth
return M

And your main.lua would stay the same:

local leveldata = require(“leveldata”)
local ChuteWidth = leveldata.ChuteWidth
print(“leveldata.ChuteWidth=”,leveldata.ChuteWidth)

[import]uid: 23636 topic_id: 20272 reply_id: 81145[/import]

I struggled with this concept as well. The pattern I ended up using is as follows:

The main.lua file:

-- main.lua  
local bob = require("Bob")  
local sally = require("Sally")  
  
bob:init( {sally = sally} )  
sally:init( {bob = bob} )  

The Bob.lua file:

-- Bob.lua  
  
module(..., package.seeall)  
  
local bob = {}  
-- Forward reference sally here:  
local sally = {}  
  
bob.foo = "bobs foo"  
bob.bar = "bobs bar"  
  
function bob:init(params)  
 sally = params.sally  
end  
  
-- This "return" at the end of the file, returns everything in the "bob" table to the variable used during the "require" statement.  
  
return bob  
  

The Sally.lua file:

-- Sally.lua  
  
module(..., package.seeall)  
  
local sally = {}  
-- Forward reference bob here:  
local bob = {}  
  
sally.foo = "sallys foo"  
sally.bar = "sallys bar"  
  
function sally:init(params)  
 bob = params.bob  
end  
  
-- This "return" at the end of the file, returns everything in the "sally" table to the variable used during the "require" statement.  
  
return sally  
  

Now everyone can interact with everyone.

Example:

In the Bob.lua file, we can get sally’s foo with the “getSallysFoo” function as:

-- Bob.lua  
  
module(..., package.seeall)  
  
local bob = {}  
-- Forward reference sally here:  
local sally = {}  
  
bob.foo = "bobs foo"  
bob.bar = "bobs bar"  
  
function bob:printSallysFoo()  
 -- Since sally was forward referenced, we don't get compile errors  
 print ("sally.foo:" .. sally.foo)  
end  
  
function bob:init(params)  
 -- Now we are populating the dummy "forward referenced" variable with a real reference. Now we can get / set anything that is in the Sally.lua file, so long as the stuff in Sally.lua that we want to fiddle with is preceded with "sally." (e.g. sally.whatever" or "sally:whatever" in the Sally.lua file)  
 sally = params.sally  
  
 -- Call the fundtion once initialized:  
 bob:printSallysFoo()  
end  
  
return bob  

As you can see, we can now call that function to sallys foo from the main:

-- main.lua  
local bob = require("Bob")  
local sally = require("Sally")  
  
bob:init( {sally = sally} )  
sally:init( {bob = bob} )  
  
-- Since everything is all initialized here, we can now print sallys foo here too:  
bob:printSallysFoo()  

Hope this all makes sense (I didn’t run the code, so there may be typing errors :frowning:

But it’s the pattern I’m trying to render.

[import]uid: 616 topic_id: 20272 reply_id: 81236[/import]

I had the similar problem. You can se my solution here:
http://developer.anscamobile.com/forum/2011/12/29/very-noob-question-about-modular-coding-etc

It doesn’t use module(…, package.seeall) which is, if I understand it correctly, something Ansca want us to stop using… [import]uid: 101952 topic_id: 20272 reply_id: 81267[/import]

Here’s what I do:

data.lua

module( ..., package.seeall)  
  
local myData = {}  
myData[1] = "Fred"  
myData[1] = "Barney"  
function get()  
 return myData  
end  

then when I want the data, say inside main.lua

  
local data = require("data")  
  
local myData = data.get()  
  

[import]uid: 19626 topic_id: 20272 reply_id: 81287[/import]

Thanks all,
I appreciate all your responses. I was able to see what I was doing wrong from your examples and was able to correct it.
I ended up moving most of my level data to leveldata.lua and it has cleaned up my main.lua tremendously.
Thanks again,
-Dennis [import]uid: 108253 topic_id: 20272 reply_id: 81424[/import]