Referencing variable in a table from another doc
Posted on Mon, 2013-01-21 14:08
This is a fairly simple concept that I just can't get the right syntax for, so I'd like some help!
One doc says...
1 2 3 4 5 6 7 8 9 10 | --doc1.lua local scene = { objects = { { -- The Player name = "player", img = "art/player.png", start_x = -170, start_y = -4720, }, }, |
Is it possible to reference "player" by name? How?
So far, I have, in a separate lua document,
1 2 3 4 5 6 7 8 | --doc2.lua local function enemy(self, event) local scene = require("doc1") -- ??? Something goes here. if(self.y <= player.y) then print("SUCCESS") end end |
How do I make "success" happen? :) Thanks.
Replies
Posted on Tue, 2013-01-22 11:10
#2
Thank you, that helped a lot. I'm going to have to restructure things a bit. Nice moogle avatar, btw. :)
Posted on Tue, 2013-01-22 11:32
#3
I'd be cautious about using "scene" as a variable if you plan on using Storyboard, as it's used a lot in the docs, and you may inadvertently overwrite it.
To gain access to you data easier, I would assign it to a "player" key:
In doc1.lua:
1 2 3 4 5 6 7 | local sceneData = { objects = { player = { name = "player", img = "art/player.png", start_x = -170, start_y = -4720 } } } return sceneData |
In doc2.lua
1 2 3 4 5 6 7 | local function enemy(self, event) local sceneData = require("doc1") -- ??? Something goes here. if(self.y <= sceneData.objects.player.start_y) then print("SUCCESS") end end |
Just a suggestion. Cheers.


You have to
returnthings you want to retrieve from other files. This is how storyboard works, because you use a bunch of storyboard.* functions to create each scene, and then typereturn sceneat the end to pass the data on.Similarly I can make a lua file myself like this;
If I require in cool.lua elsewhere,
The trick with your code is that just because you're returning something doesn't mean it will attach everything in it:
So you can type
return objectsin doc1, but that won't give you the scene table, which means in doc2, scene == objects.