# Phaser data-- arrays?

**URL:** <https://phaser.discourse.group/t/phaser-data-arrays/4643>\
**Category:** Phaser 3\
**Created:** [December 18, 2019, 4:01pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643 "2019-12-18T16:01:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 4:01pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/1 "2019-12-18T16:01:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![snowbillr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/snowbillr/32/332_2.png) [@snowbillr](https://phaser.discourse.group/u/snowbillr)\
**Post date:** [December 18, 2019, 4:44pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/2 "2019-12-18T16:44:26Z")

</div>

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:

```javascript
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.

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 5:39pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/3 "2019-12-18T17:39:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 5:43pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/4 "2019-12-18T17:43:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 18, 2019, 5:45pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/5 "2019-12-18T17:45:04Z")

</div>

You can use any value type in the Data Manager.

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

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

```

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 5:47pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/6 "2019-12-18T17:47:03Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 18, 2019, 5:48pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/7 "2019-12-18T17:48:31Z")

</div>

> [@casey](#):
>
> ```
> this.scene.start("B", this.DATA_PASS);
> 
> ```

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

It’s unrelated to the game registry ([this.registry](https://photonstorm.github.io/phaser3-docs/Phaser.Scene.html#registry)) or the scene’s data manager ([this.data](https://photonstorm.github.io/phaser3-docs/Phaser.Scene.html#data)).

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 5:52pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/8 "2019-12-18T17:52:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Villan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/villan/32/2920_2.png) [@Villan](https://phaser.discourse.group/u/Villan)\
**Post date:** [December 18, 2019, 6:02pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/9 "2019-12-18T18:02:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 6:02pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/10 "2019-12-18T18:02:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 18, 2019, 6:08pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/11 "2019-12-18T18:08:48Z")

</div>

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

```javascript
// 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

```

---

<div class="post-metadata">

**Author:** ![snowbillr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/snowbillr/32/332_2.png) [@snowbillr](https://phaser.discourse.group/u/snowbillr)\
**Post date:** [December 18, 2019, 6:18pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/12 "2019-12-18T18:18:24Z")

</div>

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

> <https://github.com/snowbillr/archer-adventure/blob/master/src/plugins/persistence-plugin.ts>

and one example of using it is [https://github.com/snowbillr/archer-adventure/blob/master/src/components/adventurer-component.ts#L35](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](https://github.com/snowbillr/archer-adventure/blob/master/src/scenes/hud-scene.ts#L27)

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [December 18, 2019, 6:21pm UTC](https://phaser.discourse.group/t/phaser-data-arrays/4643/13 "2019-12-18T18:21:21Z")

</div>

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