arvindm1991's Forum Posts

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Still can't seem to get this to work. This is my main.js file. Worker is turned on.

    Ashley - Am I missing something ?

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

  • Based on this, a script as eventsheet action the way I have it in the screenshot should work right?

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

    I'm cross posting this from the scripting forum since it's been close to a month and my work is stalled due to this. I'm a bit confused about the various scripting options - adding a script file vs adding an inline event script vs adding a browser.execjs, I'm not sure how to get a postmessage posted to a game inside an iframe. This is the topic I created in the scripting forum. Can anyone point me to an example? The way I have it setup is my game is hosted in an s3 bucket and it is being loaded in an iframe inside a webpage.

    construct.net/en/forum/construct-3/scripting-51/receive-postmessage-inside-162472

  • Ashley - Can you help me with this?

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

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

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

  • Hey Ashley,

    I'm not particular about scripting. I just assumed this wouldn't be possible without scripting since webstorage is deprecated. I wasn't able to use the API while using worker, so I'd prefer to avoid it if possible. Are there other ways of doing this?

  • My game is inside of a website with a login. Login generates a auth token that gets stored in localstorage (webstorage). Since webstorage is deprecated, how do I pass the token into the game? Since tokens can be long, passing it in url params might not be the best idea.

  • I have a website that stores login info of a user in a localstorage key on login. A few clicks later, user opens the game which is hosted within the same server. How do I get access to the example.com localstorage entries?

  • Ah realized my error! I was beautifying the inner json but not setting the actual put json string correctly. Both Ashley and dop2000 were right. In case someone stumbles upon this, this is what I did to get it to work - beautify the "nesting" json and then add it to the json I was going to use in the ajax call. Then beautified the json and used it in the ajax call. This works now for me without scripting. Thanks for the help.