Page 1 of 1

Is NIL argument later defined still a local?

Posted: Fri Jun 05, 2026 12:58 pm
by Bugala
I have my own DisplayBrush function, and I just ran into a situation, which isnt that critical, but just wanting to keep the code clean.

Code: Select all

Function MyDisplayBrush(ID, X, Y, OpArgs)

if OpArgs <> NIL
	do stuff
else
	OpArgs = {}
Endif

DisplayBrush(ID, X, Y, OpArgs)


Main
MyDisplayBrush(1, 200, 300)

This is just to illustrate the problem, not the real version.

Idea being here that I have an option to pass down the usual Optional Arguments, that are possible to use in DisplayBrush.

However, since in some cases, I want to catch and alter some of those Optional Arguments, it therefore checks if OpArds is something else than NIL. If it is, then it checks those certain cases and alters the values.

But problem comes that in case OpArgs is NIL, then if I use DisplayBrush(ID, X, Y, OpArs) Hollywood crashes, since it is expecting a table.

Therefore in case OpArgs are not given, I need to make OpArgs an empty table.


But since right now this happens inside a IF-ELSE-ENDIF check, will this stil be local, or am I declaring a gloval variable here?

As in, when I use function MyFunc(X, Y)

X and Y are Local by default.

OpArgs itself is Local as well, but in this case, if OpArgs was never given, it is NIL instead. So when I then inside that IF-ELSE-ENDIF check declare OpArgs = {}, is it now actually global declaration, or still just assigning value to the local OpArgs?

Re: Is NIL argument later defined still a local?

Posted: Sat Jun 06, 2026 5:35 am
by plouf
why should ? everything inside function is local

Code: Select all

DebugPrint("is randomvariable before? "..StrStr(IsNil(RandomVariable)))
RandomVariable={}
DebugPrint("is randomvariable after?"..StrStr(IsNil(RandomVariable)))


DebugPrint("is OpArgs before? "..StrStr(IsNil(OpArgs)))

Function MyDisplayBrush(ID, X, Y, OpArgs)
DebugPrint("is OpArgs before inside func? "..StrStr(IsNil(OpArgs)))
  If OpArgs <> Nil
	  i=1
  Else
	  OpArgs = {}
  EndIf
DebugPrint("is OpArgs after inside func? "..StrStr(IsNil(OpArgs)))
EndFunction


MyDisplayBrush(ID, X, Y, OpArgs)
DebugPrint("is OpArgs after func executed? "..StrStr(IsNil(OpArgs)))

did i understand corerctly what ou mean?

Re: Is NIL argument later defined still a local?

Posted: Sat Jun 06, 2026 10:39 am
by Bugala
Point being that functions arguments are Func(A, B, C, D)

now if this Func in practice is called only with A, B, C = Func(1, 2, 3) then it means that D was never sent, and is therefore NIL inside the function.

If I then assign D as empty table inside the Function, when it is NIL, then will it still stay Local, or is it now actualy Global variable?


To give opposite example, if Function is called by Func(1, 2, 3, 4) then in this case D is clearly 4, not NIL. Therefore inside function there exists a local D, and if I then assign it to empty table D = {}, then it clearly stays Local, since its been local the whole time.


That actually the main point is that if calling Func(1, 2, 3) and therefore D not having value at all and becoming NIL, does that mean that D doesnt exist at all, and is therefore not LOCAL either?
And therefore when assigning it as Empty table inside the function, it will actually only define it at that point, and therefore define it as Global (since there is no "Local" in front of it?)

Re: Is NIL argument later defined still a local?

Posted: Sat Jun 06, 2026 11:56 am
by Flinx
Bugala, plouf has already answered the question precisely, hasn't he? His code example clearly shows that `OpArgs` is a local variable of the function even when it is passed as Nil.

Re: Is NIL argument later defined still a local?

Posted: Sat Jun 06, 2026 9:42 pm
by Bugala
Thanks Flinx!

Somehow when I looked ploufs answer, I thought the code was just reply from mine. Didnt realise it was whole new code, which also answers the question.

Thanks plouf, useful example which shows the answer.