RPGツクールMVでWindow_Commandの使い方のまとめ

RPGツクールMVにおけるWindow_Commandクラスの使い方についてまとめていく。

Window_Command とは?

選択肢を簡単に作れるクラス。

「はい」「いいえ」とかの選択肢を簡単に作れる。

Window_Commandの使い方

新しいクラスを作成してWindow_Commandを継承することで使う。

例えば、以下のように使う。

重要なのはmakeCommandListメソッドを定義して、追加したいコマンドを定義すること。

/**
 * Window_CancelConfirmクラス - クエストキャンセル確認用のウィンドウ
 * 
 * @note windowの位置は、インスタンス生成時に引数で指定する。
 */
class Window_CancelConfirm extends Window_Command {
    /**
     * コマンドリストの作成
     */
    makeCommandList() {
        this.addCommand('はい', 'yes');
        this.addCommand('いいえ', 'no');
    }

    /**
     * Windowのwidthを指定できる。(option)
     */
    windowWidth() {
        return 240;
    };

    /**
     * Windowのheightを指定できる。(option)
     */
    windowHeight() {
        return this.fittingHeight(this.numVisibleRows());
    };
}

addCommandの引数は以下のようになっている。

addCommand('メニュー名', 'メニューのシンボル', isEnable: bool);

第3引数のisEnableはオプションであり、falseにすると選択不可にできる。

そして、Scene_MenuBase内でWindowの設定をする。createメソッド内で定義してあげること。

(厳密には``Scene_MenuBase`は必須ではないが、一番ラク)

class Scene_Sample extends Scene_MenuBase {

    create(){
        super.create();

        // x: 200 y: 300 の位置にwindowを作成する。
        this._cancelConfirmWindow = new Window_CancelConfirm(200, 300);

        // setHandlerでメニューが選択された時の挙動を指定する。
        this._cancelConfirmWindow.setHandler('yes', this.onCancelConfirmYes.bind(this));
        this._cancelConfirmWindow.setHandler('no', this.onCancelConfirmNo.bind(this));
        this._cancelConfirmWindow.setHandler('cancel', this.onCancelConfirmNo.bind(this));

        this.addWindow(this._cancelConfirmWindow);
    }
}

また、cancelというシンボルの設定もしているが、これは右クリックをした時などの挙動を指定できるもの。

およそ、handlerで呼びだす関数でやることは何かしらの処理をした後に、あるwindowは閉じてあるwindowは開くといった 行為になる。

onCancelConfirmNo() {
    this._cancelConfirmWindow.close();
    this._questListWindow.open();
    this._questListWindow.activate();

    // メニューから抜け出す時など
    SceneManager.pop();
}

他のWindowクラスとの比較について

Window_Commandクラスは「はい」「いいえ」などのシンプルな選択肢を実装するのに役立つ。

もし、より柔軟な一覧表とかアイテムリストのようなものを作る場合は、Window_Selectableから実装するのがよい。

また、横並びの選択肢を作りたい場合は、Window_HorzCommandを使うほうがよい。