I have a problem when switching back and forth between two scenes.
I have a lobby and a game scene. In the game scene i use the code you provide to create the engine.
Each time i switch to the lobby scene and then back to the game scene the scene loading takes a bit longer and the monitor extends to left outside the bounds of the screen.
To me it looks like the engine is not getting destroyed when switching scenes. Could this be the case?
Assuming the switching context is all happening in the editor, and you are creating an engine in your Start method, you may need to add this code so you only instantiate a Pulse engine when your application is playing when context switching
void Start()
{
// Ensure we only read data if the application is playing
// and we have a state file to initialize the engine with
if (!Application.isPlaying || initialStateFile == null)
return;
// Now create a Pulse engine
engine = new PulseEngine(logFilePath, pulseDataPath);
You could also put a break point on the engine = new PulseEngine(logFilePath, pulseDataPath); call when you context switch in the editor to see if you are indeed creating an engine on a scene switch
Thanks for the reply. Maybe I did not express myself correctly. It does not occur when switching scenes in Editor by clicking a scene file. I occurs when i switch between scenes during runtime by using SceneManager.LoadScene.
I do create a new engine in the Start method. I expected the engine to get destroyed when loading another scene, but it seems like that is not the case.
I use the provided code each time i load the scene:
protected virtual void Start()
{
// Ensure we only read data if the application is playing
// and we have a state file to initialize the engine with
if (!Application.isPlaying)
return;
// Allocate PulseEngine with path to logs and needed data files
string dateAndTimeVar = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string logFilePath = Application.persistentDataPath + "/" +
gameObject.name +
dateAndTimeVar + ".log";
engine = new PulseEngine();
//engine.SetLogFilename(logFilePath);
SEDataRequestManager data_mgr = new SEDataRequestManager(data_requests);
// NOTE, there are other ways to initialize the engine, see here
// https://gitlab.kitware.com/physiology/engine/-/blob/3.x/src/csharp/howto/HowTo_EngineUse.cs
// Initialize engine state from tje state file content
if (initialStateFile != null)
{
if (!engine.SerializeFromString(initialStateFile.text, data_mgr, serializationFormat))
Debug.unityLogger.LogError("PulsePhysiologyEngine", "Unable to load state file " + initialStateFile);
}
else
{
// You do not have to use the Editor control if you don't want to,
// You could simply specify a file on disk via use of the Streaming Assets folder
string state = Application.streamingAssetsPath + "/Data/states/StandardMale@0s.pbb";
if (!engine.SerializeFromFile(state, data_mgr))
Debug.unityLogger.LogError("PulsePhysiologyEngine", "Unable to load state file " + state);
}
pulseTime = 0;
pulseSampleTime = 0;
}
Or maybe the lineRenderer is consuming and displaying data from multiple engines?
I am really not sure without looking at your whole project code base and how things are constructed.
So, you are using C#, so you don’t really have control of when objects get deconstructed
Generally, once the reference count = 0 the automatic garbage collector deconstructs the object
You can decrement your reference count by setting your engine pointer to null
void OnApplicationQuit()
{
engine = null;
}
It seems like maybe this is due to how your application is architected
If you can somehow reproduce this behavior within the provided Pulse asset, I can try to debug it
You can also just reuse the engine instance
You can call engine.SerializeFromString on the same engine object
You do not have to allocate a new engine each time in Start()
You can just allocate the engine in the class constructor