Kono

Villager
Member
Joined
May 9, 2021
Messages
7
Reaction score
3
First Language
French
Primarily Uses
RMMZ
Hello,

I want to add an entry in the title menu to open an URL, using the player's browser. I have the code to do that, but when I open the URL, I cannot interact anymore with the game, as if I lost the focus. I can still refresh the game with F5, I can still here the main menu music playing, but I can't scroll, or anything in the menu.

JavaScript:
(function () {

    var commandList = Window_TitleCommand.prototype.makeCommandList;

    Window_TitleCommand.prototype.makeCommandList = function () {
        commandList.call(this);
        this.addCommand("CLICK ME", 'openURL');
    };

    var commandWindow = Scene_Title.prototype.createCommandWindow;

    Scene_Title.prototype.createCommandWindow = function () {
        commandWindow.call(this);
        this._commandWindow.setHandler('openURL', this.commandOpenURL.bind(this));
    };

    Scene_Title.prototype.commandOpenURL = function () {
        require('nw.gui').Shell.openExternal('https://example.com/');
    };

})();

There may be a subtlety that I did not understand.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,827
Reaction score
2,164
First Language
EN
Primarily Uses
RMMZ
Have you tried using the native open method instead? E.g.
JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
  window.open("https://example.com");
};
More info:

Edit: no sorry, I just tested and got the same freeze effect. :kaoslp: Doing more testing...

Edit 2: OK, I'm not sure why it freezes, but this sort of thing seems to work:
JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
    const url = "https://forums.rpgmakerweb.com";
    if (Utils.isNwjs()) require('nw.gui').Shell.openExternal(url);
    else open(url);
    SceneManager.goto(SceneManager._scene.constructor);
};
I.e. go to "this scene" immediately afterwards. I guess making the window lose focus mid-loop might be interrupting the game loop in some way.

Edit 3: better solution + explanation by casper below! :kaohi:
 
Last edited:

Kono

Villager
Member
Joined
May 9, 2021
Messages
7
Reaction score
3
First Language
French
Primarily Uses
RMMZ
It works like a charm, thank you !
 

casper667

Casper Gaming
Veteran
Joined
Mar 15, 2012
Messages
326
Reaction score
444
First Language
English
Primarily Uses
RMMZ
Just wanted to add that the "freeze" happens because when OK input is processed, the window is deactivated by default. The game isn't frozen, the window is just deactivated and not accepting inputs which might give the appearance of a freeze. You can see this in Window_Selectable.prototype.processOk:

JavaScript:
Window_Selectable.prototype.processOk = function() {
    if (this.isCurrentItemEnabled()) {
        this.playOkSound();
        this.updateInputData();
        this.deactivate();
        this.callOkHandler();
    } else {
        this.playBuzzerSound();
    }
};

All you should need to get around this is
JavaScript:
this._commandWindow.activate();

For example:

JavaScript:
Scene_Title.prototype.commandOpenURL = function () {
    require('nw.gui').Shell.openExternal('https://example.com/');
    this._commandWindow.activate();
};

Although as caethyril has done you should really check if you are in an environment where "require" is defined or else it'll crash (such as on mobile/web), and use the native open as a backup so the full code would be

JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
    const url = "https://forums.rpgmakerweb.com";
    (Utils.isNwjs()) ? require('nw.gui').Shell.openExternal(url) : window.open(url);
    this._commandWindow.activate();
};
 

Kono

Villager
Member
Joined
May 9, 2021
Messages
7
Reaction score
3
First Language
French
Primarily Uses
RMMZ
Thank you for this explanation !
 

Latest Threads

Latest Posts

Latest Profile Posts

Apparently MZ lets you preview move routes in the move route editor without having to start up test game. That could save a lot of time during development compared to MV, especially for a long game that uses a lot of event movement for cutscenes.
A repost of an old meme I had made, now with music

I have a really hard time keeping all these goodies secret til December... but I swear, it is worth the wait!
Working on a Premium version of my minimap plugin that will have advanced functionalities such as drawing a Terrain map based completely on the users tilesets, whatever they may be, and drawing minimap radar icons automatically based on texture of the event the note is applied to.

Forum statistics

Threads
115,859
Messages
1,093,525
Members
151,081
Latest member
MOLLARTA
Top