Skip to content

Commit 3ba2cec

Browse files
Use TryGet pattern to get BcpInterface from GLE
1 parent 547961c commit 3ba2cec

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Runtime/MediaController/Text/MonitoredVariableText.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public abstract class MonitoredVariableText<TVar, TMessage> : MonitoredVariableT
3535

3636
private void OnEnable()
3737
{
38-
var bcpInterface = MpfGamelogicEngine.GetBcpInterface(this);
39-
if (bcpInterface != null)
38+
if (MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
4039
{
4140
_monitor = CreateMonitor(bcpInterface);
4241
SetText(_monitor.VarValue);

Runtime/MpfGamelogicEngine.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ private async void OnDestroy()
269269
MpfWrangler.Dispose();
270270
}
271271

272-
public static BcpInterface GetBcpInterface(Component requestingComponent)
272+
public static bool TryGetBcpInterface(
273+
Component requestingComponent,
274+
out BcpInterface bcpInterface
275+
)
273276
{
274277
var gle = requestingComponent.GetComponentInParent<MpfGamelogicEngine>();
275278

@@ -281,7 +284,8 @@ public static BcpInterface GetBcpInterface(Component requestingComponent)
281284
if (!Application.isPlaying)
282285
{
283286
Logger.Error(string.Format(errorMessage, "the game is not running."));
284-
return null;
287+
bcpInterface = null;
288+
return false;
285289
}
286290

287291
if (gle == null)
@@ -295,7 +299,8 @@ public static BcpInterface GetBcpInterface(Component requestingComponent)
295299
+ "table or remove the component that requested the BCP interface."
296300
)
297301
);
298-
return null;
302+
bcpInterface = null;
303+
return false;
299304
}
300305

301306
if (gle._wranglerOptions.MediaController != MpfMediaController.Included)
@@ -308,18 +313,21 @@ public static BcpInterface GetBcpInterface(Component requestingComponent)
308313
+ "logic engine inspector."
309314
)
310315
);
311-
return null;
316+
bcpInterface = null;
317+
return false;
312318
}
313319

314320
if (gle.MpfWrangler == null)
315321
{
316322
Logger.Error(
317323
string.Format(errorMessage, "the game logic engine is not initialized.")
318324
);
319-
return null;
325+
bcpInterface = null;
326+
return false;
320327
}
321328

322-
return gle.MpfWrangler.BcpInterface;
329+
bcpInterface = gle.MpfWrangler.BcpInterface;
330+
return true;
323331
}
324332

325333
public void DisplayChanged(DisplayFrameData displayFrameData) { }

0 commit comments

Comments
 (0)