How do I set a given string as a global variable?

1 favourites
  • 8 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • I've got some lines on a <span class="letters"> that get updates from a remote server.

    And I want to register those updates on a global variable to work with them, so I'm trying to use JavaScript to set the global variable like this:

    But it's not working.

    Does anyone know how to solve this or any workaround?

    Thank you.

  • Can you execute a JS code when the value is updated in the parent page? If yes, then you can call C3 function:

    c3_callFunction("UpdateVariable", [newValue]);

    And in Construct create a function "UpdateVariable", which takes the parameter and saves it in the variable.

  • I can't execute JS code when the value is updated in the parent page.

    I found some youtube vide about iframes and changed it a bit. Now I have this:

    If I chain something like "ok", I get "undefined" as the value for innerText. If I chain no other value, I will get an empty value.

    "streamloots" it's my iframe Id.

  • So is this iframe inside your game, or the game inside the iframe?

    Check out this demo:

    howtoconstructdemos.com/communicating-with-an-iframe-in-construct

  • The iframe is inside the game.

    I hadn't found that demo till yesterday, but that's the one that taught me that I could call it by it's id. It's a very good demo and gave me a better understanding about what's going on and what am I doing (I'm not a javascript programmer), but I can't still get that string and store it in a variable.

    I don't think the problem is about storing. I can store any defined string using JavaScript, something like :

    runtime.globalVars.New = "Hello."

    Works fine. But, for some reason, I can't get anything inside the iframe. I've tried to use getElementsByClassName(), getElementById and now this contentWindow, but nothing is working. It's really weird to me as I can select with mouse and copy the text inside <span class="letters"> while on preview, so I think it's working fine as HTML code... but I can't acces to it using JavaScript somehow.

    Thank you very much for your time. I'll be thinking about this again today as I need to solve it for a very important project.

  • You need to give an id to the iFrame and try something like this:

    runtime.globalVars.New=Myiframe.contentWindow.document.getElementByClassName(...

    .

    If this doesn't work, you can create a JS function inside the iframe, which will retrieve the value from your HTLM element and return it. And then call that function from Construct:

    runtime.globalVars.New=Myiframe.contentWindow.functionName()

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, it doesn't work, and I can't create a JS function inside the iframe, as I'm using an URL to target to another domain.

    I've been told that this is about CORS, that I won't be allowed and nothing can be done. Is that it? Even when the info is shown in my project after it loads and I can select the text with my mouse and copy paste it, I can't do that with some JavaScript script?

    I'm thinking now about a quick "select text" to paste it to a textbox with the COPYCLIP plugin so that I can acces to that text from the texbos. It may not be an elegant solution but it can do the job I wanted to do, although this would change my question completely:

    Is there any quick way to select the text shown in an iframe?

  • So I could talk to a friend of a friend and we managed to get this working:

    document.querySelector("#alert-container .text-wrapper .letters").innerHTML

    But only when inspecting. When added as a script, it will just give a "null" value.

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