Phaser data-- arrays?

After banging my head over here for a bit I think I finally understand P3 data get/set registry, but it seems only for a string.
Is there a way to set an array of data in a way that makes updating/editing the array easy? I can’t seem to figure out how to push/pull/ etc.

I think the registry should work fine for other data types. Can you give an example of it breaking?

EDIT:
Ah - it does function a little differently than I’d expect:

set: function (key, data)
    {
        if (this._frozen)
        {
            return this;
        }

        if (typeof key === 'string')
        {
            return this.setValue(key, data);
        }
        else
        {
            for (var entry in key)
            {
                this.setValue(entry, key[entry]);
            }
        }

        return this;
    },

So you can’t do arrays or objects, fair enough. I suppose you could get around it by doing a JSON.stringify() before you set the value, and then JSON.parse() when you get it out.

1 Like

Thanks: It’s just SO much more complicated than using global variables–I’ve got a massive list of an inventory, so this would just be nuts to do it this way. I’m really not sure why the structure was built this way.
I think I can do it by passing an object to another scene, but I can’t get it to work.
I have my initial scene where I store the info into an object, which means I can use an array. I’m trying different data types in my object but I can’t get it to retrieve in the next scene.

Scene A:

 init(){
        this.DATA_PASS = {
            MyNum: 5,
            Arr: [1, 3, 5],
            string: "string"
        };
    }
create(){
this.scene.start("B", this.DATA_PASS);
}

Scene B

init(stuff){
 this.stuff = this.DATA_PASS;
}

create(){
console.log(this.stuff);
console.log(this.DATA_PASS);
console.log(stuff);

}

None of this works–How do I retrieve the data in SceneB?

Huh. Actually, I got it to work. Scene B

init(info){
this.DATA_PASS= info;
}

create(){
console.log(this.DATA_PASS);

}

will give me the entire object, so from there I can access the items in the object, including the array.

You can use any value type in the Data Manager.

this.registry.get('arrayThing').push(1);

this.registry.values.arrayThing.push(1);

Thank you @samme Is there an advantage to doing it that way over passing the whole object which contains multiple different items?
Otherwise I have a bunch of arrays that I have to set/get every single time I change scene (which is a LOT).

That’s the data argument, used for passing optional values between scene operations.

It’s unrelated to the game registry (this.registry) or the scene’s data manager (this.data).

I’m sorry, I don’t even understand that.
I had a project in Phaser 2.0 that had one page of global variables that I could call from various points in the game.
I’m trying to do that in P3, but I’m thoroughly confused by the way to use any form of data across scenes.
if it is a simple score, I can do that with the get/set registry, but I have like 50 global variables, including 5 long arrays. How do I deal with this in P3?

I have a variable.js file and am able to call anything from it as long as it’s loaded before my scenes.

Can you share the structure of that? I tried that approach without success.

No worries. For global data, you just use get/set in the game registry when you need to.

// Initial data
this.registry.merge(DATA_PASS);

this.registry.get('MyNum'); // → 5
this.registry.get('Arr'); // → [1, 3, 5]
this.registry.get('string'); // → 'string'

this.registry.get('Arr')[0]; // → 1

FWIW I built out a cross-scene persistence plugin which has been useful for me. It works as a game level plugin.

and one example of using it is https://github.com/snowbillr/archer-adventure/blob/master/src/components/adventurer-component.ts#L35 and https://github.com/snowbillr/archer-adventure/blob/master/src/scenes/hud-scene.ts#L27

1 Like

OK, sorry for being thick, but thank you both. I think I get it now. I will play with it for a while!