# Running a function only once inside an observable

**URL:** <https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281>\
**Category:** Questions\
**Created:** [July 10, 2023, 5:10am UTC](https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281 "2023-07-10T05:10:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![freetoplay](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@freetoplay](https://forum.babylonjs.com/u/freetoplay)\
**Post date:** [July 10, 2023, 5:10am UTC](https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281/1 "2023-07-10T05:10:10Z")

</div>

I am building an app that allows the user to edit their mesh, then take a screenshot of it. I have a situation where my save logic does not have direct access to the scene, so I am trying to use an observable for the file with the button to communicate with the scene and capture the screenshot. My issue is the callback inside of `onSaveObservable.addOnce` gets called multiple times if I make updates to the mesh in my scene. Is there a way to have the callback only be called once after clicking the Save button? Is there a better approach to my problem?

I have a custom observable that’s declared like this:

```auto
const onSaveObservable = new Observable();

```

Inside of fileA (does not have direct access to scene canvas):

```auto
<button @click="handleSave">Save</button>

const handleSave = () => {
  onSaveObservable. notifyObservers(null);

```

}

In my Babylon scene code fileB, I have expected it to only take one screenshot:

```auto
onSaveObservable.addOnce(() => {
Tools.CreateScreenshot(engine, scene.activeCamera as Camera, 150);
})

```

---

<div class="post-metadata">

**Author:** ![11128](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@11128](https://forum.babylonjs.com/u/11128)\
**Post date:** [July 10, 2023, 6:04am UTC](https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281/2 "2023-07-10T06:04:21Z")

</div>

Difficult to understand and reproduce accurately.  
so I’ll try to solve it with another proposal.

1. Create a simple export function like this  
Can you import and use this in the corresponding button event?  

2. Make sure you do the observable constructor multiple times.

3. You’ll also want to make sure that the execution is also nested multiple times.

4. Can you create an example for states in PG?  
[https://playground.babylonjs.com/#U3WMS5#1](https://playground.babylonjs.com/#U3WMS5#1)

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [July 10, 2023, 11:01am UTC](https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281/3 "2023-07-10T11:01:35Z")

</div>

> [@freetoplay](#):
>
> `notifyObservers`

Hum! Not a specialist here (not at all 😉) but the API description lets me think that it would be a normal behavior to have this trigger multiple times. May be you could try “[notifyObserversWithPromise](https://doc.babylonjs.com/typedoc/classes/BABYLON.Observable#notifyObserversWithPromise)”  
Else, another approach I believe would be to just add a pointerUp or trigger a variable on the button click that sets i.e. ‘screenshottaken = true’ or false if the observer/promise returns an error?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 10, 2023, 9:23pm UTC](https://forum.babylonjs.com/t/running-a-function-only-once-inside-an-observable/42281/4 "2023-07-10T21:23:23Z")

</div>

This should totally work: [Babylon.js Playground](https://playground.babylonjs.com/#FJ4QJC)

The code with addOnce might be called several times in your code. you could add a log to be sure it is not ?
