How do I compare a variable to null

Get help using Construct 2

Post » Fri Apr 21, 2017 2:16 pm

Hi,

I have a variable returned from an AJAX call that might be null. How do I compare this variable to null & redirect when the variable is null?

Thanks,

Orion
B
3
Posts: 5
Reputation: 185

Post » Fri Apr 21, 2017 2:26 pm

There's a system condition "If number is NaN" that you can use to check.
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Fri Apr 21, 2017 2:33 pm

sorry, I should have been more specific. The variable is named "returnPage" & is a string.
B
3
Posts: 5
Reputation: 185

Post » Fri Apr 21, 2017 2:52 pm

dang it, I can't figure out how to insert an image here to help explain what I'm trying to do. I'll try English :D

On touched resultsHomeButton
if returnPage is null
go to "MissionChallenges.aspx" (target self)
else
go to returnPage (target self)

but Construct 2 shows (not valid here) next to my else. How do I get this to work? Again, the returnPage variable is returned from an AJAX call in a JSON object, & the server might return null for that variable...
B
3
Posts: 5
Reputation: 185

Post » Fri Apr 21, 2017 2:56 pm

@RealityMasque

Is that null as in no value or an empty string?

If it's an empty string, doing System-Compare two values: returnPage = "" should do it.

Otherwise, not sure if this will work, but maybe System-Compare two values: "" & returnPage = ""
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Fri Apr 21, 2017 2:58 pm

It's actually null, because the current javascript that simply redirects to returnPage refreshes the page when it does window.location.href = returnPage.
B
3
Posts: 5
Reputation: 185

Post » Fri Apr 21, 2017 3:05 pm

@RealityMasque

Just remembered something that works with boolean instance variables is the expression Object.BooleanVar ? "true" : "false"

I wonder if that would work for the comparison here
e.g.
System-Compare two values: returnPage ? "notNull" : "null" = "null"


EDIT: Had a quick look in the Editor, there is a System-Is value type condition, which you could use to see if returnPage is a string
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Fri Apr 21, 2017 3:31 pm

well, it looks I can do that, but the else is still saying (not valid here)
B
3
Posts: 5
Reputation: 185

Post » Fri Apr 21, 2017 4:11 pm

RealityMasque wrote:well, it looks I can do that, but the else is still saying (not valid here)


I've just tried adding an Else event following the System-Is value type event, and that works, so not sure where you're putting your else to get that error.

If you can't get the else to work, do another event with the same condition, but invert it (right-click, select Invert).
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Fri Apr 21, 2017 5:58 pm

You are probably trying to use an "else" statement on a trigger. This won't work.

See the difference here :
Image
B
67
S
28
G
24
Posts: 970
Reputation: 18,675


Return to How do I....?

Who is online

Users browsing this forum: alextro, Ryan Kocourek and 1 guest