Functions for Publishers

Discussion and feedback on Construct 2

Post » Thu Apr 18, 2013 4:27 pm

Hello, I have a bit of a technical question. I am working with a publisher who has requested I insert some functions into the code (such as PUBLISHER_GAMESTART which they can call to begin the game or PUBLISHER_SUBMITSCORE to collect highscore data). These are requested so they can hook the game into their API.

My original assumption was that I could simply use the Function plugin to accomplish this. For instance I have a loader layout which moves to the title screen only when the function "PUBLISHER_GAMESTART" is called using the "On function" event.

Unfortunately, I have no way of knowing if the javascript developers who work with this company will be able to access the functions via a normal text editor. Looking at the code for myself, it looks rather mangled and does not resemble functions I have seen in the past (though this may be a result of my inexperience). The code is not minified.

I think my question, then, is: If I tell the publisher that the function they can call is "PUBLISHER_GAMESTART"--will they be able to accomplish this through their own methods?
B
10
S
2
Posts: 19
Reputation: 1,245

Post » Thu Apr 18, 2013 4:41 pm

the function plugin dont create a function like

function myFunction(param1){

}

It's something used by the construct 2 engine for the event system and I dont think you can access it externaly.

Instead, I would create a plugin for your publisher using the construct SDK
B
12
S
3
G
4
Posts: 62
Reputation: 3,408

Post » Thu Apr 18, 2013 5:18 pm

The Function object works exclusively with events, it does not work with Javascript. For your case you need to use the Javascript SDK.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,468

Post » Thu Apr 18, 2013 5:23 pm

Miste is right. the Function Plugin is not actually javascript function code.

For what you want. I suggest either trying one of the javascript plugins or writing your own plugin that provides the publisher hooks.

Unfortunatly there is no easy way for this to work. Unless of course the js plugin can do what you want.

http://www.scirra.com/forum/plugin-call-javascript_topic45866.html
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Thu Apr 18, 2013 8:34 pm

This is actually not hard to do, you just need to use the plugin that jayderyu mentioned. Then it's as easy as adding the "Execute" action from that plugin to any part of your game. For instance, after a button is pressed, add the CallJS execute action and enter one of the names of the javascript functions the publisher needs, like "PUBLISHER_SUBMITSCORE()".

You don't need to do anything else.

As for the function to start the game it's a bit more technical but still easy. You can't add that function inside C2, you need to add it after you export the game by editing the index.html with a text editor. You only need to make 1 edit:

Find the line: // Start the Construct 2 project running on window load.

There you'll find the following code:

     [code]jQuery(document).ready(function ()
          {              
              // Create new runtime using the c2canvas
              cr_createRuntime("c2canvas");
          });
[/code]

You need to replace all of that with this new code:

[code]     {              
     function PUBLISHER_GAMESTART()
{
   jQuery(document).ready(function ()
              // Create new runtime using the c2canvas
              cr_createRuntime("c2canvas");
          });
}
[/code]

On the new code, replace PUBLISHER_GAMESTART with any name you need for the function to start the game. Everything should be ready after that.Xavier2013-04-18 20:54:43
B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Thu Apr 18, 2013 10:07 pm

That makes sense. I'll see how well I'm able to put it together for them and let you know the results. They also want some empty functions so they can add in their content. I'm assuming I should just include some hollow functions in the external js file the plugin requires and then call the functions within C2.

Thank you so much for the help! I'm not sure how I could have done this without some input from people who know more about the undercarriage of the program.

Edit: Ok, so I have run into a bit of a hiccup. It's fairly straight-forward calling functions which exist outside of Construct, but what if the publisher wants a function which they can call to affect the game? In this case, they want to be able to pause the game by calling PUBLISHER_TOGGLEPAUSE().

I have looked around for further help on the CallJS plugin, but very little exists. I can't figure out what the events included in the plugin do, or if they would be of any help. I thought maybe I could have PUBLISHER_TOGGLEPAUSE() flip a variable on and off in the js file which CallJS could then read, but I'm not sure CallJS does any reading at all.ryguydavis2013-04-18 22:50:48
B
10
S
2
Posts: 19
Reputation: 1,245

Post » Fri Apr 19, 2013 1:30 am

You should just use the Javascript SDK. CallJS amounts to a very hacky version of doing what the SDK does well, and the SDK also allows for other javascript events to fire triggers in the event system, which should do what you want.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,468


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests