How do I execute Javascript after game has loaded

Get help using Construct 2

Post » Thu Aug 20, 2015 12:50 pm

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

Code: Select all
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");
   }
}();


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?
B
3
Posts: 4
Reputation: 183

Post » Thu Aug 20, 2015 1:26 pm

You have to import the Browser-Object and then use "Execute Javascript". That should work with "on start of Layout".
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Thu Aug 20, 2015 4:03 pm

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?
B
3
Posts: 4
Reputation: 183

Post » Thu Aug 20, 2015 6:51 pm

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.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165


Return to How do I....?

Who is online

Users browsing this forum: Armench and 20 guests