How do I execute Javascript after game has loaded

0 favourites
  • 4 posts
From the Asset Store
five golem elements Sprites Sheet.Best for enemy or villain game characters.
  • Hello,

    New here so sorry if my understand of C2 is limited.

    I have an external Javascript files located in the same directory as the exported index.html which I am using to store all the external functions required for the game. I am using CallJS plugin to interface with this external script which works well in other area of the game.

    My problem here is I would like to be able to execute a function in the external script (I assume using CallJS is best here) as soon as the game is loaded. As an example here is my function

    var myObject = function() {
       function gameLoaded() { //this does not work
           //do something here as soon as game loaded
           console.log("game was loaded successfully");
    I have attached this to the system.OnStartLayout event but when the game has loaded this function is not firing (I am using the console log to test this). 
    How can I get this to run on first load?
  • You have to import the Browser-Object and then use "Execute Javascript". That should work with "on start of Layout".

  • Thanks for the response. I have included the browser object but it is unable to execute a function located in the external Javascript file, it throws a referenceError undefined which makes sense as it's unable to locate the function.

    So I guess what I'm asking is how to call a function in an external file?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Sorry, you are right. Execute from Browser is for execute Javascript as a string, not from a file.

    I tried CallJS and got also an error when I call it in on start of Layout. It seems that CallJS is to fast in execute the Javascript. So I add a Wait of 0.1sec from System before CallJS in "on start of Layout" and now it works.

    Maybe you have to alter the Wait-Time, but it seems that it works.

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