Question: Local vs Global Variable

Using Composer, if I set up a variable as local in my Forward Declarations do I also have to set it up as a local variable in scene create to prevent it from being a global variable?  

Would setting up the rock in the example below be the correct way to set it up and remove it?

Forward Declarations:

local rock

Scene Create:

rock=display.newImage(“Images/rock3.png”)
sceneGroup:insert(rock)
rock.isVisible = true
rock.x=500;  rock.y=230

Scene Destroy:

rock: removeSelf()

rock = nil

Thanks,
Lori

Hi Lori,

The forward declaration should be local, and that’s it. If you specify the same variable name as local within a function, it will be considered a different variable entirely (thus not the same as the forward reference).

Take care,

Brent

Thank you!   Have a great day.

Hi Lori,

The forward declaration should be local, and that’s it. If you specify the same variable name as local within a function, it will be considered a different variable entirely (thus not the same as the forward reference).

Take care,

Brent

Thank you!   Have a great day.

If I only have the local in the Forward declarations since I have SceneGroup:insert(rock), do I need to keep The removal code in Scene Destroy?

I was thinking I didn’t need the scene destroy code as Composer would garbage collect because inserted (rock) into SceneGroup.  Is this correct?

Thanks,

Lori

Hi Lori,

If the display object (rock) is part of the scene, you shouldn’t need to explicitly remove the forward reference to that object. Lua should garbage collect that on its own. Of course, that assumes you didn’t somehow tie it into some other reference, table, timer, etc. that would keep it “locked” in memory. :slight_smile:

Brent

If I only have the local in the Forward declarations since I have SceneGroup:insert(rock), do I need to keep The removal code in Scene Destroy?

I was thinking I didn’t need the scene destroy code as Composer would garbage collect because inserted (rock) into SceneGroup.  Is this correct?

Thanks,

Lori

Hi Lori,

If the display object (rock) is part of the scene, you shouldn’t need to explicitly remove the forward reference to that object. Lua should garbage collect that on its own. Of course, that assumes you didn’t somehow tie it into some other reference, table, timer, etc. that would keep it “locked” in memory. :slight_smile:

Brent

I have been reading the Tutorial: Scope for beginners.

If I set the rock variable up as below, is it correct to say that since the “rock” variable is not a back ground item and will need to be accessed after it is created, I cannot put local in front of it?

Since I did the following:  sceneGroup:insert(rock), will the “rock” variable be garbage collected?

Do I need to put local rock in Forward Declarations so that the “rock” variable will not be a global variable?

Thanks,

Lori

 

Forward Declarations:

Scene Create:

rock=display.newImage(“Images/rock3.png”)
sceneGroup:insert(rock)
rock.isVisible = true
rock.x=500; rock.y=230

Scene Destroy:

Hi Lori,

Yes, you should still make “rock” a local object. Even if you insert it into the sceneGroup, that is only inserting the Corona display object into the group. Now, that is fine because it will be “cleaned up” as part of the scene, but the actual variable will still be global. So, definitely make it local.

Brent

Do I need to nil it out in Scene Destroy or does the garbage collection from inserting the variable into sceneGroup take care of removing the variable?

Thanks!

Lori

Hi Lori,

Local variables will be automatically garbage-collected unless you somehow “tie them up” (link them) to some other reference that can’t be garbage-collected. If it’s just a local variable in the scene, then you don’t need to worry about it.

Brent

Thanks!

I have been reading the Tutorial: Scope for beginners.

If I set the rock variable up as below, is it correct to say that since the “rock” variable is not a back ground item and will need to be accessed after it is created, I cannot put local in front of it?

Since I did the following:  sceneGroup:insert(rock), will the “rock” variable be garbage collected?

Do I need to put local rock in Forward Declarations so that the “rock” variable will not be a global variable?

Thanks,

Lori

 

Forward Declarations:

Scene Create:

rock=display.newImage(“Images/rock3.png”)
sceneGroup:insert(rock)
rock.isVisible = true
rock.x=500; rock.y=230

Scene Destroy:

Hi Lori,

Yes, you should still make “rock” a local object. Even if you insert it into the sceneGroup, that is only inserting the Corona display object into the group. Now, that is fine because it will be “cleaned up” as part of the scene, but the actual variable will still be global. So, definitely make it local.

Brent

Do I need to nil it out in Scene Destroy or does the garbage collection from inserting the variable into sceneGroup take care of removing the variable?

Thanks!

Lori

Hi Lori,

Local variables will be automatically garbage-collected unless you somehow “tie them up” (link them) to some other reference that can’t be garbage-collected. If it’s just a local variable in the scene, then you don’t need to worry about it.

Brent

Thanks!