Hmm I'm really lost.. I searched over the land and sea in the docs or blogposts but can't find anything of this. Who executes the function ImageLoadBar_hook()? Is it done by gamemaker? If so, where am I supposed to know that? I literally cannot tell what goes where and what gets executed.
I know I'm struggling the basics of GM-JS communication in itself, it would be great if you could reference something for me to look further. Thanks a bunch for your help.
After you import the extension, go into Options > HTML5 > General and change the the dropdown that says "Loading bar extension". Once you do that and build, the loading bar function you selected would be called periodically by the runner when it starts loading resources outside of the runner (i.e. everything else in
html5game except the main .js file). The runner would pass in information on the size of the canvas, the drawing context and the current progress, so that the loading bar function can draw the appropriate content.
Among all the extension options in GM, the one to JS on the HTML5 export is by far the simplest, so simple that they neglected to document it. Begin by starting an extension and adding an empty JS file to it. On the JS side, simply declare each function using the
function keyword. Here is an example:
Code:
function helloWorld() {
alert("Hello world!");
}
function myFunction(a, b) {
return a+b;
}
function myFunction2(name) {
return "Hello " + name + "!";
}
Then in GMS 2, double-click the extension and then the js file listed in the window that comes up. Now click the hamburger menu icon next to "Functions" and select "Add function". In there, set "Name" to the name you want to call it from the GML side (can be the same as or different from in the JS file; here we'll assume it's the same), "External Name" to the name of the function as shown in the JS file (
helloWorld, myFunction and
myFunction2 in the example), and add as many arguments as are shown in the JS file (0, 2, 1 respectively in the example). Repeat the "Add function" part once per JS function that you want to be directly accessible from the GML side. Once you're done that, you can use these JS functions from GML like this:
Code:
helloWorld();
show_message("1+1=" + string(myFunction(1, 1)));
show_message(myFunction2("Kitty"));
From there you'll probably want to learn how to do
JS-to-GML callbacks, but beyond that there really isn't much more to learn. I've done JS extension work before and know YoYo should have more ways to hook in (e.g. getting at runner-internal surfaces, buffers, data structures and other resources from external JS are currently off-limits, but everyone agrees that isn't the way things should be), but the basics are there and it's small enough to pick up in a quick read.
PS: I saw you talking back and forth with Mike on some thread something about this as well, and it basically resulted with Mike including
Code:
window.onload = GameMaker_Init
in every GM HTML5 index page. Does it have a correllation with this to be doing stuff before the game loads?
No, that line just starts the runner. The runner will call the loading bar extension somewhere down the line, but only after it starts.