チュートリアル6 — スコアとオプションの保存
プレイヤーのファイルは、デスクトップではゲームのvar/フォルダに、Webではブラウザのストレージ(IndexedDB)に保存されます。コードは同じです。
// ハイスコア
$best = $this->engine->storage->readJson('scores.json', ['best' => 0]);
if ($this->score > $best['best']) {
$this->engine->storage->writeJson('scores.json', ['best' => $this->score]);
}
// 設定(すぐに保存されます)
$difficulty = $this->engine->options->get('difficulty', 'normal');
$this->engine->options->set('difficulty', 'hard');
ページを離れるとき、またはウィンドウを閉じるときには、closeイベントで状態を保存できます。
$this->engine->lifecycle()->on('close', fn () => $this->engine->storage->writeJson('partie.json', $this->state()));
ビルド時にプレイヤーのファイルを読み込んではいけません。var/フォルダは公開されないからです。