[SOLVED] Accessing table from a different Lua file?

Hey guys,

Here is what im trying to do…

I have my ‘gameplay.lua’ file for the gameplay scene

And I have ‘myData.lua’ for the levels data

In ‘myData.lua’ file there is a table like this that holds the level’s 0-100 data:

[lua]

local level0To100 = {

level0={ 

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,5,6,4,6,4,6},

        {4,4,1,3,2,5,1,1},

        {4,4,2,1,1,3,1,3},

        {3,3,5,6,2,2,3,2},

        {6,2,2,6,2,5,2,4},

        {1,1,4,5,1,4,4,1},

        }

        

    ,BoardShapeID=2

    ,GameType = 0

    ,TimeLeft = 30 

    ,GameTarget = 2 

    ,MovesLeft = 0 }

,

level1={

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,6,5,5,6,4,6},

        {3,2,3,4,1,3,1,1},

        {3,6,6,4,2,5,1,3},

        {1,3,6,3,1,1,6,2},

        {6,2,5,6,2,6,2,4},

        {1,1,4,5,3,4,4,1},

        }

    

    ,BoardShapeID=17

    ,GameType = 1 

    ,TimeLeft = 60 

    ,GameTarget = 1500 

    ,MovesLeft = 0 }

,

level2={

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,6,5,5,6,4,3},

        {3,6,3,4,1,3,3,5},

        {3,6,1,2,2,1,1,3},

        {1,1,4,1,5,6,2,3},

        {3,2,3,3,2,3,2,4},

        {1,1,4,5,3,4,4,1},

        }

    ,BoardShapeID=18

    ,GameType = 2 

    ,TimeLeft = 5 – in seconds

    ,GameTarget = 2000 

    ,MovesLeft=13 }

}

– … you got the point

[/lua]

In my gameplay scene I require the file:

[lua]local mydata = require(“mydata”) [/lua]

And then I try to grab the correct level data like so

[lua]

– in gameplay.lua file

    local currGameData = mydata[“level0To100”][“level”…_levelNum]

– _levelNum is an integer with the value of the current level

[/lua]

This doesnt seems to work…I get the error

“attempt to index field ‘level0To100’ (a nil value)”

Any help?

Roy.

Reason is simple. You require “mydata” instead “myData” - remember that devices are case sensitive (windows is not and it can cause trouble if you do not pay attention to this matter).

The second one is that if you want require(“myData”) to return table then you must return this table in file. So at the end of myData.lua add line: return level0To100.

And remember - if you modify this table in other file - you will also modify original one - tables are passed by reference.

hey @piotrz55, sorry my bad it was a typo, my lua file name is mydata.lua and I require it fine

Do I have to return the table?

Cause If im doing something like this: (local every level)

[lua]

local level0={ 

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,5,6,4,6,4,6},

        {4,4,1,3,2,5,1,1},

        {4,4,2,1,1,3,1,3},

        {3,3,5,6,2,2,3,2},

        {6,2,2,6,2,5,2,4},

        {1,1,4,5,1,4,4,1},

        }

        

    ,BoardShapeID=2

    ,GameType = 0

    ,TimeLeft = 30

    ,GameTarget = 2 

    ,MovesLeft = 0 }

local level1={

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,6,5,5,6,4,6},

        {3,2,3,4,1,3,1,1},

        {3,6,6,4,2,5,1,3},

        {1,3,6,3,1,1,6,2},

        {6,2,5,6,2,6,2,4},

        {1,1,4,5,3,4,4,1},

        }

    

    ,BoardShapeID=17

    ,GameType = 1 

    ,TimeLeft = 60

    ,GameTarget = 1500 

    ,MovesLeft = 0 }

[/lua]

And then doing this in my gameplay.lua file:

[lua]    local currGameData = mydata[“level”…_levelNum][/lua]

It works, only when I put all the levels *inside* the 

[lua]local level0To100 = {}[/lua] 

As shown above it doenst work…

Oh and dont worry about the modifying part, this data is remained untouched.

Roy.

Yes, you must return table, because you have to return something from file to get access to it and table is the best one for this task. It ins’ t working for each level separately because this tables are local and you are not returning it!

You can just put at the end of mydata.lua

return {level1, level2, level3, and so on...}

And you still will be fine with

local data = require("mydata") local level1Data = data[1]

You can also do at the end

return level1, level2, level3...

But you would have to do

local level1Data, level2Data, level3Data = require("mydata")

Which is not flexible and is pain in the ass :stuck_out_tongue:
Table is just the best for such task, and even more if you write some piece of common functions or some library.

Works like a charm! @piotrz55 you are awesome!

Roy.

I posted 2nd post also

