How do I refer to a C2 variable from "Execute Javascript"?

Get help using Construct 2

Post » Fri Sep 09, 2016 9:30 am

I have a text global variable called myLocalVar and would like it to pop up in a window prompt, so I execute Javascript like this:

Code: Select all
"window.prompt('title goes here.', myLocalVar)"


But nothing came up when I run the game. How do you refer to your variable in the Javascript here?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Fri Sep 09, 2016 10:34 am

That's a confusing name for a global variable :P

This is actually pretty difficult because you need to build a string with the variable contents, but if the string contains JS characters then it will cause a syntax error (or in some circumstances, a pretty bad security vulnerability). It's probably easier to find a different way of doing that - why not prompt with ordinary objects in the C2 project?
Scirra Founder
B
399
S
236
G
89
Posts: 24,519
Reputation: 195,361

Post » Fri Sep 09, 2016 10:49 am

That's a confusing name for a global variable


...? ...! oops. Indeed lol.

why not prompt with ordinary objects in the C2 project?


Prompt with ordinary objects in the C2 project? Which object could act like window.prompt()?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Fri Sep 09, 2016 10:05 pm

Browser plugin .. action .. Alert ?
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Sep 10, 2016 12:54 am

@99Instances2Go Perhaps I am not clear. That only creates a message box with text. That's not what i wanted. window.prompt() creates a prompt box that allows user to type in something, copy/paste text, and press ok or cancel.

In fact, run the live code snippet here to understand what am I talking about: http://stackoverflow.com/a/6055620
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Sat Sep 10, 2016 2:33 am

Can code that with rich graphic, buttons and text boxes. Its just a background sprite with a gui.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Sep 10, 2016 4:32 am

You refer to it like this:

"prompt('title goes here.',"& myLocalVar&")"

The basic idea is you can't directly access an event sheet variable from JavaScript, but you can combine its value in the text.
B
94
S
33
G
113
Posts: 5,354
Reputation: 73,269

Post » Sat Sep 10, 2016 7:25 am

it may be better to use TextBox..

Image

see the difference..

myLocalVar_test.capx
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Sat Sep 10, 2016 8:26 am

I'm not 100% sure on that but I think you can call a C2 function in javascript, just make it return the value of the variable you need and you should be ok, never tried though.

c2_callFunction("name", ["param1", "param2"])

source https://www.scirra.com/manual/149/function

EDIT: just tried it: https://1drv.ms/u/s!AjkoCSebIn9JiXcTHqtxEm0psfjb , it shows the variable name in the prompt and sets the result of the prompt to the variable

you may have to url encode it in the function to ensure no syntax error can be done.

EDIT 2: doesn't seem to be an issue with characters in the end, so url encoding it is not needed it seems.
in any case, you have to handle the fact the player may simply cancel the prompt too.

also rojohound solution seems to be easier and faster, did not read all the way through, duh, I'm dumb.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sat Sep 10, 2016 4:09 pm

@aphrodite's way works too. Basically there are three ways:

1. Pass by value by building a string. This one as Ashley says can be an issue if the string contains " characters. You probably can come up with a creative solution if need be.

2. Utilize the function object. Make the JavaScript call a c2 function that in turn returns the value of something. This wouldn't have the " issue.

3. Find where the actual variable is in c2's runtime code and access it there. This will only work with minification if done inside a plugin. Also it is the most complex to do.
B
94
S
33
G
113
Posts: 5,354
Reputation: 73,269


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 11 guests