How to access display group when not in scope

Given a display group, theGroup, that has been declared in a module, game.lua, what’s the syntax to make that group accessible in another module, ship.lua

In ship.lua, I tried…

local theGroup = require("filepath.game").theGroup  

…but that didn’t work. Any recommendations?

Hi @Dotnaught,

You may want to consider the “globals that aren’t actually globals” method of accessing variables/objects across scenes and modules. Maybe you already know this method, but just in case, here’s a tutorial on it:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Best regards,

Brent

Thanks, Brent. Unfortunately, refactoring isn’t practical in this instance. Is there a way to make a specific display group accessible from a separate module? I suspect I could do it indirectly using a parent reference off an object that’s in scope, but I’m not sure how to then refer the right display group if it exists inside a parent group with other objects.

Fully understood (no refactoring). I think this should work for you:

Let’s assume your “game.lua” module looks something like this (or can be adapted similarly):

[lua]

–game.lua


local M = {}

local group = display.newGroup()

print( group )

M.group = group

return M

[/lua]

And you require it like normal:

[lua]

–ship.lua


local gameModule = require( “game” )

[/lua]

The following should get the group ID:

[lua]

print( package.loaded[“game”].group )

[/lua]

Give that a test and see if the IDs match. I had to tweak and assemble this from some project code I had on the side, so hopefully I didn’t type anything wrong. :wink:

Brent

Awesome, thanks, Brent.

Hi @Dotnaught,

You may want to consider the “globals that aren’t actually globals” method of accessing variables/objects across scenes and modules. Maybe you already know this method, but just in case, here’s a tutorial on it:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Best regards,

Brent

Thanks, Brent. Unfortunately, refactoring isn’t practical in this instance. Is there a way to make a specific display group accessible from a separate module? I suspect I could do it indirectly using a parent reference off an object that’s in scope, but I’m not sure how to then refer the right display group if it exists inside a parent group with other objects.

Fully understood (no refactoring). I think this should work for you:

Let’s assume your “game.lua” module looks something like this (or can be adapted similarly):

[lua]

–game.lua


local M = {}

local group = display.newGroup()

print( group )

M.group = group

return M

[/lua]

And you require it like normal:

[lua]

–ship.lua


local gameModule = require( “game” )

[/lua]

The following should get the group ID:

[lua]

print( package.loaded[“game”].group )

[/lua]

Give that a test and see if the IDs match. I had to tweak and assemble this from some project code I had on the side, so hopefully I didn’t type anything wrong. :wink:

Brent

Awesome, thanks, Brent.