Trying to "centralise" a table

Hi Everyone, my first Corna project is going well thanks to the generous guys on the forum.

My project consists of several screens (lua files) and I have tables that need to be used in each of them. Instead of declaring these tables locally in each of the lua files, I would like to centralise the table declarations to another lua file as global variables and then "require" them into the screen files. This way everytime I change a table, I won't have to update it in every lua file.

I have seen examples of using functions within external lua files but how would I retrieve table data.

For example, I create a lua file called test.lua and my table is

local table1 = {

{ name = "111", type = "1"},
{ name = "222", type = "2"},
{ name = "333", type = "3"}
}

I know I need to

test1 = require ("test")

How would I retrieve the table data? Would it be test1.table1.name or test1.name? Or am I miles off?

Thanks

Adrian

Remove the "local" from your table1 in test.lua

Change test1 = require "test" to;
local test1 = require "test"

Then you can see values like so;
print (test1.table1[1].name)
OR
print (test.table1[1].name)

Either should print 111.

Peach :)

Thanks Peach - you're an angel! Makes everything far more compact and managaable.

Adrian

Hi,
Could You tell me how to manage (move from - to another ,copy from - to another, delete) some objects contained in two tables that have objects with proprieties (speed, image,..)?

general ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
local table1={
object1={
 
{},{},{}
},
 
    object2={
 
        {},{},{}
    }
 
    object3={
 
     {},{},{}
  },
 
}
 
local table2={
    object5={
 
      {},{},{}
    },
 
    object4={
 
       {},{},{}
    },
 
    object6={
 
      {},{},{}
    }
 
}

Thanks Adrian :)

@sewfew2000 - Take a look at this tutorial by Jon Beebe; http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

views:1801 update:2011/10/16 9:47:44
corona forums © 2003-2011