# Passing data (more than one) from scene to scene

**URL:** <https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512>\
**Category:** Phaser 3\
**Created:** [May 10, 2021, 5:15am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512 "2021-05-10T05:15:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cybr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/cybr/32/4323_2.png) [@cybr](https://phaser.discourse.group/u/cybr)\
**Post date:** [May 10, 2021, 5:15am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/1 "2021-05-10T05:15:48Z")

</div>

Hi there!

I’m currently working on an inventory system. I have a menu, and an inventory set up. I’m using the rexUI plugins.

codepen example [here](https://codepen.io/winstonitidev/pen/wvJagRW)

I’ve been passing through the neccesary data from one scene to another using the scene.launch function. I’ve run into a problems with how that data is transferred or read.

```
class InventoryScene extends Phaser.Scene {
constructor() {
super({key: "InventoryScene", active: true})
}
preload() {
this.load.scenePlugin({
        key: 'rexuiplugin',
        url: url,
        sceneKey: 'rexUI'
    });       
}

init(data) {
this.inventory = data
// console.log(this.inventory)
// let newArr = this.inventory(Math.sqrt)

this.scene.sleep()
}

create() { 

let scene = this, menu = undefined

this.menuKeys = this.input.keyboard.addKeys("W, S, A, D, P, Z");

let items = scene.inventory.items

// console.log(items)    
let w = config.width / 2 -38, h = config.height / 2 -77

this.menu = createList(scene, w, h, items, function(button) {
  let activePointer = scene.input.activePointer
  
  let inventoryButton = button
  let data = {activePointer, inventoryButton}
  // heres my trouble, I'm unsure how to seperate these and use the data properly
//

  scene.scene.launch("UseMenuScene", data)
  scene.scene.pause()
})
}  

```

MainScene \> InGameMenuScene \> InventoryScene \> UseMenuScene

I have scenes that are seperated, as seen in my codepen example.  
Aside from the main scene, theres the InGameMenu scene that handles the first menu that you see.  
The one I’m having trouble with is the UseMenuScene, and how to receive the data that was passed through the Inventory scene.

I want to recieve the the pointer data and button data (selected item)from the previous scene

```
class UseMenuScene extends Phaser.Scene {
  constructor() {
    super({key: "UseMenuScene", active: true})
  }
  preload() {
    this.load.scenePlugin({
      key: 'rexuiplugin',
      url: url,
      sceneKey: 'rexUI'
    });
  }
  
  init(data) {
    this.pointer = data
  }
  
  create() {
    this.menuKeys = this.input.keyboard.addKeys("P");
    let scene = this
    
    let menu = createUseMenu(scene, this.pointer.x, this.pointer.y, [{name: "Use"}], function() {
      menu.destroy()
      scene.scene.wake('InventoryScene')
      scene.scene.sleep()
    })
    
    this.scene.pause("InventoryScene")
  }
}

```

I had attempted to send both button data and pointer data, created in the inventory scene, so that the x and y of the inventory list at the mouse position was read and the UseMenu is created.  
I’ve been using the init function in the scenes, so I can read the data.

I want to send two pieces of data, from a parent scene, to its child scene. I would assume you can use destructuring to send to pieces of data, but I also want to be able to read it correctly, because I feel I have failed to grasp how properly passing information from scene to scene works.

If anyone could give me a rundown on the dos and donts of dealing with data, or any criticism, I will take it on board to the best of my ability.

I am currently using codepen to complete the inventory for future reference, in case anyone wants to look at the code.

If anyone could help me out, I’d appreciate it. 🙂

edit: So i attempted again to send two pieces of data, and while its working, I’m getting an uncaught type error which the reason why I’m not sure.

`Uncaught TypeError: Cannot read property 'x' of undefined at UseMenuScene.create`

```
  create() {
let scene = this
console.log(scene.data.inventoryButton)
console.log(scene.data.activePointer)

let menu = createUseMenu(scene, scene.data.activePointer.x, scene.data.activePointer.y, [{name: "Use"}, {name: "Equip"}], function() {
  menu.destroy()
  scene.scene.wake('InventoryScene')
  scene.scene.sleep()
})

this.scene.pause("InventoryScene")
}

```

The console says I’m recieving both objects but I cant use them without getting the uncaught type error.

Thanks

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 10, 2021, 5:29am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/2 "2021-05-10T05:29:46Z")

</div>

> **[Working with objects - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects?retiredLocale=nl)**
>
> JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a...

```javascript
var myCar = {
    make: 'Ford',
    model: 'Mustang',
    year: 1969
};

```

---

<div class="post-metadata">

**Author:** ![cybr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/cybr/32/4323_2.png) [@cybr](https://phaser.discourse.group/u/cybr)\
**Post date:** [May 10, 2021, 7:15am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/3 "2021-05-10T07:15:28Z")

</div>

> [@Milton](#):
>
> `var`

I know I need to brush up on objects and do some reading. I’m hoping someone can tell me because a quick read through working with objects, hasnt really given me an answer. I atleast know that I can pass pointer information through from scene to scene. I’m dumbfounded as how to pass an object of objects through without getting an error. Thanks though.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 10, 2021, 7:19am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/4 "2021-05-10T07:19:58Z")

</div>

```javascript
let data = { a:activePointer, i:inventoryButton, o: { str:"another object" } }

```

---

<div class="post-metadata">

**Author:** ![cybr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/cybr/32/4323_2.png) [@cybr](https://phaser.discourse.group/u/cybr)\
**Post date:** [May 10, 2021, 7:21am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/5 "2021-05-10T07:21:52Z")

</div>

Ohh. Thanks a lot. You’ve saved me a lot of time. I’ll make sure I’ll read about objects more often. Thanks

---

<div class="post-metadata">

**Author:** ![cybr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/cybr/32/4323_2.png) [@cybr](https://phaser.discourse.group/u/cybr)\
**Post date:** [May 14, 2021, 7:27am UTC](https://phaser.discourse.group/t/passing-data-more-than-one-from-scene-to-scene/9512/6 "2021-05-14T07:27:49Z")

</div>

Just posting here in case anyone wants to check the code for the inventory. Just finished working on the parts I was struggling with here. [Here](https://codepen.io/winstonitidev/pen/PopqgqN?editors=0011) it is in codepen.Thanks again!
