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,824
Reaction score
2,162
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

Okay so today I heard the dumbest theory I have heard in a loooooooong while. It's so stupid I just have to share... Appearantly I have learned to draw 'hot chicks' so that 'real men' get physically excited by looking at it and I can then swoop in and 'turn' them.... Wow... Just... Amazing how some brains works...
What the writer wrote: "The curtain is blue."
The readers: "There must be a hidden meaning behind this, perhaps to convey the character emotions ...".
What the writer has in mind: "The curtain is frickin' blue".
Time to share something!
hmZFwQe.png

I made these a while ago for my game. I took reference from various huts in Africa.
Have a great night! :D
I really need to buy a new mouse. Mine's keeps double-clicking and makes me waste A LOT of time eventing
How come there's no spooktober game jam? I thought horror maker games were some of the most popular genre out there...

Forum statistics

Threads
115,843
Messages
1,093,359
Members
151,031
Latest member
Florian973
Top