This section covers some common problems and presents their solutions.
t = {} ; create a table t.x = y ; assign 'y' to field x; note that y is Nil DebugPrint(t.x) ; ---> Error! Field 'x' not initialized! |
The solution is to initialize y
first, e.g.:
t = {} ; create a table y = 0 ; set y to 0 t.x = y ; assign 'y' to field x DebugPrint(t.x) ; Works! Prints '0' |
a = 0 b = Nil DebugPrint(GetType(a) = #NIL, a = Nil) ; prints "0 1" DebugPrint(GetType(b) = #NIL, b = Nil) ; prints "1 1" |
You see that "a = Nil" returns True although a
is zero. That is because Nil
is always regarded as zero when used in expressions. Thus, if you want to
find out whether a variable really is Nil, use GetType() and compare the
result against #NIL
. Starting with Hollywood 6.0 you can also use the dedicated
IsNil() function to check a variable against Nil
. See IsNil for details.
; bad code! Local a = 5, b = 6, c = 7 |
Unfortunately, this bad code would not even trigger an error but it would
be interpreted in a wrong way. The code above would make Hollywood assign
5 to a
and simply drop the rest because there is only one variable on the
left side of the equal sign. So you have to be careful with multiple variable
initialization. Accordingly, the correct version would be the following code:
; good code! Local a, b, c = 5, 6, 7 |
This code will assign 5 to a, 6 to b, and 7 to c.
Function p_Add(a, b) Local r = a + b Return r ; OUCH!!! EndFunction |
This code would be interpreted as "Return, and then call the function r()".
Of course, the call to r()
will never be reached but the function as written
above will return Nil, i.e. nothing, in every case. The correct version is
this:
Function p_Add(a, b) Local r = a + b Return(r) EndFunction |