Referencing variable in a table from another doc

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…

[lua]–doc1.lua
local scene = {
objects = {
{ – The Player
name = “player”,
img = “art/player.png”,
start_x = -170,
start_y = -4720,
},
},[/lua]

Is it possible to reference “player” by name? How?

So far, I have, in a separate lua document,

[lua]–doc2.lua
local function enemy(self, event)
local scene = require(“doc1”)
– ??? Something goes here.
if(self.y <= player.y) then
print(“SUCCESS”)
end
end[/lua]

How do I make “success” happen? :slight_smile: Thanks. [import]uid: 191140 topic_id: 35170 reply_id: 335170[/import]

You have to return things 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 type return scene at the end to pass the data on.

Similarly I can make a lua file myself like this;

[code]–cool.lua
local rabbits = {2,3,4}

return rabbits[/code]

If I require in cool.lua elsewhere,

local cool = require("cool") print( cool[1] ) -- 2

The trick with your code is that just because you’re returning something doesn’t mean it will attach everything in it:

local rabbit = {} rabbit.name = "robert" -- this is part of rabbit, so it goes with it blob = "derek" -- this is not a part of rabbit, so it doesn't go with it

So you can type return objects in doc1, but that won’t give you the scene table, which means in doc2, scene == objects.
[import]uid: 41884 topic_id: 35170 reply_id: 139798[/import]

Thank you, that helped a lot. I’m going to have to restructure things a bit. Nice moogle avatar, btw. :slight_smile: [import]uid: 191140 topic_id: 35170 reply_id: 139962[/import]

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:

[lua]local sceneData = {
objects = {
player = { name = “player”, img = “art/player.png”, start_x = -170, start_y = -4720 }
}
}

return sceneData[/lua]

In doc2.lua

[lua]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[/lua]

Just a suggestion. Cheers. [import]uid: 202223 topic_id: 35170 reply_id: 139963[/import]

You have to return things 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 type return scene at the end to pass the data on.

Similarly I can make a lua file myself like this;

[code]–cool.lua
local rabbits = {2,3,4}

return rabbits[/code]

If I require in cool.lua elsewhere,

local cool = require("cool") print( cool[1] ) -- 2

The trick with your code is that just because you’re returning something doesn’t mean it will attach everything in it:

local rabbit = {} rabbit.name = "robert" -- this is part of rabbit, so it goes with it blob = "derek" -- this is not a part of rabbit, so it doesn't go with it

So you can type return objects in doc1, but that won’t give you the scene table, which means in doc2, scene == objects.
[import]uid: 41884 topic_id: 35170 reply_id: 139798[/import]

Thank you, that helped a lot. I’m going to have to restructure things a bit. Nice moogle avatar, btw. :slight_smile: [import]uid: 191140 topic_id: 35170 reply_id: 139962[/import]

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:

[lua]local sceneData = {
objects = {
player = { name = “player”, img = “art/player.png”, start_x = -170, start_y = -4720 }
}
}

return sceneData[/lua]

In doc2.lua

[lua]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[/lua]

Just a suggestion. Cheers. [import]uid: 202223 topic_id: 35170 reply_id: 139963[/import]