Skip to content

Commit 4fcd889

Browse files
committed
fix: Retrieve table and GLE globally instead from reference.
1 parent df5e636 commit 4fcd889

10 files changed

+39
-55
lines changed

Editor/Widgets/GetLampUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4747

4848
private IEnumerable<string> GetNameSuggestions()
4949
{
50-
if (!GameObjectAvailable) {
51-
return new List<string>();
52-
}
53-
var gle = Gle;
54-
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
50+
return !GleAvailable
51+
? new List<string>()
52+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
5553
}
5654
}
5755
}

Editor/Widgets/GetSwitchUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
5555

5656
private IEnumerable<string> GetNameSuggestions()
5757
{
58-
if (!GameObjectAvailable) {
59-
return new List<string>();
60-
}
61-
var gle = Gle;
62-
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.AvailableSwitches.Select(sw => sw.Id).ToList();
6361
}
6462
}
6563
}

Editor/Widgets/GleUnitWidget.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using Unity.VisualScripting;
18+
using UnityEngine;
1819

1920
namespace VisualPinball.Unity.VisualScripting.Editor
2021
{
@@ -30,19 +31,20 @@ public static class GleUnitWidget
3031
public abstract class GleUnitWidget<TUnit> : UnitWidget<TUnit> where TUnit : Unit, IGleUnit
3132
{
3233
protected override NodeColorMix baseColor => GleAvailable ? GleUnitWidget.Color : NodeColor.Red;
33-
protected bool GameObjectAvailable => reference != null && reference.gameObject != null;
34-
protected IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
35-
protected VisualScriptingGamelogicEngine VsGle => reference.gameObject.GetComponentInParent<VisualScriptingGamelogicEngine>();
36-
private bool GleAvailable => GameObjectAvailable && Gle != null;
37-
private bool VsGleAvailable => GameObjectAvailable && VsGle != null;
34+
protected IGamelogicEngine Gle;
35+
protected VisualScriptingGamelogicEngine VsGle;
36+
protected bool GleAvailable => Gle != null;
37+
protected bool VsGleAvailable => VsGle != null;
3838

3939
protected GleUnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
4040
{
41-
if (!GameObjectAvailable) {
42-
unit.Errors.Add("Not attached to GameObject. You need to attach this graph to a flow machine sitting on a GameObject in order to use it.");
43-
44-
} else if (!GleAvailable) {
45-
unit.Errors.Add("No gamelogic engine found. One of the GameObject's parents must have a gamelogic engine component.");
41+
var table = TableSelector.Instance.SelectedOrFirstTable;
42+
if (table != null) {
43+
Gle = table.GetComponentInChildren<IGamelogicEngine>();
44+
VsGle = table.GetComponentInChildren<VisualScriptingGamelogicEngine>();
45+
}
46+
if (!GleAvailable) {
47+
Debug.LogError($"Cannot find GLE for {GetType()}.");
4648
}
4749
}
4850
}

Editor/Widgets/LampEventUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4747

4848
private IEnumerable<string> GetNameSuggestions()
4949
{
50-
if (!GameObjectAvailable) {
51-
return new List<string>();
52-
}
50+
return !GleAvailable
51+
? new List<string>()
52+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
5353

54-
var gle = Gle;
55-
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
5654
}
5755
}
5856
}

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
5555

5656
private IEnumerable<string> GetNameSuggestions()
5757
{
58-
if (!GameObjectAvailable) {
59-
return new List<string>();
60-
}
61-
var gle = Gle;
62-
return gle == null ? new List<string>() : gle.AvailableCoils.Select(coil => coil.Id).ToList();
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.AvailableCoils.Select(coil => coil.Id).ToList();
6361
}
6462
}
6563
}

Editor/Widgets/SetCoilUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
5555

5656
private IEnumerable<string> GetNameSuggestions()
5757
{
58-
if (!GameObjectAvailable) {
59-
return new List<string>();
60-
}
61-
var gle = Gle;
62-
return gle == null ? new List<string>() : gle.AvailableCoils.Select(coil => coil.Id).ToList();
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.AvailableCoils.Select(coil => coil.Id).ToList();
6361
}
6462
}
6563
}

Editor/Widgets/SetLampEnabledUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4646

4747
private IEnumerable<string> GetNameSuggestions()
4848
{
49-
if (!GameObjectAvailable) {
50-
return new List<string>();
51-
}
52-
var gle = Gle;
53-
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
49+
return !GleAvailable
50+
? new List<string>()
51+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
5452
}
5553
}
5654
}

Editor/Widgets/SetLampUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4646

4747
private IEnumerable<string> GetNameSuggestions()
4848
{
49-
if (!GameObjectAvailable) {
50-
return new List<string>();
51-
}
52-
var gle = Gle;
53-
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
49+
return !GleAvailable
50+
? new List<string>()
51+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
5452
}
5553
}
5654
}

Editor/Widgets/SwitchEnabledEventUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
5555

5656
private IEnumerable<string> GetNameSuggestions()
5757
{
58-
if (!GameObjectAvailable) {
59-
return new List<string>();
60-
}
61-
var gle = Gle;
62-
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.AvailableSwitches.Select(sw => sw.Id).ToList();
6361
}
6462
}
6563
}

Editor/Widgets/SwitchEventUnitWidget.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4747

4848
private IEnumerable<string> GetNameSuggestions()
4949
{
50-
if (!GameObjectAvailable) {
51-
return new List<string>();
52-
}
53-
var gle = Gle;
54-
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
50+
return !GleAvailable
51+
? new List<string>()
52+
: Gle.AvailableSwitches.Select(sw => sw.Id).ToList();
5553
}
5654
}
5755
}

0 commit comments

Comments
 (0)