# Unity Stop/Start the simulation

**URL:** https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132
**Category:** Pulse Physiology Engine
**Created:** [September 21, 2023, 9:01am UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132 "2023-09-21T09:01:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Glenn](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@Glenn](https://discourse.kitware.com/u/Glenn)
#### Post date: [September 21, 2023, 9:01am UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/1 "2023-09-21T09:01:28Z")

</div>

Hi kitware,

Is there a way to set the state of the pulse engine driver. ie start stop restart? also there is an issue when you reload the unity scene related to this the dll seems to still be running in the background and causes graphical issue with the vitals monitoring with the line render going out of the area till it catches up.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [September 21, 2023, 3:03pm UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/2 "2023-09-21T15:03:49Z")

</div>

Hey Glenn,

You can modify the PulseEngineDriver in our Unity asset.

There is a mechanism for [pausing the advancement of the engine](https://gitlab.kitware.com/physiology/unity/-/blob/main/Scripts/PulseEngineDriver.cs?ref_type=heads#L97)  
At any time, you can call any of the Serialize method on the engine (SerializeFromFile, SerializeFromString, SerializeToFile, SerializeToString) to create states and set the engine to those a state, we only do it in Awake to initialize the engine, but you could call it in Update if you wanted.

Can you give me a list of steps to reproduce the graphical issue using the provided VitalsMonitor scene?

---

<div class="post-metadata">

### Author: ![Glenn](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@Glenn](https://discourse.kitware.com/u/Glenn)
#### Post date: [September 21, 2023, 4:21pm UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/3 "2023-09-21T16:21:04Z")

</div>

hey I am running the pulse action on click scene in unity 2021.3.26f1 run the scene for a few seconds then call SceneManager.LoadScene(); with the current scene the bars run out to the left hand side until the sim time catches up.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [September 21, 2023, 4:29pm UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/4 "2023-09-21T16:29:55Z")

</div>

Ok

How do you `call SceneManager.LoadScene();` from the editor?

---

<div class="post-metadata">

### Author: ![Glenn](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@Glenn](https://discourse.kitware.com/u/Glenn)
#### Post date: [September 21, 2023, 4:32pm UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/5 "2023-09-21T16:32:11Z")

</div>

I added a UI button to call it but you can do it on a key press as well if that’s easier for you. inside the brackets you can put the build index number for your scene or the scene name.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [September 21, 2023, 5:43pm UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/6 "2023-09-21T17:43:39Z")

</div>

I have not tried to reset the monitor before  
I would expect we would need to write a script that knows about the monitor and all its components and would clear out the LineRenderer of its data on a scene reset.

I think the default LoadScene is not doing that in our scripts, maybe we could just add clear calls in our `PulseDataLineRenderer::Awake`

---

<div class="post-metadata">

### Author: ![Glenn](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@Glenn](https://discourse.kitware.com/u/Glenn)
#### Post date: [September 22, 2023, 10:56am UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/7 "2023-09-22T10:56:09Z")

</div>

Hi abray, if you add this to the unity pulse engine driver it handles the large time jump that causes the Issue. not the best fix but works.

```auto
// Iterate over multiple time steps if needed
int numberOfDataPointsNeeded = (int)Math.Floor(timeElapsed / pulseTimeStep);
int MinDataPoints = 80;
//if there are too many steps skip the drawing of the data bellow and have the sim catch up
if (numberOfDataPointsNeeded > MinDataPoints)
{
      for (int i = 0; i < numberOfDataPointsNeeded; ++i)
      {
        pulseTime += pulseTimeStep;
        pulseSampleTime += pulseTimeStep;
        engine.AdvanceTime_s(pulseTimeStep);
        data_values = engine.PullData();
      }

      return;
}

```

---

<div class="post-metadata">

### Author: ![Glenn](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@Glenn](https://discourse.kitware.com/u/Glenn)
#### Post date: [September 22, 2023, 11:03am UTC](https://discourse.kitware.com/t/unity-stop-start-the-simulation/1132/8 "2023-09-22T11:03:18Z")

</div>

here is a code snippet for reloading a scene in unity add this to any update method. Should help with testing.

```auto
if (Input.GetKeyDown(KeyCode.R))
{
      SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

```
