1

My game will handle a data model like this:

- Characters
  - Name
  - PowerBase
- League
  - Title
  - BossName
- Match
  - Player
  - Character
  - League
  - Progress
  - PowerTotal
  - Money
    - Loan1, Loan2
    - Funding1, Funding2

As you can see, it's a nested data.

So, how should I handle this data between registry update events, local storage and game scenes?

Option 1) Keep everything in a single class with nested properties. It's a problem because registry events will not fire for specific properties.

Option 2) Keep a single variable for each data, with is hard to handle since they'll not be nested in classes.

Option 3) Any other idea?

anderlaini
  • 1,593
  • 2
  • 24
  • 39

1 Answers1

1

It depends on your preference and usage. I personally like to use a global variable, that holds the entire gamestate. Using this method you have access from every scene without having to pass it.

And more over I don't use a class, I would simply use a json object, since they are easy to store if needed (I try to keep the datastore as dumb as possible). But if you need more logic and actions, this might not be the best solution.

// in the global scope 
const GAME_STATE_KEY = 'MY_GAME_STATE';
var GameState = {
    // ...
};

// functions to save and load the State (if you want to persist it)

function loadStateData(){
    GameState = JSON.parse(localStorage.getItem(GAME_STATE_KEY));
}

function saveStateData(){
    localStorage.setItem(GAME_STATE_KEY, JSON.stringify(GameState));
}
winner_joiner
  • 12,173
  • 4
  • 36
  • 61