0 Favourites

How do I execute Javascript after game has loaded

  • 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

    someScript.js
    
    var myObject = function() {
       function gameLoaded() { //this does not work
           //do something here as soon as game loaded
           console.log("game was loaded successfully");
       }
    }();[/code:lvtza9g3]
    
    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?
  • Construct 3

    Buy Construct 3

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

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

  • 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)
Similar Topics Posts Views Last Post
Unread sticky
763 305,734
asikanddestroy's avatar
asikanddestroy
Unread hot topic
232 177,700
Tetriser's avatar
Tetriser
Unread hot topic
266 15,909
GameeApp's avatar
GameeApp