Kono

Villager
Member
Joined
May 9, 2021
Messages
9
Reaction score
5
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,855
Reaction score
2,191
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
9
Reaction score
5
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
9
Reaction score
5
First Language
French
Primarily Uses
RMMZ
Thank you for this explanation !
 

Latest Threads

Latest Posts

Latest Profile Posts

A certain person better sell his stock while he still can, because it looks like it's going to take an unrecoverable nose dive.
7 years of trying to plan this project... and it got an official plotline today. 7 years. I really just have no concept of order huh GHJGSJHGVGHASFH.
Doing RPG Maker News for 24th October 2021

I'm not sure how to do this so I'll just ask here, Wheres a good place to post your game (for free) once its done?
I don't know what I'm doing :D

Forum statistics

Threads
116,063
Messages
1,095,437
Members
151,422
Latest member
otwm
Top