Skip to content

Commit 685bd17

Browse files
committed
misc: Added new GLE OnStarted event handler. Replaced PlayerStartedEvent with GleStartedEvent
1 parent 25e94eb commit 685bd17

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

Runtime/Gamelogic/VisualScriptingEventNames.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace VisualPinball.Unity
1818
{
1919
public static class VisualScriptingEventNames
2020
{
21-
public const string PlayerStartedEvent = "PlayerStartedEvent";
21+
public const string GleStartedEvent = "GleStartedEvent";
2222
public const string LampEvent = "LampEvent";
2323
public const string SwitchEvent = "SwitchEvent";
2424
public const string CoilEvent = "CoilEvent";

Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,54 @@ namespace VisualPinball.Unity.VisualScripting
2323
[DisallowMultipleComponent]
2424
[RequireComponent(typeof(IGamelogicEngine))]
2525
[RequireComponent(typeof(Player))]
26-
[AddComponentMenu("Visual Pinball/Game Logic Engine/Visual Scripting Bridge")]
26+
[AddComponentMenu("Visual Pinball/Gamelogic Engine/Visual Scripting Bridge")]
2727
public class VisualScriptingGamelogicBridge : MonoBehaviour
2828
{
29-
private IGamelogicEngine _gle;
3029
private Player _player;
30+
private IGamelogicEngine _gle;
31+
32+
private bool _init;
3133

3234
private void Awake()
3335
{
34-
_gle = GetComponent<IGamelogicEngine>();
36+
_init = false;
37+
3538
_player = GetComponent<Player>();
36-
if (_gle == null) {
37-
Debug.LogWarning("Cannot find gamelogic engine.");
38-
return;
39-
}
4039
if (_player == null) {
4140
Debug.LogWarning("Cannot find player.");
42-
return;
4341
}
4442

45-
_player.OnPlayerStarted += OnPlayerStarted;
43+
_gle = GetComponent<IGamelogicEngine>();
44+
if (_gle != null) {
45+
_gle.OnStarted += OnStarted;
46+
}
47+
else {
48+
Debug.LogWarning("Cannot find gamelogic engine.");
49+
}
4650
}
4751

48-
private void OnDestroy()
49-
{
50-
if (_player != null) {
51-
_player.OnPlayerStarted -= OnPlayerStarted;
52+
private void OnDestroy() {
53+
if (_gle != null) {
54+
_gle.OnStarted -= OnStarted;
55+
56+
if (_init) {
57+
_gle.OnSwitchChanged -= OnSwitchChanged;
58+
_gle.OnCoilChanged -= OnCoilChanged;
59+
_gle.OnLampChanged -= OnLampChanged;
60+
}
5261
}
62+
}
63+
64+
private void OnStarted(object sender, EventArgs e)
65+
{
5366
if (_gle != null) {
54-
_gle.OnSwitchChanged -= OnSwitchChanged;
55-
_gle.OnCoilChanged -= OnCoilChanged;
56-
_gle.OnLampChanged -= OnLampChanged;
67+
_gle.OnSwitchChanged += OnSwitchChanged;
68+
_gle.OnCoilChanged += OnCoilChanged;
69+
_gle.OnLampChanged += OnLampChanged;
70+
71+
_init = true;
72+
73+
EventBus.Trigger(VisualScriptingEventNames.GleStartedEvent, e);
5774
}
5875
}
5976

@@ -67,21 +84,9 @@ private static void OnCoilChanged(object sender, CoilEventArgs e)
6784
EventBus.Trigger(VisualScriptingEventNames.CoilEvent, e);
6885
}
6986

70-
private void OnPlayerStarted(object sender, EventArgs e)
71-
{
72-
if (_gle != null) {
73-
_gle.OnSwitchChanged += OnSwitchChanged;
74-
_gle.OnCoilChanged += OnCoilChanged;
75-
_gle.OnLampChanged += OnLampChanged;
76-
}
77-
78-
EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty);
79-
}
80-
8187
private static void OnLampChanged(object sender, LampEventArgs e)
8288
{
8389
EventBus.Trigger(VisualScriptingEventNames.LampEvent, e);
8490
}
85-
8691
}
8792
}

Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace VisualPinball.Unity.VisualScripting
2626
{
2727
[DisallowMultipleComponent]
28-
[AddComponentMenu("Visual Pinball/Game Logic Engine/Visual Scripting Game Logic")]
28+
[AddComponentMenu("Visual Pinball/Gamelogic Engine/Visual Scripting Game Logic")]
2929
public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
3030
{
3131
public string Name => "Visual Scripting Gamelogic Engine";
@@ -51,6 +51,7 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
5151
public event EventHandler<LampColorEventArgs> OnLampColorChanged;
5252
public event EventHandler<CoilEventArgs> OnCoilChanged;
5353
public event EventHandler<SwitchEventArgs2> OnSwitchChanged;
54+
public event EventHandler<EventArgs> OnStarted;
5455

5556
[NonSerialized] public BallManager BallManager;
5657
[NonSerialized] private Player _player;
@@ -59,7 +60,8 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
5960
{
6061
_player = player;
6162
BallManager = ballManager;
62-
EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty);
63+
64+
OnStarted?.Invoke(this, EventArgs.Empty);
6365
}
6466

6567
public void Switch(string id, bool isClosed)
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@
1919

2020
namespace VisualPinball.Unity.VisualScripting
2121
{
22-
[UnitTitle("On Player Started Event")]
22+
[UnitTitle("On Gamelogic Engine Started Event")]
2323
[UnitCategory("Events\\Visual Pinball")]
24-
public sealed class PlayerStartedEventUnit : EventUnit<EventArgs>
24+
public sealed class GleStartedEventUnit : EventUnit<EventArgs>
2525
{
2626
protected override bool register => true;
2727

2828
public override EventHook GetHook(GraphReference reference)
2929
{
30-
return new EventHook(VisualScriptingEventNames.PlayerStartedEvent);
31-
}
32-
33-
protected override void Definition()
34-
{
35-
base.Definition();
30+
return new EventHook(VisualScriptingEventNames.GleStartedEvent);
3631
}
3732
}
3833
}

Runtime/Nodes/PlayerStartedEventUnit.cs.meta renamed to Runtime/Nodes/GleStartedEventUnit.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)