Skip to content

Commit 472f893

Browse files
committed
BaseInlet - updates now virtual; add support for EndOfFrame; stream found/lost handlers no longer required
1 parent 046c576 commit 472f893

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

Runtime/Scripts/BaseInlet.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
using LSL;
2-
using System;
1+
using System;
32
using System.Linq;
3+
using System.Collections;
44
using System.Collections.Generic;
55
using UnityEngine;
6-
using UnityEngine.Events;
6+
using LSL;
7+
78

89
namespace LSL4Unity.Utils
910
{
1011
public abstract class ABaseInlet<TData> : MonoBehaviour
1112
{
1213
public string StreamName;
13-
1414
public string StreamType;
15+
// Hook in frame lifecycle in which to pull data.
16+
public MomentForSampling moment;
1517

1618
// Set this to true to only pass the last sample in a chunk to Process.
1719
public bool ProcessLastInChunkOnly = false;
1820

1921
// Duration, in seconds, of buffer passed to pull_chunk. This must be > than average frame interval.
2022
double maxChunkDuration = 0.2;
2123

22-
// Hook in frame lifecycle in which to pull data.
23-
public MomentForSampling moment;
24-
2524
protected StreamInlet inlet;
2625
public Resolver resolver;
2726
protected double[] timestamp_buffer;
@@ -123,6 +122,7 @@ protected virtual bool isTheExpected(StreamInfo stream_info)
123122
// Must override for each data type because LSL can't handle Generics.
124123
protected abstract void pullChunk();
125124

125+
// Process will be called by one of FixedUpdate, Update, LateUpdate, or a
126126
protected abstract void Process(TData[] newSample, double timestamp);
127127

128128
protected void ProcessChunk(int n_samples)
@@ -139,35 +139,51 @@ protected void ProcessChunk(int n_samples)
139139
}
140140
}
141141

142+
// Child class should implement a method to do more setup after the target stream is available.
142143
protected virtual void OnStreamAvailable()
143144
{
144145
// base implementation may not decide what happens when the stream gets available
145-
throw new NotImplementedException("Please override this method in a derived class!");
146+
// throw new NotImplementedException("Please override this method in a derived class!");
146147
}
147148

149+
// Child class should implement a method to cleanup any resources that depend on the stream,
150+
// and might change if a stream becomes available again but has somehow changed.
148151
protected virtual void OnStreamLost()
149152
{
150153
// base implementation may not decide what happens when the stream gets lost
151-
throw new NotImplementedException("Please override this method in a derived class!");
154+
// throw new NotImplementedException("Please override this method in a derived class!");
152155
}
153156

154-
protected void FixedUpdate()
157+
protected virtual void FixedUpdate()
155158
{
156159
if (moment == MomentForSampling.FixedUpdate && inlet != null)
157160
pullChunk();
158161
}
159162

160-
protected void Update()
163+
protected virtual void Update()
161164
{
162165
if (moment == MomentForSampling.Update && inlet != null)
163166
pullChunk();
167+
else if (moment == MomentForSampling.EndOfFrame && inlet != null)
168+
{
169+
// Pull and process as close to render-time as possible.
170+
// No idea why you'd want to use this; it won't affect anything in the game until the next frame.
171+
StartCoroutine(PullAfterRendered());
172+
}
164173
}
165174

166-
protected void LateUpdate()
175+
protected virtual void LateUpdate()
167176
{
168177
if (moment == MomentForSampling.LateUpdate && inlet != null)
169178
pullChunk();
170179
}
180+
181+
IEnumerator PullAfterRendered()
182+
{
183+
yield return new WaitForEndOfFrame();
184+
pullChunk();
185+
yield return null;
186+
}
171187
}
172188

173189
public abstract class AFloatInlet : ABaseInlet<float>

0 commit comments

Comments
 (0)