I’ve been working on it using Paser 3 for a few days, but it’s hard to manage the ‘Paser.Game’ instance.
Now I’m using React state. I put new Paser.Game(config) in the state and renamed the player using ‘useEffect’, which detects when the state changes.
As a result, there was a problem trying to access the game scene before it was fully loaded.
I make setPlayerNameUntilLoaded function as a temporary measure, but I know this is the wrong approach. I’m worried that I should check that GameObject is loaded whenever I want to update it.
I have two questions.
- How do I know if the Game Scene is fully loaded?
- How do I manage the Paser.Game instance in React?
Below is the code for the part I’m curious about.
import { config } from "@/phaserGame.js";
import React, {useEffect, useState} from "react";
import Phaser from "phaser";
import { useRecoilState, useRecoilValue } from "recoil";
import {characterNameAtom, phaserGameAtom} from "@/recoil/atom.js";
import { Chat } from "@/components/Chat/Chat.js";
import {Player} from "@/characters/player";
export function Town() {
const [game, setGame] = useState(null);
const [recoilGame, setRecoilGame] = useRecoilState(phaserGameAtom);
const characterName = useRecoilValue(characterNameAtom);
useEffect(()=>{
setGame(new Phaser.Game(config));
//setRecoilGame(new Phaser.Game(config));
},[]);
useEffect(()=>{
if(game !== null) {
//setRecoilGame(game);
setPlayerNameUntilLoaded();
//gameScene.add.text(0, 0, characterName, {fontSize: 100, fontFamily: 'Line Seed Sans Bd, sans serif'});
}
},[game]) ;
function setPlayerNameUntilLoaded(){
const gameScene = game.scene.keys.game.scene.scene ;
if(gameScene.myPlayer === null){
setTimeout(() => {
setPlayerNameUntilLoaded();
},300);
return ;
}
gameScene.myPlayer.setPlayerName(characterName) ;
}
return (
<>
<Chat/>
</>
)
}