Variable is nil automatically when out of scope?

In this piece of code:

[lua]function makeGlow()
local glow = display.newImage(“red heart glow.png”)
local function goAway(obj)
display.remove(obj)
end
transition.to ( glow, { time=1000, alpha=0, onComplete=goAway} )
end[/lua]

…inside of goAway() if I do obj = nil will it actually nil the glow variable? Or do I need to worry about that? Does Lua set the glow var to nil after it goes out of scope?

After searching PiL I believe that’s what happens (Lua nils it when it goes out of scope) but would like some verification. :slight_smile:

Jay [import]uid: 9440 topic_id: 34810 reply_id: 334810[/import]

Not sure either…

+1 for verification [import]uid: 70847 topic_id: 34810 reply_id: 138439[/import]

No, setting obj to nil will not nil the glow variable. What will nil the glow variable is the function where glow is declared local being exited…

A variable is nil when it does not exist (by virtue of not being declared.)

All variables declared local will effectively cease to exist when their parent function exits because they cannot be seen outside of their scope.

[lua]function makeGlow()
local glow = display.newImage(“red heart glow.png”) – goes nil when makeGlow exits
local function goAway(obj) – obj goes nil when goAway exits
display.remove(obj) – remove tells corona to remove the obj display object from the display groups
end – the obj variable stops being visible and effectively ceases to exist
transition.to ( glow, { time=1000, alpha=0, onComplete=goAway} )
end[/lua]

One important thing to know is that setting ‘obj’ to nil inside the goAway function will not affect the ‘glow’ variable. They are two different variables. Yes, they refer to the same object in memory, but they are not the same value. Killing one of them does not affect the other.

For more on that, lookup Object Oriented Programming and maybe take a look at some beginners tutorials on Java and C# “pointers and references”. [import]uid: 8271 topic_id: 34810 reply_id: 138448[/import]

I liked this one:

For more on that, lookup Object Oriented Programming and maybe take a look at some beginners tutorials on Java and C# “pointers and references”.

What do you say Jay? Maybe we should dust off some of our old C programming books… Hehehe [import]uid: 70847 topic_id: 34810 reply_id: 138450[/import]

It’s definitely worth a look because understanding the underlying workings, at least a bit, can really help formulate good code structure and debugging practices. Knowing the difference between a primitive and an object reference is critical, IMHO. [import]uid: 8271 topic_id: 34810 reply_id: 138454[/import]

Not sure either…

+1 for verification [import]uid: 70847 topic_id: 34810 reply_id: 138439[/import]

No, setting obj to nil will not nil the glow variable. What will nil the glow variable is the function where glow is declared local being exited…

A variable is nil when it does not exist (by virtue of not being declared.)

All variables declared local will effectively cease to exist when their parent function exits because they cannot be seen outside of their scope.

[lua]function makeGlow()
local glow = display.newImage(“red heart glow.png”) – goes nil when makeGlow exits
local function goAway(obj) – obj goes nil when goAway exits
display.remove(obj) – remove tells corona to remove the obj display object from the display groups
end – the obj variable stops being visible and effectively ceases to exist
transition.to ( glow, { time=1000, alpha=0, onComplete=goAway} )
end[/lua]

One important thing to know is that setting ‘obj’ to nil inside the goAway function will not affect the ‘glow’ variable. They are two different variables. Yes, they refer to the same object in memory, but they are not the same value. Killing one of them does not affect the other.

For more on that, lookup Object Oriented Programming and maybe take a look at some beginners tutorials on Java and C# “pointers and references”. [import]uid: 8271 topic_id: 34810 reply_id: 138448[/import]

I liked this one:

For more on that, lookup Object Oriented Programming and maybe take a look at some beginners tutorials on Java and C# “pointers and references”.

What do you say Jay? Maybe we should dust off some of our old C programming books… Hehehe [import]uid: 70847 topic_id: 34810 reply_id: 138450[/import]

It’s definitely worth a look because understanding the underlying workings, at least a bit, can really help formulate good code structure and debugging practices. Knowing the difference between a primitive and an object reference is critical, IMHO. [import]uid: 8271 topic_id: 34810 reply_id: 138454[/import]