How do I trigger event from javascript in both Construct 2 and Construct 3?

0 favourites
  • 3 posts
From the Asset Store
Game Triggers is as versatile as a Swiss army knife, and will be a great resource for any type of game you're creating.
  • I have a large back catalog of Construct 2 and Construct 3 projects that I am looking to integrate with a customer's database backend using javascript. I am looking for a way to trigger a Construct event using javascript.

    For Construct 2 I found the following in the manual:

    "Javascript integration

    It is strongly recommended to use the Javascript plugin SDK to integrate Javascript code with Construct 2. However it is possible to trigger a function in the Function object from Javascript code using the following function:

    if (c2_callFunction)

    c2_callFunction("name", ["param1", "param2"]);

    Note if the Function object is not included in a project, the c2_callFunction function will not exist, so the if check is necessary before using it. The function with the given "name" is triggered synchronously. Parameters are optional and can be omitted, but must be provided as an array in the second argument, and parameters may only be string or number values (any other types will return as 0 in Construct 2). The c2_callFunction method also returns the return value set in Construct 2 (if any), and also can only return a string or number."

    I haven't found an equivalent in Construct 3.

    I would like to create a single event sheet template for both C2 and C3 that I can use to integrate with my client's javascript functions. I originally tried creating an HTML button in Construct, then modifying the "value" of the button and reading it as "button.text" in the event sheet, but this didn't work because the "text" property in Construct 3 is not changed when its HTML element "value" is modified in javascript.

    Is there a way to trigger an event from javascript in both Construct 2 and Construct 3 without repeatedly running Browser.ExecJS?

    I would prefer to use a single event sheet that works in both C2 and C3.

    I would also prefer to not have to learn both the C2 and the C3 javascript SDK.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • EDIT - It looks like

    window.c3_callFunction("getUser")

    exists in Construct 3 as well. I should be able to write a single event sheet that works with both C2 and C3.

    Ashley Is there any possibility that c3_callFunction will be removed in future releases?

  • PROBLEM #1 (SOLVED) - MAKING PROMISES WORK IN BROWSER -> EXECUTE JAVASCRIPT

    I'm now struggling with integrating with the existing API built for my client. The API relies almost entirely on javascript promises. I have found that promises don't work directly in Browser -> Execute Javascript() because promises don't work with eval(), and that is the basis for the Construct event.

    I found that I have been able to work around this by wrapping the promise in an anonymous function, casting the MOPusername promise as a new MOPusername object:

    Browser -> Execute Javascript "MOPusername = Service.GetUser().then(GetUserResponse => { document.getElementById(""getUserResponse"").value = GetUserResponse.Username;

    //GetUserResponse.IsGuest

    //GetUserResponse.Username

    return GetUserResponse;

    //if(ErrorOccurred (GetUserResponse)) {

    // abort

    //}

    }).then(result => {return result.Username});"

    Browser -> Execute Javascript "(async () => {MOPusername = await MOPusername})()"

    PROBLEM #2 - MAKING .then() CALL c2_callFunction() IN BROWSER -> EXECUTE JAVASCRIPT

    The next problem is proving to be beyond me. For some reason I can't get c2_callFunction() to work within a promise when called from BROWSER -> EXECUTE JAVASCRIPT.

    The following works when typed into the console. The expected result is .Success = 1:

    When run from inside Construct 3, however, it gives a different result of .Success = 0:

    Ashley, is there anything going on that would wait for the promise to be resolved in the console before c3_function("setUserResult") is called, but NOT wait for the promise when c3_function("setUserResult") is called inside a Browser -> Execute Javascript within Construct 3?

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