get a constant from another file

Hi, I am trying to write get a variable from another file. But I have a question about it.

suppose I have 2 files, “main.lua” and “stage1.lua”, I am trying to get the defined constant from stage.lua to main.lua. Please look at the sample code below, if I use “x = stage1.level1.varX” it works fine, but x = “stage” …stage… “.level” … level … “.varX” doesn’t work at all. Would you please give me some suggestion ??

Hope you understand what I am trying to ask…sorry about my English isn’t too good.


stage1.lua


level1 =
{
varX = 300,
varY = 300,

}


main.lua


local stage = 1
local level = 1
local x,y
x = stage1.level1.varX – This Works !
y = stage1.level1.varY – This Works !

x = “stage” …stage… “.level” … level … “.varX” --This doesn’t
y = “stage” …stage… “.level” … level … “.varY” --This doesn’t
[import]uid: 38556 topic_id: 12889 reply_id: 312889[/import]

it won’t because when you use
[lua]x = stage1.level1.varX[/lua]
you are saying that assign x the value of stage1.level1.varX

but when you use
[lua]x = “stage” … stage … “.level” … level … “.varX”[/lua]
you are saying that assign x the value of a string that is constructed as stage1.level1.varX

From your question itself, it shows that you are in the learning phases, so if I tell you that to do so you need to reference the _G directly and pick out your variable, you would not understand much. There could be easier ways to achieve what you are after, so what are you really after? You could use arrays which could then translate as

[lua] x = stage[stageNo][levelNo].varX[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12889 reply_id: 47326[/import]

what I am trying to accomplish is…let say I have a game with different stages and around 30 levels in each stage. I want to build a look up table that store all the level’s information such as the number of physical object, the position of each object, background image name, music file name etc… When the user select the stage and level, it will automatically load all the objects from the look up table.

In The Ghost Vs Zombie example they have, they made up 2 levels, and both of them are hard coded. I wonder what if the program size is if I need to make 100 levels…

My point is…I make a basic game engine, and every time I need to make a new level, I just need to edit the look up table easily.

Is there any better suggestion? games like “Angry Birds”, “Cut the Rope” and “Cover Orange” , how do they do that??

Thanks for you help !!
[import]uid: 38556 topic_id: 12889 reply_id: 47508[/import]