0 Favourites

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

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

    Is possible?

  • Are you trying to call a c2 function from the page your game is embedded on?

    -- REMOVED --
    [/code:1qbwhd75]
    [ul]
    [li]0 : Function Name ('String')[/li]
    [li]1 : Function Arguments ('Array' & 'Optional')[/li]
    [li]2 : Function Object Name ('String' & 'Optional') -> usually 'Function' unless renamed[/li]
    [li]3 : Frame Object ('Object' & 'Optional') -> if you embed your game pass a reference to the iframe[/li][/ul]
    
    -Add Function object to your project and add a new Function ... i.e : 'callback'; 
    then call it from the page with callFunc('callback');
  • 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?

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

    ...  <p id='my_tag'>{{PlaceHolder}}</p> ....[/code:3u6olnub]
    Browser Execute JavaScript "my_tag.innerHTML = 'New Content'"
  • Noncentz705

    I've followed your instructions and I made it:

    -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:

    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(); ?

    • First part about getting and setting variables.
    • Setting variable example :

      On some event Execute javascript "(function(){window.foo = 'something'})()"[/code:wl21003j]
    • Getting variable example :

      On some event set text to Execute Javascript "(function(){return window.foo;})()"[/code:wl21003j]

    Then about the callFunc part it maybe the picture will explain better ...

    > [attachment=0:wl21003j][/attachment:wl21003j]

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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?

  • 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

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

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

    "var d= document.createElement('div'); d.id =  'my_div';  document.body.appendChild(d);
          d.onclick = function(){return 'SOME_VALUE';}
    "
    [/code:2b3g97st]
    When you want return value 
             Text set text to [code:2b3g97st]Browser.ExecJS("(function callback(){return my_div.onclick();})();")[/code:2b3g97st]
  • Noncentz705 Sorry I annoyed you, I couldn't figure out about 'callFunc', I've spent like 3 hours figuring out.

    About return values,

    Well, I've done this before but I'm not sure if you understand what I'm saying, let me explain better, when the page has <input> button IS NOT INSIDE of C2 canvas, click button to destroy a sprite. I don't know what's event or condition to get onclick(); from HTML elements are out of canvas.

    Does it make sense?

  • Joannesalfa, not annoyed just busy. If you can't figure things out maybe someone else can help while I'm gone?

    Quick answer for now... make an input in c2, set id my_c2input, and give on click event with event sheet

    example on clicked call my_callback_function

    Then with your other input (the one not inside c2) have it click my_c2input by giving it an onclick="my_c2input.onclick();" either inline or with javascript.

    and about the callFunc code just open browser console and test a bit or ignore it for now. Sorry can't help more right now.

  • Noncentz705 Thanks for your time, I really appreciate your help, I would like to request a last thing.

    I'm having a problem how to set expression properly in javascript execute

    "JSONResponse.innerHTML = '& AJAX.LastData' "

    I got returned was:

    & AJAX.LastData

  • Joannesalfa it look's like the '' are the problem

    Global text global_text = 'Some value'

    "var hold = "&"'"&global_text&"';"&"; window['my_input_id'].value = hold;"

    not sure why i had to escape it like that

  • Noncentz705 Sorry for missing this thread, your last answer worked like a charm, I know it's hard but it's neccesary like JSON format, C2 can't handle to get correct format.

    Thank you a lot! you was very useful to resolve my problem after all.

    My experience has been improved thank to you.

    Also the users are looking to call javascript actions out of canvas, this thread would be helpful.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)