This return { … } is the same as wrapping in levels0To100 table

What I did is this:

[lua]

local levels0To100 ={

    level0={

        boardLevel = {

            {1,3,3,1,5,2,1,2},

            {3,2,3,3,2,2,3,4},

            {2,5,5,6,4,6,4,6},

            {4,4,1,3,2,5,1,1},

            {4,4,2,1,1,3,1,3},

            {3,3,5,6,2,2,3,2},

            {6,2,2,6,2,5,2,4},

            {1,1,4,5,1,4,4,1},

            }

            

        ,BoardShapeID=2

        ,GameType = 0

        ,TimeLeft = 30 

        ,GameTarget = 2 

        ,MovesLeft = 0 }

    ,level1={

        boardLevel = {

            {1,3,3,1,5,2,1,2},

            {3,2,3,3,2,2,3,4},

            {2,5,6,5,5,6,4,6},

            {3,2,3,4,1,3,1,1},

            {3,6,6,4,2,5,1,3},

            {1,3,6,3,1,1,6,2},

            {6,2,5,6,2,6,2,4},

            {1,1,4,5,3,4,4,1},

            }

        

        ,BoardShapeID=17

        ,GameType = 1 

        ,TimeLeft = 60 

        ,GameTarget = 1500

        ,MovesLeft = 0 }

}

local M = {}

–levels declaration

M[“levels0To100”] = levels0To100

return M

[/lua]

Then in my gameplay.lua file I take the data like so:

[lua]

local mydata = require(“mydata”) 

local currGameData = mydata[“levels0To100”][“level”…_levelNum]

[/lua]

It works.

Roy.

Just some note. When you require some file for the first time - then this file is executed like any normal file (you could even draw some objects - it would executs it as any normal code). If you return something from required file then this is returned as from function and this returned value is remembered.
If you require file again - then this file won’t be executed second time! You will just get this remembered value and that’s all.

Hehe, just saying but you can also have it like this (just to show it’s possible)

local M = { levels0To100 = levels0To100, } return M

Lua’s
data.levels0To100 is equal to data[“levels0To100”]

Great Info, thank you VERY MUCH!

Roy.

Reason is simple. You require “mydata” instead “myData” - remember that devices are case sensitive (windows is not and it can cause trouble if you do not pay attention to this matter).

The second one is that if you want require(“myData”) to return table then you must return this table in file. So at the end of myData.lua add line: return level0To100.

And remember - if you modify this table in other file - you will also modify original one - tables are passed by reference.

hey @piotrz55, sorry my bad it was a typo, my lua file name is mydata.lua and I require it fine

Do I have to return the table?

Cause If im doing something like this: (local every level)

[lua]

local level0={ 

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,5,6,4,6,4,6},

        {4,4,1,3,2,5,1,1},

        {4,4,2,1,1,3,1,3},

        {3,3,5,6,2,2,3,2},

        {6,2,2,6,2,5,2,4},

        {1,1,4,5,1,4,4,1},

        }

        

    ,BoardShapeID=2

    ,GameType = 0

    ,TimeLeft = 30

    ,GameTarget = 2 

    ,MovesLeft = 0 }

local level1={

    boardLevel = {

        {1,3,3,1,5,2,1,2},

        {3,2,3,3,2,2,3,4},

        {2,5,6,5,5,6,4,6},

        {3,2,3,4,1,3,1,1},

        {3,6,6,4,2,5,1,3},

        {1,3,6,3,1,1,6,2},

        {6,2,5,6,2,6,2,4},

        {1,1,4,5,3,4,4,1},

        }

    

    ,BoardShapeID=17

    ,GameType = 1 

    ,TimeLeft = 60

    ,GameTarget = 1500 

    ,MovesLeft = 0 }

[/lua]

And then doing this in my gameplay.lua file:

[lua]    local currGameData = mydata[“level”…_levelNum][/lua]

It works, only when I put all the levels *inside* the 

[lua]local level0To100 = {}[/lua] 

As shown above it doenst work…

Oh and dont worry about the modifying part, this data is remained untouched.

Roy.

Yes, you must return table, because you have to return something from file to get access to it and table is the best one for this task. It ins’ t working for each level separately because this tables are local and you are not returning it!

You can just put at the end of mydata.lua

return {level1, level2, level3, and so on...}

And you still will be fine with

local data = require("mydata") local level1Data = data[1]

You can also do at the end

return level1, level2, level3...

But you would have to do

local level1Data, level2Data, level3Data = require("mydata")

Which is not flexible and is pain in the ass :stuck_out_tongue:
Table is just the best for such task, and even more if you write some piece of common functions or some library.

Works like a charm! @piotrz55 you are awesome!

Roy.