# Functions not starting till scene restarts

**URL:** <https://phaser.discourse.group/t/functions-not-starting-till-scene-restarts/14100>\
**Category:** Phaser 3\
**Created:** [March 11, 2024, 3:37am UTC](https://phaser.discourse.group/t/functions-not-starting-till-scene-restarts/14100 "2024-03-11T03:37:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegancatter](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vegancatter](https://phaser.discourse.group/u/vegancatter)\
**Post date:** [March 11, 2024, 3:37am UTC](https://phaser.discourse.group/t/functions-not-starting-till-scene-restarts/14100/1 "2024-03-11T03:37:42Z")

</div>

the spawn pipe function ISNT ran every 3 seconds until the entire scene resets by the player dying.  
here is my code(Simplified):

```javascript
function create() {
    // PIPES //
    this.pipes = this.physics.add.group();
  
    this.physics.add.overlap(
      this.pipes,
      this.player,
      () => {
        this.scene.restart();
      }
    );
  
    var spawnPipeTimer = this.time.addEvent({
      delay: 3000,
      callback: this.spawnPipe,
      callbackScope: this,
      loop: true
    });
  
    this.spawnPipe = function () {
      var yOffset = ((Math.random() - 0.5) * 100) * 2
  
      var top_pipe = this.pipes.create(800, -50 + yOffset, 'top-pipe');
      var btm_pipe = this.pipes.create(800, 650 + yOffset, 'btm-pipe');

      this.pipes.add(top_pipe)
      this.pipes.add(btm_pipe)
    }
  
    this.firstPipeSpawned = false;
  }
  
  function update(t, dt) {
    if (this.firstPipeSpawned == false) {
      this.spawnPipe();
      this.firstPipeSpawned = true;
    }
    // Pipes //
    this.pipes.children.iterate(function(pipe) {
      pipe.setVelocityX(-120);
    });
  }

```

---

<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:** [March 11, 2024, 4:10am UTC](https://phaser.discourse.group/t/functions-not-starting-till-scene-restarts/14100/2 "2024-03-11T04:10:23Z")

</div>

It looks like `this.spawnPipe` is undefined when `addEvent()` is called.

You can change it to a regular function, outside of `create()`, and it will work fine because of `callbackScope`.

```js
function create() {
  // …
  var spawnPipeTimer = this.time.addEvent({
      delay: 3000,
      callback: spawnPipe,
      callbackScope: this,
      loop: true
  });
  // …
}

function spawnPipe() {
  // …
}

```
