How do I properly test for NULL in c2

Get help using Construct 2

Post » Fri Oct 10, 2014 7:28 pm

functions have a habit of returning "" or 0 on failure, and this is a lot different than a null.

What is the equivalent of "if value == NULL" in C2?
Developing Surolace, the survival role playing space game.

surolace-survival-role-playing-space-game_t116953
B
14
S
4
Posts: 303
Reputation: 1,730

Post » Fri Oct 10, 2014 7:55 pm

There is no NULL in C2. Where would you want to test for NULL?

The only example I can think of is when using the find() expression. It will return -1 if the text can't be found.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Fri Oct 10, 2014 8:17 pm

There isn't one unfortunately... C2 doesn't have any referencing for "null" values. For official plugins everything is forced to Numbers, Strings or Boolean. nulls are usually returned as int(0) and then converted to what ever you are setting to.

Example:
You have a var myString that you set from a function return and some how that return value is null (shouldn't be possible unless using a 3rd party plugin) this would be the resulting outcome.

var myString = ""; (defined as type string)
myString = myfunction.returnValue; (set myString from function return value. if null it will return int(0))
myString equals "0" (stringified version of int(0))

Based on looking at the plugins this appears to be by design to either prevent system errors or non-programmers from making mistakes. We just have to work around this limitation.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Fri Oct 10, 2014 8:30 pm

This complicates things since empty strings and 0s are valid values in a lot of cases. I've been setting my own Functions to return -1 but if the compare dialog had a NULL option it would be VERY useful. Null is important imo. At least in the way my brain thinks.
Developing Surolace, the survival role playing space game.

surolace-survival-role-playing-space-game_t116953
B
14
S
4
Posts: 303
Reputation: 1,730


Return to How do I....?

Who is online

Users browsing this forum: Magistross, mihirolover, tarek2 and 8 guests