Set text to Global Variable

Get help using Construct 2

Post » Fri Oct 12, 2012 3:10 am

I found this in the forums http://www.scirra.com/forum/how-do-you-set-text-to-a-global-variable_topic48160.html, but it is only dealing with Construct Classic.

How can one set text to a global variable in C2?



I have a global variable (number) set. on start of layout the system runs an Ajax Request to a php file that returns data from a database for the user.

On Ajax complete I set the value of the global variable to the returned Ajax.LastData.

But when I try to set the text block to display the new global variable (every tick) it just shows the text "stat1" instead of the global variable value.

I have tried setting text to ""& stat1 which displays a NaN error.droptank212012-10-12 03:36:25
B
22
S
9
G
3
Posts: 203
Reputation: 3,647

Post » Fri Oct 12, 2012 5:33 am

Sounds like maybe you are not getting back just a number, try changing your global variable to text and see if it displayed.

Also if it is a number you might need to use the str(number) function to change it into a string for the textbox.
B
7
S
1
Posts: 74
Reputation: 1,232

Post » Fri Oct 12, 2012 11:52 am

Yeah Ajax.LastData is a string not a number. And you shouldn't have "" around Ajax.LastData.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Oct 12, 2012 1:58 pm

Anything in double-quotes is a string literal, meaning it will return exactly those characters.

So "AJAX.LastData" will return some text which just happens to look like an expression. Without the double quotes, AJAX.LastData will retrieve the last data from the AJAX object.

I guess you're making the same mistake accessing the stat1 variable. "stat1" will return some text that says "stat1". Without double quotes stat1 will return the contents of the global variable stat1.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Sat Oct 13, 2012 1:59 am

That fixed it. Thank you very much.
B
22
S
9
G
3
Posts: 203
Reputation: 3,647

Post » Sun Jun 02, 2013 9:50 am

@droptank21

How do you fix it? I have the same problem.
I want to take back global variable score from database to the game.
Thank you so much
B
3
Posts: 8
Reputation: 191

Post » Sat Jun 22, 2013 12:23 pm

Just as Ashley described above. Set the text to Ajax.LastData and do not use quotes.
B
22
S
9
G
3
Posts: 203
Reputation: 3,647


Return to How do I....?

Who is online

Users browsing this forum: emoticon, makkancs and 26 guests