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

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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:

    "window.prompt('title goes here.', myLocalVar)"[/code:110qpqxp]
    But nothing came up when I run the game. How do you refer to your variable in the Javascript here?
  • That's a confusing name for a global variable

    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?

  • [quote:qib6kf8k]That's a confusing name for a global variable

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

    [quote:qib6kf8k]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()?

  • Browser plugin .. action .. Alert ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

  • Can code that with rich graphic, buttons and text boxes. Its just a background sprite with a gui.

  • 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.

  • it may be better to use TextBox..

    see the difference..


  • 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"])


    EDIT: just tried it:!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.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)