javascriptgame-makergame-maker-languagegame-maker-studio-1.4

Game Maker JS Extension


Hi I'm looking to add javascript functions to game maker, but the format of them is like this:

companyname.initialize({ 
    soundMuteCallback: muteSound,           // optional
    soundUnmuteCallback: unmuteSound        // optional
});

And in the file they look like this

this.initialize = function(params) {
    companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);

    _isInitialized = true;

    if (typeof params !== "undefined") {
        var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
        var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
        _adsManager.setSoundCallbacks(function() {
            typeof muteSoundCallback === "function" && muteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = true;
            });
        }, function() {
            typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = false;
            });
        });
    }

    _tryShowAd();
};

Does anyone have any idea how to do this in game maker? I don't know what information to put in the extension function properties.

Thanks, Mitchell.


Solution

  • I would recommend creating a new function that Game Maker can understand and then use that to create your object and the constructor you are showing here.

    company.initialize = function(params) {
        companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);
    
        _isInitialized = true;
    
        if (typeof params !== "undefined") {
            var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
            var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
            _adsManager.setSoundCallbacks(function() {
                typeof muteSoundCallback === "function" && muteSoundCallback();
                [].forEach.call(document.getElementsByTagName("audio"), function(element){
                    element.muted = true;
                });
            }, function() {
                typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
                [].forEach.call(document.getElementsByTagName("audio"), function(element){
                    element.muted = false;
                });
            });
        }
    
        _tryShowAd();
    };
    
    function createMuteCallback() {
        muteCallback = function () {
            // Code to handle the callback
        }
        return muteCallback;
    }
    
    function createUnmuteCallback() {
        unmuteCallback = function () {
            // Code to handle the callback
        }
        return unmuteCallback;
    }
    
    function createCompany (mute, unmute) {
        if (mute == 1) {
            soundMuteCallback.createMuteCallback();
        }
    
        if (unmute == 1) {
            soundUnmuteCallback.createUnmuteCallback();
        }
    
        company.initialize(soundMuteCallback, soundUnmuteCallback);
    }
    

    So all of that goes in the same .js file. Create a new extension in Game Maker. Add the .js file to that extension. Add a function named createCompany with two optional parameters.

    Then when you call createCompany(1, 1); in your Game Maker code, the .js file will run and will initialize the company object with the two callback functions.

    Hope this helps.