Page 1 of 1

Distinguish BOOL from number

Posted: Fri Jul 19, 2019 11:03 am
by Clyde
Hey there,

for my little project where I port a lua script to Hollywood I need to explicitly distinguish between bool values (true|false) and numbers (1|0).

If I am using GetType() on boolean values I get #NUMBER which isn't of any help when I want to know whether this was the value "false" or "0".

So, is there a way to find out that the value was a boolean and not a number?

Re: Distinguish BOOL from number

Posted: Fri Jul 19, 2019 4:56 pm
by SamuraiCrow
You'll have to do a type enumeration for every variable to keep track yourself.

Re: Distinguish BOOL from number

Posted: Fri Jul 19, 2019 10:57 pm
by PEB
I don't know if it would be too much trouble to rewrite your script to do it this way, but what I normally do if I want to show a negative result in a way that will not be confused with False or Nil is to return -1. (But that only works if there is no possibility of -1 being a normal/positive return.)

Another, and possibly better, way is to have your function return an extra result that can be checked separately. For example, if this is your function:
val=p_MyFunc()

change it to this instead:
val, bool=p_MyFunc()

Now you can check your val return as a number and your bool result separately.

Re: Distinguish BOOL from number

Posted: Mon Jul 22, 2019 6:13 pm
by airsoftsoftwair
In contrast to Lua, Hollywood doesn't have a specific boolean type but just numbers. So there is no way to distinguish a boolean from a number because in Hollywood stuff like "True" and "False" are just numbers that evaluate to 1 and 0, respectively.

By the way, this might be an interesting read for you. It also says the following about booleans:
Hollywood does not support the boolean object type. In Hollywood, the values True and False are simply special constants that will be mapped to the numerical values 1 and 0 respectively. There is no special object type for boolean values. This means that comparing 0 against False will be True in Hollywood, whereas in Lua it would be False because you would be comparing two different object types. Internally, Lua's boolean API is still supported by the VM and your plugin could use the respective functions from LuaBase but this is not recommended since Hollywood itself will never use Lua's boolean object type. It will always just use numbers. Not to mention that it is impossible to pass a real Lua boolean value to one of your plugin's functions because the parser will map all the True and False keywords to plain numbers.