[RMMV] (Twitch Integration) Seeking User Input Solution (and general advice).

So for a bit of fun I've been working on integrating an RMMV project into my twitch stream. I'm not too experienced with Javascript and my RMMV knowledge is a little rusty - so I thought I'd turn here for an assist.

It's a pretty simple setup so far but it's borderline functional - The issue I've hit hopefully has a simple solution to those more familiar with RMMV and Javascript, however several hours of online investigation and looking at the game code has yielded little actionable information (At least that I've managed to parse).

I'm seeking a way to allow a command in twitch chat to emulate keypress functions in-game, specifically an ideal method for input emulation.

I can create common event calls for most specific interactions, however I cannot seem to find a way to handle choice, menu and input windows. Are there functions that I'm missing that allow a script call to activate a selection from a choice window by its index? Can I call an input function ('ok', 'up', 'escape' ect.) directly? Or do I have to find every individual choice end-point and call it directly?

How would you do it?
I think you'd need to identify the window object within the current scene, then manually set the selected index and invoke the "OK" handler.

E.g. for a Show Choices command (Window_ChoiceList):
Code:
var w = SceneManager._scene._messageWindow._choiceWindow;  // get choice window
w._index = 3;   // set index
w.processOk();  // OK!
I know basically nothing about Twitch integration but I assume you've already got the "get index from chat" part covered. 😅

I think these would be the corresponding calls for "up", "down", and "cancel", respectively, for the choice window:
Code:
SceneManager._scene._messageWindow._choiceWindow.processUp(wrap);
SceneManager._scene._messageWindow._choiceWindow.processDown(wrap);
SceneManager._scene._messageWindow._choiceWindow.processCancel();
^ Replace wrap with a true/false value indicating whether the cursor should wrap between first & last indices. 😮‍💨

Other selectable windows (e.g. menus) will likely function much the same, due to inheriting from Window_Selectable (via Window_Command in some cases). For further details, check out rpg_windows.js in your project's js folder~ 🙏
Awesome - Thanks, I could not for the life of me find the processUp (and similar functions) anywhere... Window_Selectable seemed to just completely elude me. Thankfully - I do have the "get X from chat" part functioning, so this is exactly the kind of solution I was seeking. 🙂

Though, as I understand it - this method requires a reference to individual windows - which feels like it's going to get messy with nested interfaces (thinking specifically of the equip menus). Is there an easy way to reference the active window and/or check if it inherits from Window_Selectable/Window_Command? Then I could fairly easily just write a function that calls the correct command for each command instance based on the active window reference...
...
I think... 😅
To my knowledge there's no "active window" reference: each window has its own active flag, and all windows on a scene are updated simultaneously, responding based on whether or not they're active. In theory there could be multiple active windows, but I think there's only ever one in any of the default scenes. Supporting code excerpts (rpg_windows.js):
Spoiler: Some methods from Window_Selectable
Code:
Window_Selectable.prototype.update = function() {
    Window_Base.prototype.update.call(this);
    this.updateArrows();
    this.processCursorMove();
    this.processHandling();
    this.processWheel();
    this.processTouch();
    this._stayCount++;
};

Window_Selectable.prototype.processCursorMove = function() {
    if (this.isCursorMovable()) {
        var lastIndex = this.index();
        if (Input.isRepeated('down')) {
            this.cursorDown(Input.isTriggered('down'));
        }
        if (Input.isRepeated('up')) {
            this.cursorUp(Input.isTriggered('up'));
        }
        if (Input.isRepeated('right')) {
            this.cursorRight(Input.isTriggered('right'));
        }
        if (Input.isRepeated('left')) {
            this.cursorLeft(Input.isTriggered('left'));
        }
        if (!this.isHandled('pagedown') && Input.isTriggered('pagedown')) {
            this.cursorPagedown();
        }
        if (!this.isHandled('pageup') && Input.isTriggered('pageup')) {
            this.cursorPageup();
        }
        if (this.index() !== lastIndex) {
            SoundManager.playCursor();
        }
    }
};

Window_Selectable.prototype.processHandling = function() {
    if (this.isOpenAndActive()) {
        if (this.isOkEnabled() && this.isOkTriggered()) {
            this.processOk();
        } else if (this.isCancelEnabled() && this.isCancelTriggered()) {
            this.processCancel();
        } else if (this.isHandled('pagedown') && Input.isTriggered('pagedown')) {
            this.processPagedown();
        } else if (this.isHandled('pageup') && Input.isTriggered('pageup')) {
            this.processPageup();
        }
    }
};
isOpenAndActive, isCursorMovable, etc check the window's active flag, ensuring the relevant updates only apply if the window is active:
Spoiler: A couple of Window_Selectable's active flag check methods
Code:
Window_Selectable.prototype.isOpenAndActive = function() {
    return this.isOpen() && this.active;
};

Window_Selectable.prototype.isCursorMovable = function() {
    return (this.isOpenAndActive() && !this._cursorFixed &&
            !this._cursorAll && this.maxItems() > 0);
};

I figure you could write a getActiveWindow method for each scene, then use that to get the current window, e.g. you might end up with something like this:
  • "Get window" methods:
    Code:
    Scene_Base.prototype.getActiveWindow = function() {
      return null;
    };
    
    Scene_Title.prototype.getActiveWindow = function() {
      return this._commandWindow;
    };
    
    // similar override for other relevant scenes...
  • "Update Twitch input" code:
    Code:
    var w = SceneManager._scene.getActiveWindow();
    if (w && (w instanceof Window_Selectable)) {  // active window exists & inherits from Window_Selectable?
      // then do stuff!
    }
I assume it'd be easiest to stick everything in one plugin and add an alias for Scene_Base.prototype.update, to invoke the "Twitch input update" stuff immediately before/after the default update process. In case you're unfamiliar with the structure of an alias, here's a basic example:
Spoiler: Method alias example
Code:
var alias = Scene_Base.prototype.update;
Scene_Base.prototype.update = function() {
  // code to run before
  alias.call(this);  // invoke original method
  // code to run after
};
Each scene's update method is invoked once per frame by the SceneManager. For more details on the windows in each scene check out rpg_scenes.js~

Hope that makes (some) sense! 😤
Makes perfect sense, incredibly concise - and your post formatting is honestly a pleasure to read.

While I'm not familiar with an alias; based on your example - I assume it essentially appends/calls code whenever an existing class function is called - which sounds extremely useful for writing plugins. I appreciate the tutorage greatly. It might take me a little while to wrap my head around how to implement this functionally, but I'm mostly confident that I understand it conceptually. 🙏
Great, best of luck! 😂

Original thread: https://forums.rpgmakerweb.com/threads/rmmv-twitch-integration-seeking-user-input-solution-and-general-advice.115891/