# Using common methods across scenes but still main "this.scene"

**URL:** <https://phaser.discourse.group/t/using-common-methods-across-scenes-but-still-main-this-scene/12399>\
**Category:** Phaser 3\
**Created:** [October 7, 2022, 6:23pm UTC](https://phaser.discourse.group/t/using-common-methods-across-scenes-but-still-main-this-scene/12399 "2022-10-07T18:23:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 7, 2022, 6:23pm UTC](https://phaser.discourse.group/t/using-common-methods-across-scenes-but-still-main-this-scene/12399/1 "2022-10-07T18:23:28Z")

</div>

I have this repeated method to call an event to update keys and hearts on top of the screen …

Every scenes have this **updateInventory()** method repeated in gameScene, room1, room2, room3 when either keys or hearts changes…

Q: How do I have a common method shared across scenes but stil able to use this.scene.get

I tried a global function updateInventory() { … } but the this.scene.get is not working anymore…

Code here :

> <https://github.com/stanleyseow/ToRescueThePrincess/blob/main/gameScene.js>

Common method that calls showInventory scene…

```javascript
    updateInventory() {
      // Emit events showInventory
      this.inventory = {}
      this.inventory.heart = window.heart
      this.inventory.key = window.key

      console.log('Emit event', this.inventory)
      this.invEvent = (event, data) => this.scene.get('showInventory').events.emit(event, data);
      this.invEvent("inventory", this.inventory);
  }

```

---

<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:** [October 8, 2022, 1:15am UTC](https://phaser.discourse.group/t/using-common-methods-across-scenes-but-still-main-this-scene/12399/2 "2022-10-08T01:15:33Z")

</div>

Using `call()`:

```javascript
function updateInventory() {
  this.scene.get(); // etc.
}

class gameScene extends Phaser.Scene {
  // …
  create() {
    updateInventory.call(this);
  }
}

```

With `addEvent()` you pass `callbackScope` instead:

```javascript
function updateInventory() {
  this.scene.get(); // etc.
}

class gameScene extends Phaser.Scene {
  // …
  create() {
    this.time.addEvent({
      // …
      callback: updateInventory,
      callbackScope: this,
    });
  }
}

```

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 8, 2022, 2:46am UTC](https://phaser.discourse.group/t/using-common-methods-across-scenes-but-still-main-this-scene/12399/3 "2022-10-08T02:46:30Z")

</div>

Thanks a lot, really appreciate it…

I have been repeating the same function over and over again in all the scenes…

Are they any drawbackls of using global functions ???
