How do I communicate (send/receive data, do calls..) via JS?

For developers using the Construct 2 Javascript SDK

Post » Tue Mar 01, 2016 10:15 am

Here's the jist. We're creating a webpage, with a little bit of HTML & CSS (modals). We've made a back-end with API and the javascript from the page should load the data (via the API) locally. Now we modify and can work with the data (just on like any site).
Now I want my data i just brought in to be able to be used by construct2 and vica versa, be able to do calls to Construct 2 from our JS, and the other way around (from our JS to Construct 2). All of this will be inside an IFrame.

For example:

The game starts, construct 2 game runs in the background (some cars are moving around) while the first html & css modal appears on top of the construct 2. This is going to be a small questionnaire modal (age, where you're from, gender, ...) and all will be handled outside of the game/construct (the game is still running automatically in the back ground).

But then when the user completes his "registration" and clicks a button in html5/js ('Go to Game' or something), i have to trigger something in construct 2 so that the game goes from layout 1 to layout 2. I already know how to do this INSIDE construct 2, which is easy, but now i want it to work so that when a user presses a button in html5, a construct 2 function/event is called.

Example 2:

The game starts, does it's thing and after a certain period, say 5 seconds, another modal pops up with a question for the user. When he answers the question by clicking a button (in html5/js), the modal will disappear and something in construct 2 has to be triggered so that the car moves forward. When the car hits a certain point, another call has to go out from Construct 2 to our JS so that another modal may pop-up. etc...

Does anyone know how to do this exactly?
B
6
S
1
Posts: 50
Reputation: 517

Post » Tue Mar 01, 2016 10:56 am

@rexrainbow hi there, this is just a test because i have no other way of contacting you (semi-)directly. I don't have 500 reputation yet therefor Scirra doesn't allow me to PM people (to counter spam users). If you receive a notification of this tag, could you please take a look at my issue when you can? On another post I made, you answered me your repo of several tens/hundreds of plugins. I'm unfortunately on a (sort of) tight schedule, so I can't spend hours digging in your most hughe and great collection of plugins.

Maybe you could help me with my issue directly, or send me on my way referring to a plugin you previously made that's sort of the same?

MUCH appreciated!
B
6
S
1
Posts: 50
Reputation: 517

Post » Tue Mar 01, 2016 11:03 am

The best way: use the Javascript SDK

Hacky quick fixes: the Browser object can execute Javascript, and you can call the Function object functions from JS.
Scirra Founder
B
400
S
236
G
89
Posts: 24,546
Reputation: 195,471

Post » Tue Mar 01, 2016 11:12 am

Message: horoblast can only post plain text URLS until they have 500 rep. 2 URLS modified. Why?
Ashley wrote:The best way: https://www.scirra.com/manual/15/sdk use the Javascript SDK

Hacky quick fixes: the https://www.scirra.com/manual/110/browser]Browser object[/url] can execute Javascript, and you can call the [url=https://www.scirra.com/manual/149/function Function object functions from JS.


Ah yes thank you, this is getting me partially where i'd have to be, but i'll get there ;). So say i wanted to call a function in a JS file somewhere, then I just use the execJS from browser? How does it know which file to look into?

And then, when writing the own plugin, where do i put my functions so they can be used in construct 2? Like a function Next() in my JS file that checks if the data has been sent to a DB (like age, gender, etc...) and then that function calls a function object in Construct 2, that then initiates a set of actions?
B
6
S
1
Posts: 50
Reputation: 517

Post » Tue Mar 01, 2016 1:15 pm

You might see the source code of official websocket plugin first.
To include external file, add
"dependency": "file_name"
in edittime.js, see SDK for more detail.
B
109
S
27
G
278
Posts: 4,482
Reputation: 155,424

Post » Tue Mar 01, 2016 3:01 pm

Message: horoblast can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
rexrainbow wrote:You might see the source code of https://www.scirra.com/manual/153/websocket official websocket plugin first.
To include external file, add
"dependency": "file_name"
in edittime.js, see SDK for more detail.


Ah great, thank you. I'll figure it out ;).
B
6
S
1
Posts: 50
Reputation: 517

Post » Sun Mar 06, 2016 8:48 am

We created a plugin that can handle cross-domain post-messages.

So, let's say when the user presses the button in the modal window you send a post-message and the plugin condition (Cnd) catches it and updates construct.
Works both ways - when construct finishes something, it uses an actions, say AwesomePlugin.update, and it sends a post-message to the browser window.
Works flawlessly.

Hope this helps somehow.

We also use this plugin to handle iframes/cross-domain.. etc.
(I don't have 500 rep so search for "porthole js")
B
5
S
1
Posts: 10
Reputation: 295


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests