RPG Maker MZ
RPG Maker MZ・JavaScript+イベントコマンド
まず、一文を画面に表示してみましょう。この小さなサンプルは「港の鍵」の実際のエクスポートを直接読み込みます。会話をすべて実行する仕組みではありません。表示できたら、データをゲームのUIにつなげていきます。
1 ・ファイルやシーンの準備
対象はRPG Maker MZです。story.jsonをdata/へコピーします。Action Buttonで起動するマップイベントを作り、読み込みスクリプト、以下の待機ループ、表示スクリプトの順に追加します。ゲーム変数1は文章、2は画像対応用に確保します。続けて\V[1]を含むShow Textを置きます。プラグインは不要です。
2 · 最初のテキストを表示
// Event command: Script. The next command waits until loading finishes.
$gameTemp.vnleExample = { ready: false, story: null, error: null };
const request = new XMLHttpRequest();
request.open('GET', 'data/story.json');
request.overrideMimeType('application/json');
request.timeout = 15000;
request.onload = () => {
try {
if (request.status >= 400) throw new Error('HTTP ' + request.status);
$gameTemp.vnleExample.story = JSON.parse(request.responseText);
} catch (error) { $gameTemp.vnleExample.error = String(error); }
$gameTemp.vnleExample.ready = true;
};
request.onerror = request.ontimeout = () => {
$gameTemp.vnleExample.error = 'Cannot load data/story.json';
$gameTemp.vnleExample.ready = true;
};
request.send();Loop → Conditional Branch(Script: $gameTemp.vnleExample.ready)→ Break Loop → End → Wait: 1 frame → Repeat Aboveの順に追加します。表示スクリプトはループの後に置きます。これで読み込み中もエンジンが応答できます。
// Event command: Script, AFTER the loading loop below has finished.
const result = $gameTemp.vnleExample;
if (result.error) {
$gameVariables.setValue(1, result.error);
} else {
const locale = 'en';
const id = 'fd7b61a3-8233-4ba7-8f77-b88b119a91bf';
const content = result.story.content;
const translation = (content.translations[locale] || {})[id];
const message = translation ? translation.message : content.texts[id].sourceMessage;
const line = message.segments.map(segment => {
if (segment.kind !== 'text') throw new Error('Text-only example');
return segment.text;
}).join('');
$gameVariables.setValue(1, line);
}
// Next event command: Show Text, containing \V[1]. Reserve variable 1 for this sample.「Welcome to the harbour. I am Mira.」と表示されれば成功です。locale = "de"、または言語パラメーターをdeにすると「Willkommen im Hafen. Ich bin Mira.」になります。言語が見つからない場合は原文を使います。このサンプルはテキストセグメントに対応しており、プレースホルダーを黙って捨てることはありません。
3 · 一文の表示から会話へ
上の固定テキストIDは、最初の動作確認用です。実際のゲームでは、story-index.jsonからflowRef/entryRefを選び、execution.entries[entryRef].nodeRef、続いてexecution.nodes[nodeRef]を読みます。現在の会話ノードからtext.textRefとspeakerRefを取得します。次へ進む方法はノードのkindによって異なります。
Show Textは変数1を表示して確認を待ちます。Show Choicesは回答と対応するイベント分岐を提示します。完全なエクスポートから動的に回答を作る場合は、選択肢IDをゲームの選択処理につなぎます。Endでは会話イベントを終え、会話用画像を消します。
- ContinueとEnd:接続をたどり、会話ボックスを閉じる
- 選択肢:回答のID、表示順、選択後の進行先
- 条件と変数:10ゴールドと2ゴールドで試す
- 言語:同じIDから別の言語の内容を読み出す
- コマンド: アイテムを与えるか、ショップを開く
- Call/Return:サブストーリーを呼び出して戻る
この言語で回答と所持金の判定を確認する
選択肢のデータを読む
読み込み後にstoryを使える場所へ、このコード片を挿入します。RPG Makerではconst story = $gameTemp.vnleExample.story;で取得します。確認用にIDを出力するので、その出力処理を回答ボタンに置き換えてください。対象は港の2つの回答と、その後の所持金判定です。あらゆるノードを実行できる汎用処理ではありません。
// story is already loaded. Keep each optionId on its answer button.
const choice = story.execution.nodes['42edfff8-b8fe-46bf-ae0d-9e2347b6e726'];
for (const optionId of choice.optionOrder) {
const option = choice.options[optionId];
console.log(optionId, option.text.textRef); // Resolve text, then create your button.
}
// Inspect the actual purchase check. This particular node is Gold >= 5.
const branch = story.execution.nodes['c1c3c372-0a73-4cd0-851d-ede59d5709bb'];
const gold = 10; // Repeat with 2, using your current game value.
const next = gold >= branch.condition.right.value.value ? branch.whenTrue : branch.whenFalse;
console.log(next.nodeRef);所持金が10ならnextは「Pay five gold」、2なら「Not enough gold」を指します。実際のゲームでは、選択されたルートがこのノードに到達したときだけ判定してください。各回答には、それぞれの進行先があります。
4 · 自分の画像を対応付ける
この補足コードでは、現在の会話オブジェクトをnodeとし、コメントに記載したUI要素が存在することを前提とします。C++とMonoGameでは、speakerRefはすでにSpeakerIdまたはspeakerIdとして読み込まれています。キーはミラの実際のキャラクターIDです。画像の名前と配置先はゲーム側で決めます。指定箇所に各行を挿入してください。
// node is your current VNLE dialogue. Reserve variable 2 for this mapping.
$gameVariables.setValue(2, node.speakerRef === '807b2957-7bab-4a28-83a4-1f3d1589bbc2' ? 1 : 0);
// Event commands: if Variable 2 == 1, Show Picture 20: mira from img/pictures.
// Otherwise: Erase Picture 20. Erase it again when the conversation ends.話者名は、会話文と同様にcontent.characters[speakerRef].nameTextRefから取得します。画像の対応がない場合は、前のポートレートを隠すか代替画像を表示します。画像は言語によって切り替わりません。
5 · ゲームに必要なファイルを同梱する
配布後にdata/story.jsonがあるか確認します。画像はエディターから読み込み、未使用ファイルを除外する設定にも注意してください。MZのAPIがMV、VX Aceなどでもそのまま使えるとは限りません。
結果を確認する
- 英語とドイツ語の挨拶が正しく表示され、対応していない言語では英語に戻ることを確認します。
- 進行処理を追加したら、両方の回答、所持金10と2、サブストーリーからの復帰、Endを試してください。
- ビルドしたゲームをプロジェクトフォルダーの外で実行します。JSON、フォント、ポートレートがそこでも読み込めることを確認してください。
公式ドキュメント
この実装で使用するAPIと設定の資料: