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,862
Reaction score
2,198
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

It does sometimes get a little embarrassing that I often come here to ask for help with something and seldom have anything to actually show for it. If there is one thing though I can commend myself for, it's for sticking with an idea I've been tinkering with for this long now and gradually making headways. Even though I've been reserved about my progress, I can say I'm farther now than where I initially started.
I'm gonna put my project on pause for a tiny bit so I can explore the engine outside of it... Winging it can only get you so far LMFAO
Have seen some of my favorite people here posting and helping others with questions today -- love to see it!
Haven't been on here actively in awhile, project is going strong my biggest issue is the STORY, i'm refining the plot and learning so much i decided to take a writers class to improve even more but the programming and functionality of the game is stepping up nicely!

Forum statistics

Threads
116,128
Messages
1,096,018
Members
151,552
Latest member
Davna90
Top