[SOLVED]How do I Callback in outside of canvas?

Get help using Construct 2

Post » Wed Jul 02, 2014 10:48 am

I need to know how to make an callback using javascript variables to HTML document outside of Canvas?

Is possible?
Last edited by Joannesalfa on Tue Jul 15, 2014 8:21 am, edited 1 time in total.
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Thu Jul 03, 2014 3:25 am

Are you trying to call a c2 function from the page your game is embedded on?
Code: Select all
-- REMOVED --

  • 0 : Function Name ('String')
  • 1 : Function Arguments ('Array' & 'Optional')
  • 2 : Function Object Name ('String' & 'Optional') -> usually 'Function' unless renamed
  • 3 : Frame Object ('Object' & 'Optional') -> if you embed your game pass a reference to the iframe

-Add Function object to your project and add a new Function ... i.e : 'callback';
then call it from the page with callFunc('callback');
Last edited by Noncentz705 on Sun Jan 04, 2015 6:43 pm, edited 1 time in total.
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Thu Jul 03, 2014 6:13 am

@Noncentz705 Yes, I want to grab one of the variables to HTML from C2 Canvas to set text in <p>Current Variable</p>

I saw your code, I don't get, it seems runtime.js for new plugin or is just hack in javascript?

Sorry for my confusion, I'm not expert at programming, would you explain more simple?
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Thu Jul 03, 2014 4:24 pm

@Joannesalfa , it's not code for a plugin but instead something you would add to the page your game sits on to trigger c2 functions. A quick example :
- Make a c2 project that has a function named 'callback'
- After exporting the project, add the js from above to index.html, and then you can call the 'callback' with callFunc('callback')


However it sounds like you're trying to do the opposite and update an element of the page your game sits on from c2;
If that's the case then you don't need the code, just use the browser object and execute js.
Example :
Code: Select all
...  <p id='my_tag'>{{PlaceHolder}}</p> ....

Browser Execute JavaScript "my_tag.innerHTML = 'New Content'"
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Fri Jul 04, 2014 5:52 am

@Noncentz705

I've followed your instructions and I made it:

http://www.powerupware.com/callback/

-Change <p> worked like charm, is possible to manipulate var Variables like:

var x = "Idle"

to

var x = "Done" ?


-About Callback, there another <script> is included from your code and added a function in project:


Image

and added <button> in outside of canvas, I was unsure that you told me.

I clicked both and they don't anything, isn't there a way to add alert(); ?
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Fri Jul 04, 2014 8:49 am

    First part about getting and setting variables.
  • Setting variable example :
    Code: Select all
    On some event   Execute javascript "(function(){window.foo = 'something'})()"
  • Getting variable example :
    Code: Select all
    On some event set text to Execute Javascript "(function(){return window.foo;})()"


Then about the callFunc part it maybe the picture will explain better ...
>
Untitled.jpg
You do not have the required permissions to view the files attached to this post.
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sun Jul 06, 2014 7:33 am

@Noncentz705 Sorry for late reply, I've tried about getting and setting variables worked like a charm.

About callFunc, well it seems breaking because it seems different than another code callFunc you posted it because I can't get this code because last "}" is missing, would you mind to post .capx instead of complicated format?
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Sun Jul 06, 2014 8:24 am

@Joannesalfa maybe an easier way to trigger functions from the page would be to create a button in your c2 project give it an id and on click do something.. Then with js you should be able to call that event with id_name.click(); A lot less code lol
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sun Jul 06, 2014 10:33 am

@Noncentz705 I understand but isn't possible to get return from id_name.click(); to this C2 Canvas?

By the way, are you refusing to share your .capx what you did in this screenshot?
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Sun Jul 06, 2014 5:36 pm

Only difference is that callFunc is assigned to window['callFunc'], anything after the = sign is the same. If you can't figure it out I can remake the capx... I hadn't saved it before.


Question about return values
On Start of layout ('or make the element in index.html after export '
Code: Select all
"var d= document.createElement('div'); d.id =  'my_div';  document.body.appendChild(d);
      d.onclick = function(){return 'SOME_VALUE';}
"

When you want return value
Text set text to
Code: Select all
Browser.ExecJS("(function callback(){return my_div.onclick();})();")
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Next

Return to How do I....?

Who is online

Users browsing this forum: boybacteria, dop2000, Microbex and 27 guests