Update() launching before create()?

Continuing the discussion from Error on enableing physics:
When switching starting a same scene again, my update function lauches before my create functuion… I have no idea why it happends but apparently does.

I have my scene.start('scene'); in the update() of the 2 scenes i’m switching between, might it be the issue ?

How can you tell update() is being called before create()?

With console logs. On create, I call a function that adds a player and on update, I update the player. But one I switch back to one scene, it says that it can’t do setVelocityX of undefined on player.body.setVelocityX (player beeing a container). I did a if(player) in the update but doesent change anything.
BTW, ty for all your help samme

You can try updating Phaser to v3.24.1 if not already. There was a bug fix for something like that.

I was running old version, but still, doesent seems to work eather with new version…

And that’s definitely coming from scene update(), not an event?

I’m not sure, then. Can you post the scene code?

Client-side :

class Scene1 extends Phaser.Scene {

    constructor() {
        super('Scene1');
    }

    create() {
        var self = this;
        let currentMap = 0;

        socket.emit('changeMap', currentMap);

        socket.on('currentPlayers', function (players) {
            Object.keys(players).forEach(function (id) {
                if (players[id].playerId === socket.id) {
                    addPlayer(self, players[id]);
                } else if(players[id].currentMap === currentMap){
                    addOtherPlayers(self, players[id]);
                }
            });
        });

        socket.on('disconnect', function () {
            self.player.destroy();
        });
    }

    update() {
        if (this.player) {
            if (this.player.x > config.width+20) {
                this.player.x = 0;
                socket.emit('preQuit');
                socket.off('currentPlayers');
                socket.off('disconnect');
                this.scene.start('Scene2');
            }

            if (this.player.x < 0) {
                this.player.x = config.width;
            }
        }
        if (this.player) {
            player_gameplay(this);
        }
}

Server-side :

socket.on('preQuit', function () {
      socket.leave(player[socket.id].currentMap);
      io.sockets.in(player[socket.id].currentMap).emit('disconnect', socket.id);
    });

    socket.on('changeMap', function (data) {
      player[socket.id].currentMap = data;
      socket.join(data);
      socket.emit('currentPlayers', player);
    });

functions :

function addPlayer(self, playerInfo) {
    var playerSprite = self.add.sprite(0, 0, playerInfo.skin.sprite);
    self.player = self.add.container(playerInfo.x, playerInfo.y).setSize(playerSprite.width, playerSprite.height);
    var playerUsername = self.add.text(0,0,playerInfo.username,{
        fontFamily:'Arial',
        color:'#227805',
      }).setFontSize(18).setOrigin(0.5, 1.5);

    self.player.add(playerSprite);
    self.player.add(playerUsername);

    self.player.playerId = playerInfo.playerId;
    self.physics.world.enable(self.player);
    self.physics.add.collider(self.player, floor);
}
function player_gameplay (self) {
    if (cursors.left.isDown) {
        self.player.body.setVelocityX(vitesse * -1);
        self.player.list[0].flipX = true;
        self.player.walk = true;
    } else if (cursors.right.isDown) {
        self.player.body.setVelocityX(vitesse);
        self.player.list[0].flipX = false;
        self.player.walk = true;
    } else {
        self.player.body.setVelocityX(0);
        self.player.walk = false;
    }

    if (cursors.up.isDown && (self.player.body.blocked.down || self.player.body.touching.down || fly)) {
        self.player.body.setVelocityY(-350); // 1350
    }

    if (self.player.body.velocity.y > 0 && !(self.player.body.blocked.down || self.player.body.touching.down)) {
        self.player_down = true;
    } else if (self.player.body.velocity.y < 0 && !(self.player.body.blocked.down || self.player.body.touching.down)) {
        self.player_down = true;
    }

    if(self.player.y > config.height){
        self.player.y = 0;
    }
}

I tryed to simpplify this as much as possible, sorry if you get confused, I never comment :’)

The players are added after create(), though. Only the currentPlayers event handler is added during create().

1 Like

Oh sure sorry, I forgot to coment that. The currentPlayers listeners also adds the player

But the player won’t be created during create(). It only happens when the socket sends currentPlayers, which is after.

This is the real issue that I found while logging stuff:

this is the state of the scene at the end of Create() :


This is the function player_gameplay(scene) called in Update():

self. being the current scene*

The socket sends currenPlayers at the start of create() so I’m not sure if I can deal with it differently. But the thing is that it loads correctly the first time that I enter the game, so this is rlly wierd…

OK, apparenty I’m not the only one running this same issues, I’ve been throught other forums and apparently it also happends on scene.scene.restart()… I couldn’t find any answers tho

Can you make a test case without any server/socket code?

Hi @Santette,
If the problem is that the event is not received on time, then this something like this on the client side should work:

create(){
  this.isPlayerReady = false;
  // .........
  socket.on('currentPlayers', (players) => {
            Object.keys(players).forEach(function (id) {
                if (players[id].playerId === socket.id) {
                    addPlayer(self, players[id]);
                } else if(players[id].currentMap === currentMap){
                    addOtherPlayers(self, players[id]);
                }
            });
            this.isPlayerReady = true;
        });
  //......
}

update() {
  if(!this.isPlayerReady){
    return;
  }
  // .......
}

Regards.

1 Like

Thanx a lot !!!
This is a nicely thought fix x)