I have created a scene file (say game.lua) in one file, and I have another file (say graph.lua) which works for the same scene but placed in a separate file for module purpose. The problem I am having here is that I need to insert the images from graph.lua file into the same scene that game.lua created, and I poked around the internet and the forum, and seem not able to find a solution that works for me. If anyone has experience in such a situation and knows how to solve it, I would really appreciate the help. Thanks.
Not sure what you’re trying to do, but you could create a “globals” module and create your images there
-- appglobals.lua local M = {}; local appGlobals = {}; local getGlobals = function() return appGlobals; end return M;
At the top of your scenes you call:
local appglobals = require("appglobals") local g = appglobals.getGlobals();
…and anywhere you want to create shared objects/data call
-- for display objects g.someImage = display.newImage(...); -- for data g.someData = 1234;
You can then access the shared objects and data by using the g variable.
There are various ways of doing it - composer has a getVariable/setVariable that allows you to attach things as a ‘global to composer’ object. You could add another method to your scene and call it from the first scene.
I would add that Composer et al are designed for seperate scenes where each ‘scene’ has its own graphics and state and so on, graphics being created on the create event - passing images about (displayObjects ?) would work but it is conceptually confusing.
Can you explain what you have already tried?
From what I read the solution seems to be either:
A ) get graph.lua to return a display group which has all the images created by graph.lua in it, then add that display group to game.lua’s scene group.
B ) if graph.lua returns the images after say for instance the new/init function calls, then insert them into game.lua’s scene group then.
Am i missing something here? Otherwise it sounds like a or b is what you want to do
What I want to do is B ) and I only created a scene in game.lua, and there is not another scene in graph.lua, the only reason I have graph.lua is I don’t want to have a super long game.lua file. But to do B ) so far I haven’t found a nice way to do it. Creating a global.lua probably would work, but I don’t want to place everything I create into that global.lua file either. In fact, I already have such a file, and so far I only put some necessary variables and constants there that I need to access from most of other lua files, but for graph related objects, only my game.lua file needs it mostly.
Ultimately the problem I am having is inside graph.lua how do I retrieve the display group from game.lua so that I can insert method there on the image objects I just create? I tried to use local scene = storyboard.getScene(“game”), and then use local group = scene.view, and group:insert(myobject), the runtime error complains scene.view is nil value. I even tried to asign a display group object inside game.lua for the scene, and that doesn’t work either.
Would you be able to post up some code? I think you are either over thinking this, or overcomplicating it. No offence intended.
If you could post up either your current code, or better, a mini example of it, it will be easier for us to show you what needs to be done to get this working for you.
I found a way to solve this, when I call a function from game.lua into graph.lua, I am passing the display group name to the function argument, so that inside graph.lua, it can directly insert the display object into the passed in display group. Not sure if it is stated anywhere, what I find out for the scene change special effect to take place (be it fade, or slideLeft, etc), all the image objects need to be associated with the scene display group first, otherwise, the gotoScene call would show the called scene instantly. My way seems to have solved the issue I was facing, even though I am not sure if it’s the best way, but I am ok for now. Thanks everyone for all the suggestions.
Not sure what you’re trying to do, but you could create a “globals” module and create your images there
-- appglobals.lua local M = {}; local appGlobals = {}; local getGlobals = function() return appGlobals; end return M;
At the top of your scenes you call:
local appglobals = require("appglobals") local g = appglobals.getGlobals();
…and anywhere you want to create shared objects/data call
-- for display objects g.someImage = display.newImage(...); -- for data g.someData = 1234;
You can then access the shared objects and data by using the g variable.
There are various ways of doing it - composer has a getVariable/setVariable that allows you to attach things as a ‘global to composer’ object. You could add another method to your scene and call it from the first scene.
I would add that Composer et al are designed for seperate scenes where each ‘scene’ has its own graphics and state and so on, graphics being created on the create event - passing images about (displayObjects ?) would work but it is conceptually confusing.
Can you explain what you have already tried?
From what I read the solution seems to be either:
A ) get graph.lua to return a display group which has all the images created by graph.lua in it, then add that display group to game.lua’s scene group.
B ) if graph.lua returns the images after say for instance the new/init function calls, then insert them into game.lua’s scene group then.
Am i missing something here? Otherwise it sounds like a or b is what you want to do
What I want to do is B ) and I only created a scene in game.lua, and there is not another scene in graph.lua, the only reason I have graph.lua is I don’t want to have a super long game.lua file. But to do B ) so far I haven’t found a nice way to do it. Creating a global.lua probably would work, but I don’t want to place everything I create into that global.lua file either. In fact, I already have such a file, and so far I only put some necessary variables and constants there that I need to access from most of other lua files, but for graph related objects, only my game.lua file needs it mostly.
Ultimately the problem I am having is inside graph.lua how do I retrieve the display group from game.lua so that I can insert method there on the image objects I just create? I tried to use local scene = storyboard.getScene(“game”), and then use local group = scene.view, and group:insert(myobject), the runtime error complains scene.view is nil value. I even tried to asign a display group object inside game.lua for the scene, and that doesn’t work either.
Would you be able to post up some code? I think you are either over thinking this, or overcomplicating it. No offence intended.
If you could post up either your current code, or better, a mini example of it, it will be easier for us to show you what needs to be done to get this working for you.
I found a way to solve this, when I call a function from game.lua into graph.lua, I am passing the display group name to the function argument, so that inside graph.lua, it can directly insert the display object into the passed in display group. Not sure if it is stated anywhere, what I find out for the scene change special effect to take place (be it fade, or slideLeft, etc), all the image objects need to be associated with the scene display group first, otherwise, the gotoScene call would show the called scene instantly. My way seems to have solved the issue I was facing, even though I am not sure if it’s the best way, but I am ok for now. Thanks everyone for all the suggestions.