• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

HTML5 Javascript extension with callbacks and events - how?

DukeSoft

Member
Heyall,

I'm working on an extension for HTML5 javascript which will be using certain events.

I know we can make functions in the JS files and call them from GM - but how can I do this the other way?

Would there be a way to bind GameMaker events (such as User Event 0) to an "event" from the JS library?

e.g:

JS:
Code:
document.onResize = function(eventData) {
    console.log('Window resize event');
    my_callback_function(eventData); //This being the GM function / event
}
GameMaker:

Script my_callback_function
Code:
show_debug_message('Received resize event function with data ' + string(argument0));

Because this would be best. If this is not possible i'll have to go with a messagecue.. Filling the message queue with messages and reading it out in a step event in GM.
 

Freddy Jones

Your Main Detective
It is possible to execute a Game Maker script using JavaScript. It fully relies on how you name the script in your game, though.

Here's how to make it javascript ready:

1. Create a script
2. Make sure the name of your script starts with gmcallback
3. Your script is now in the global scope.

Here's how you access it:
1. Your script is in the global scope, but it is not called "gmcallback..._my_script"
2. The name of your script in JS is "gml_Script_gmcallback" + whatever you added after gmcallback.

Take for example:
Game Maker Script "gmcallback_hello"
Code:
///gmcallback_hello()
show_debug_message("hello");
Run it in JavaScript
Code:
gml_Script_gmcallback_hello(); // Directly

var scr = "gmcallback_hello";
window["gml_Script_"+scr]();// dynamically

Edit:
If you wanted to invoke events from JavaScript to your GML just make a wrapper GML script that can do it for you.

Say you wanted to be able to dynamically pass a javascript function to your GML script and call it. You could make an array that stores the functions and can be later invoked by passing the index and such.
Example:
Code:
let savedFns = [
function(){

},
];

/// Method you add into game maker to be used for dynamic/anonymous callbacks
function executeFunction( ind, ...params ){
    var cback = savedFns[ ind ];
    cback.apply(this, params);
}
 
Last edited:
S

Simon Keating

Guest
It is possible to execute a Game Maker script using JavaScript. It fully relies on how you name the script in your game, though.

Here's how to make it javascript ready:

1. Create a script
2. Make sure the name of your script starts with gmcallback
3. Your script is now in the global scope.

Here's how you access it:
1. Your script is in the global scope, but it is not called "gmcallback..._my_script"
2. The name of your script in JS is "gml_Script_gmcallback" + whatever you added after gmcallback.

Take for example:
Game Maker Script "gmcallback_hello"
Code:
///gmcallback_hello()
show_debug_message("hello");
Run it in JavaScript
Code:
gml_Script_gmcallback_hello(); // Directly

var scr = "gmcallback_hello";
window["gml_Script_"+scr]();// dynamically

Edit:
If you wanted to invoke events from JavaScript to your GML just make a wrapper GML script that can do it for you.

Say you wanted to be able to dynamically pass a javascript function to your GML script and call it. You could make an array that stores the functions and can be later invoked by passing the index and such.
Example:
Code:
let savedFns = [
function(){

},
];

/// Method you add into game maker to be used for dynamic/anonymous callbacks
function executeFunction( ind, ...params ){
    var cback = savedFns[ ind ];
    cback.apply(this, params);
}
This is great but how do I send a variable from javascript back into Gamemaker? For example, I have an input text box prompt and I want that input text to be sent back into GM. Thanks
 

klys

Member
I were looking for this post all the day, i remember i read this before, and was exactly here.

I made my own research in the ofuscastion code and i found that for the third argument in the javascript functions start the first argument you use on GML script, so for example i create the following script in GML
Code:
/// gmcallback_test
show_message("Test This."+string(argument[0]))
and in the javascript console i call it like this:

gml_Script_gmcallback_test("","","Test More...")

And it worked showing up the message with the initial message on GML and after it the message on the third argument.

Thank you very much.
 

FrostyCat

Redemption Seeker
He meant that it has not been officially documented for all this time.
That is old news. The current Manual for GMS 2 most certainly documents this feature.
The Manual said:
Please note that if you are developing for Web (ie: targeting HTML5), then there is an additional script protocol that you can use, which is to preface a script name with gmcallback_, for example:

Code:
gmcallback_create_button
Using the above script name would mean that the script gmcallback_create_button will not be obfuscated and so can be used in JavaScript extensions and other areas of your game, for example, when using the clickable_* functions.
 

Freddy Jones

Your Main Detective
I don't even use game maker anymore, for like 2+ years, sorry. i just don't know what's up to date anymore and assume if people are looking it's lacking.
 
Top