Group objects data undefined

I am adding extended sprites to a group. In the constructor I am setting various variables, hp, gold, xp etc.

This does not work

this.setData('name', 'rat');

By doesn’t work I mean the sprites data is undefined.

This works:

this.name = 'rat';

I read this: https://phaser.discourse.group/t/setvelocity-vx-vy-not-working-as-excepted/8258

It basically says when you add things to a group the physics get overwritten and set to the group settings.

this.spawns.defaults = {};

Worked for physics variables.

I assume the data object is getting overwritten similarly? Is there a way around this or another better way to achieve this?

Thank you.

By chance, can you share some example code that recreates the issue?

Phaser never calls setData() itself so there shouldn’t be any conflicts. It’s possible the sprite has been destroyed or there’s been a this mixup. Need to see the code.

I believe this is all the relevant code, if there’s more please let me know:

spawnRandomEnemies() {
  this.spawns = this.physics.add.group();
  this.spawns.defaults = {}; // https://phaser.discourse.group/t/setvelocity-vx-vy-not-working-as-excepted/8258/8
  this.spawns.name = 'random spawns';
  const monsters = [Bat,
    Rat];
  for (var i = 0; i < 30; i++) {
    var x = Phaser.Math.RND.between(0, this.physics.world.bounds.width);
    var y = Phaser.Math.RND.between(0, this.physics.world.bounds.height);
    let newMonster = Phaser.Math.RND.pick(monsters);
    this.spawns.add(
      new newMonster(this, x, y, null, 0, i)
    );
  }
  this.physics.add.collider(this.player, this.spawns, this.onMeetEnemy, this.enterBattleScene, this);
  return this.spawns;
}


export default class Actor extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x, y, texture, frame, name) {
    super(scene, x, y, texture, frame);
    this.name = name;
    this.scene.physics.add.existing(this);
    this.scene.add.existing(this);
    this.setCollideWorldBounds(true, 0, 0, true);
    this.collideExclusionLayers = true;
    this.body.collideExclusionLayers = true;
    this.setVelocity(0, 0);
    this.trackingPlayer = false;
  }

  movementTimer(isCollision = false) {
    if (isCollision) {
      //this.setTint(0x000000);
    }
    if (!this.trackingPlayer) {
      this.moveActor();
      this.timedEvent = this.scene.time.addEvent(
        {
          delay: this.tdelayTime,
          // how often moveActor is called
          callback: this.automateActor,
          callbackScope: this,
          loop: true,
          startAt: this.tstartAt
        }
      );
    }
  }

  import { rollEnemyHP } from '../js/dice.js';

  export default class Rat extends Actor {
    constructor(scene, x, y, texture, frame) {
      super(scene, x, y, 'ratEnemy', frame);
      this.body.setSize(10, 10);
      this.scene = scene;
      this.setScale(2);
      this.trackingPlayer = false;
      //this.name = 'rat';
      this.setData('name', 'rat');

      // movement timer stuff
      this.maxMoveTime = 10001; // maximum time to move
      this.maxIdleTime = 1000; // maximum time to stand still before changing direction
      this.tmoveTime = Phaser.Math.Between(this.maxMoveTime*.25, this.maxMoveTime);
      this.tidleTime = Phaser.Math.Between(this.maxIdleTime*.25, this.maxIdleTime);
      this.tdelayTime = this.tmoveTime + this.tidleTime;
      this.tstartAt = Phaser.Math.Between(this.tdelayTime - 10, this.tdelayTime - 20);
      this.movementTimer();
    }
  }

What exactly happens and where?

I’m noticing it when a collision happens.

onMeetEnemy(player, monster) {        
  this.cameras.main.shake(300);
  this.cameras.main.on('camerashakecomplete', () => {
    this.scene.start('Battle', {player: this.player, monster: monster});
  });
}

On collision I pass the enemy object to a battle scene and the enemies data is undefined when logging it out to console.
Nothing happens just data for a enemy is undefined.

But if I set the rat/enemy data like:
this.name = ‘rat’;
The name is set to rat. I was hoping to store the data in rat.data as it looks better and is easier to read.

It’s likely the sprite has been destroyed by then and its data store was removed.

You can pass monster.data.values instead of the sprite itself. Or if you don’t want to stop the original scene, use launch() instead of start().

I logged the enemies out just after creation and the do indeed have the data variable set. Thank you.