Global Tables

Hi Geeks,

I want to have global data… and I created a file - globals.lua

local data1 = {

level=1

score = 0

}

local data2= {}

bg_images[1] = “asd.jpg”

bg_images[2] = “asdfsf.jpg”

bg_images[3] = “blasfsdfah.jpg”

return data1,data2

Now in my scene.lua, I call this as follows… 

local my_global = require(“globals”)

If I want to access the data2 ? … what is the method ? … is having 1 file per 1 global table the only solution ?

regards,

Joel

[lua]

local glo = {}

glo.data1 = {

level=1

score = 0

}

glo.data2= {

somemore = 4

data = “here”

}

return glo

[/lua]

[lua]

glo = require(“globals”)

print (glo.data1.level)

print (glo.data2.data)

[/lua]

@nick_sherman:

I’d make “glo” local, though, or it defeats the whole purpose :slight_smile:

  • C

Thanks for the reply nick! 

I don’t know if this would work or not, but did you try:

local my_global1, my_global2 = require(“globals”)

???

Though honestly the one table method that Nick mentioned could be the best.

local glo = require(“globals”)

local my_global1 = glo.data1

local my_global2 = glo.data2

[lua]

local glo = {}

glo.data1 = {

level=1

score = 0

}

glo.data2= {

somemore = 4

data = “here”

}

return glo

[/lua]

[lua]

glo = require(“globals”)

print (glo.data1.level)

print (glo.data2.data)

[/lua]

@nick_sherman:

I’d make “glo” local, though, or it defeats the whole purpose :slight_smile:

  • C

Thanks for the reply nick! 

I don’t know if this would work or not, but did you try:

local my_global1, my_global2 = require(“globals”)

???

Though honestly the one table method that Nick mentioned could be the best.

local glo = require(“globals”)

local my_global1 = glo.data1

local my_global2 = glo.data2