How to refer to variables in JS

Discussion and feedback on Construct 2

Post » Tue Jul 15, 2014 9:16 am

I have Javascript code that has to do something with variable from my game.
But when I put this code in Browser - Execute Javascript it doesn't see that variable.
I get this:
Image

How to refer to variables (initiated in C2) in Execute Javascript action of Browser object?
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Tue Jul 15, 2014 10:17 pm

Do you want to just use the value of the variable or do you want to change the variable in the code? If you just need the value then you include it in the string the way you normally include a variable in a string:

Browser: Execute javascript "some javascript code" & playerPoints & "more js code"
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Wed Jul 16, 2014 12:24 am

Execute javascript "var foo = 'bar';"
Execute javascript "alert(foo)" - >Error foo is undefined


Execute javascript "window['foo'] = 'bar'; "
Execute javascript "alert(foo)" - > bar


;)
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Wed Jul 16, 2014 7:01 am

ramones wrote:[..]
Browser: Execute javascript "some javascript code" & playerPoints & "more js code"

Thank you. This was too easy to figure it by myself :)
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Jul 16, 2014 1:04 pm

'Execute javascript' just passes whatever you give it to eval(). This can make it hard to write useful sequences as you've noted. We strongly recommend using the Javascript SDK instead.
Scirra Founder
B
399
S
236
G
89
Posts: 24,524
Reputation: 195,380

Post » Tue Jul 22, 2014 2:35 pm

I understand, but sometimes client wants me to input his simple script into my game. Using "Execute Javascript" is very fast in this case.
B
55
S
14
G
10
Posts: 287
Reputation: 7,677


Return to Construct 2 General

Who is online

Users browsing this forum: maddy467 and 6 guests