does variable = NIL clean variable away?

Find quick help here to get you started with Hollywood
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

does variable = NIL clean variable away?

Post by Bugala »

I am quite often using something following:

Code: Select all

function func()
var = NIL
if varcheck = true then var = 1
return(var)
endfunction
My assumption has been that I create a "local var" that would get destroyed after returning from the function.

However, now I am wondering if I have misunderstood something.

For in case varcheck = true, then does "var" still get destroyed after being returned from function because it is local variable.

Or is it actually so that my first "var = NIL" doesn't actually do anything, but at varcheck = true situation, "var" is created as a global variable?
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: does variable = NIL clean variable away?

Post by jPV »

Yes, "var = NIL" does nothing (or actually clears the global "var" variable).

You must declare all local variables with the "Local" statement, otherwise they become global (with an exception of function parameters which are local too).

So, you should do it like this to get a local "var" variable:

Code: Select all

function func()
	local var
	if varcheck = true then var = 1
	return(var)
endfunction
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: does variable = NIL clean variable away?

Post by airsoftsoftwair »

Setting a variable to Nil also marks it as a candidate for garbage collection in the next cycle.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: does variable = NIL clean variable away?

Post by Bugala »

Thanks Jpv, forgot I can do it that way, although have done so before.


But one more question. Is there possibility to do similar empty declaration of a variable when using Function?

For example:
CreateNewItem(20, 30, Global status)
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: does variable = NIL clean variable away?

Post by airsoftsoftwair »

Bugala wrote: Thu Sep 19, 2019 1:04 pm But one more question. Is there possibility to do similar empty declaration of a variable when using Function?

For example:
CreateNewItem(20, 30, Global status)
No, that doesn't make sense to me.
Post Reply