Forum Discussion

Elem08's avatar
Elem08
Member
9 months ago
Solved

Can't get or set VariableGroup variables

Hello!

I am extremely new to development (using the Desktop Editor), and I wanted to try a quick world that just enables a button that, when pressed, updates a player's score using NetworkBroadcastEvents and Variable Groups. Here's what I've got:

A VariableGroup called `PlayerAttributes` with `test` and `localScore` 

Which I then try to reference in a Typescript file:

 

 

 

// names of the variable group and persistent variable containing player data
const varGroupName: string = "PlayerAttributes";
const varName: string = "localScore"; // Set as a Number
const varKey: string = `${varGroupName}:${varName}`;

export type ButtonPressedType = {
  player: Player
}

class ScoreManager extends hz.Component<typeof ScoreManager> {
  static propsDefinition = {};

  start() {
    this.connectNetworkBroadcastEvent(NetworkEvents.buttonPressed, (data: ButtonPressedType) => {
      console.log('Button pressed by player', data.player.id);    
      const curScore = Number(this.world.persistentStorage.getPlayerVariable(data.player, 'PlayerAttributes:localScore'));
      console.log('Current score:', curScore)
      this.world.persistentStorage.setPlayerVariable(data.player, 'PlayerAttributes:localScore', curScore + 1)
      console.log('Player', data.player.id, 'score:', curScore + 1)
    })
  }
}
hz.Component.register(ScoreManager);

 

 

 

 

But every time I send the network event, I get the following:

Can't set value for PlayerAttributes:localScore. PlayerAttributes:localScore was deleted or never existed. Go to Systems tab in creation UI to confirm.

What am I doing wrong?

4 Replies

  • Confirm after you've created the variable group and variables, that they have been added to the world under the 3-dot menu.

     

    • Elem08's avatar
      Elem08
      Member

      Yes! I can confirm they were added to my world.

      Interestingly enough, I left to go make dinner for the family, and when I got back and re-opened the project / world and tested it again, the code and everything started working.

      Is it documented somewhere that the Editor / World has to be closed and opened again to have access to Variable Groups? It appears that may have been the solution but it's hard for me to tell ๐Ÿ™‚ 

      • PigeonNo12's avatar
        PigeonNo12
        Partner

        It's a known bug. Hopefully it gets resolved soon