passing displayobjects between scenes - works until object reloaded

Dear Corona community,

following the Tutorial: Goodbye Globals:

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

I have a “globalSpace.lua” with the following content:

--my global space local globalSpace = {} return globalSpace

In one of my scenes I require this file using

local globals = require("globalSpace")

outside the scene-functions and add a displayObject to it within my create-function by

local finalImage = display.captureBounds( imageBounds, false ) globals.img1 = finalImage

or

local finalImage = display.captureBounds( imageBounds, false ) globals.img2 = finalImage

, depending on a parameter passed.

If I go to another scene, I have access to these globals.img1 and globals.img2 no problem, I even can update them easily using the code above.

The problem starts here: I can insert globals.img1 into my sceneGroup every time I go to the scene as long as I stick to one parameter. In other words, as long as I set

globals.img1 = finalImage

it works fine, but as soon as I send another parameter and use

globals.img2 = finalImage

and try to insert globals.img2 into my sceneGroup, I get the following error:

bad argument #-2 to 'insert' (Proxy expected, got nil)

If, vice versa, I start with the other parameter and beging with globals.img2, it works fine until I change parameter and go back to globals.img1.

I hope anyone can understand the problem. I just dont get why it says “got nil” even though I can easily access both images. Weird…

Thanks for any help or comments

Update: If I have the following code in the lua file that actually sets the globals.img1 or globals.img2 variables, everything works fine.

local tmp1 = globals.img1 local tmp2 = globals.img2 if(tmp1) then if(tmp1.width) then sceneGroup:insert(tmp1) end end if(tmp2) then if(tmp2.width)then sceneGroup:insert(tmp2) end end

If I leave out the “.width” - check, I get the error again. So, it must have something to do with the displayobjects not passed or read out correctly.

God damnit, this is killing me. Fact is: I can only insert globals.img1 to my scenegroup if the scene before also added it to the scenegroup.

In other words: I have scene A, B and C. In scene C, globals.img1 is set for the first time and also added to the sceneGroup. If I switch to scene B, I can access that globals.img1 no problem. If I then go to scene A, where globals.img1 is not accessed at all, and go back to scene B, I can no longer access globals.img1.

If, instead, also access globals.img1 in scene A, it works.

Hours and hours pass, I just dont get it…

This is a strange error.  globals.img2 is just a pointer to the image object. You can have multiple variables holding a reference to the image object, just be aware that you have to remove all references before the object would get garbage collected.

It sounds like globals.img2 is nil and I don’t see enough information to answer why that might be the case.

Rob

Update: If I have the following code in the lua file that actually sets the globals.img1 or globals.img2 variables, everything works fine.

local tmp1 = globals.img1 local tmp2 = globals.img2 if(tmp1) then if(tmp1.width) then sceneGroup:insert(tmp1) end end if(tmp2) then if(tmp2.width)then sceneGroup:insert(tmp2) end end

If I leave out the “.width” - check, I get the error again. So, it must have something to do with the displayobjects not passed or read out correctly.

God damnit, this is killing me. Fact is: I can only insert globals.img1 to my scenegroup if the scene before also added it to the scenegroup.

In other words: I have scene A, B and C. In scene C, globals.img1 is set for the first time and also added to the sceneGroup. If I switch to scene B, I can access that globals.img1 no problem. If I then go to scene A, where globals.img1 is not accessed at all, and go back to scene B, I can no longer access globals.img1.

If, instead, also access globals.img1 in scene A, it works.

Hours and hours pass, I just dont get it…

This is a strange error.  globals.img2 is just a pointer to the image object. You can have multiple variables holding a reference to the image object, just be aware that you have to remove all references before the object would get garbage collected.

It sounds like globals.img2 is nil and I don’t see enough information to answer why that might be the case.

Rob