KO

튜토리얼 6 — 점수와 옵션 저장하기

플레이어의 파일은 데스크톱에서는 게임의 var/ 폴더에, 웹에서는 브라우저 저장소(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/ 폴더는 게시되지 않아요.