How do I receive a Postmessage inside construct?

From the Asset Store
Simple yet very life-like rag doll made with Physics!
  • I have my game hosted in an iframe inside a page. The parent page needs to pass a token to the game. I have the parent page send a post message to the gamewindow. Inside construct, would this code be an eventscript made inside the eventsheet or is it a browser javascript action or is it a script to be added to the scripts folder under main.js ?

    window.addEventListener("message", (event) => {

    if (event.origin !== "http://example.org:8080")

    return;

    // ...

    }, false);

    Also, since the game runs in a worker, would window.addeventlistener work?

  • Bumping this. If the parent webpage is sending a message to the iframe with the game in it, how do I read the message inside construct? Is it through a browser.execjs event or a eventsheet inline js? Is there an example somewhere I can take a look at?

    Thanks in advance for the help.

  • This is what I do right now, is this right? This is an eventsheet add script section.

  • Ashley - Can you help me with this?

  • Adding a script file with the original code should work. Listening for the "message" event on window is the correct approach.

  • Cool I'll try it out today. For my learning, what is the difference between a script file vs inline event js like the one I posted?

  • The differences are documented in the manual: see scripts in event sheets and script files.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Based on this, a script as eventsheet action the way I have it in the screenshot should work right?

  • You should only add the event listener once. I can't see from the screenshot whether the event blocks do that. It would probably be best to add it in a script file, because even if it's under 'On start of layout', it could still add the listener multiple times if you keep revisiting the layout.

  • Yup! That makes a lot of sense. Thanks. One last question - If I were to put the listener at runOnStartup and set the message to a globalvar, on startup, globalvar might not have been initialized right? Or should I create a global var in script and then use it later in eventsheets?

  • Still can't seem to get this to work. This is my main.js file. Worker is turned on.

    Ashley - Am I missing something ?

  • Turn off worker mode. Otherwise your script is running in a worker, not the window, so won't receive messages to the window.

  • Finally, seeing the post message. One last bug I have is, the addeventlistener seems to not get added every time.I keep reloading the page and a few times, I see the console message and few times I don't, erratically.I can see the postmessage being sent from html but the console message inside the event listener isn't triggered. My understanding was that main.js is called automatically and the runOnStartup function is executed first before anything else. Any idea why it has this strange behaviour?

  • Solved this! In case someone stumbles upon this - What was happening was the postmessage was getting sent before construct had a chance to add event listener. Sometimes the eventlistener was added in time to catch the message, sometimes it was not. I extended the timeout of the postmessage and added a boolean flag to wait for the event to be caught before the loader moved to the next layout. Works fine now. Thanks Ashley for the help.

Jump to:
Order by:
Duplicate Topics
Posts
Views
Last Post
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)