Open arthurkehrwald opened 1 month ago
I tried to track down what causes the ids to be set to null by debugging, but I can't figure it out. Nothing seems to change the ids or replace the switches in the array on the MPF game logic engine, they're just null as soon as the game starts
I found the problem. This commit in the main VPE repository changed the access specifier of the switch, coil and lamp IDs from public to private. This causes Unity to no longer serialize their values. From the Unity documentation:
To use field serialization you must ensure that the field:
- Is public, or has a SerializeField attribute
I can think of the following options to fix this: | Option | Downside |
---|---|---|
Make the properties public again | Violates encapsulation principle, A (bad) design decision made because of an implementation detail of Unity in a project that should not be dependent on it | |
Add the [SerializeField] attribute to the properties |
Introduces a dependency on Unity to the core part of VPE that is not supposed to have third-party dependencies | |
Make the properties protected , create new subclasses of GameLogicEngineCoil , GameLogicEngineSwitch and GameLogicEngineLamp in the VisualPinball.Unity project and implement Unity's ISerializationCallbackReceiver interface to serialize the protected properties of their respective parent classes |
Maintenance burden, increased complexity |
I think the downsides of the last option are the least severe.
On top of this, when the machine description is updated via the inspector, the Unity Editor must be notified of the changes for them to be saved with the scene.
I have created pull request #20 in this repository and this pull request in the main VPE repository to implement the proposed changes and solve this issue.
When starting the table in the editor with an
MpfGameLogicEngine
component containing a valid machine description attached, theOnInit
function inMpfGameLogicEngine.cs
throws the following exception:The
After clicking play:
![Screenshot 2024-07-14 173104](https://github.com/user-attachments/assets/93792d41-55c0-45fc-be8e-912ceec482e4)
Id
property of all switches, coils and lights, is set to null when the game is started. This is also visible in the Inspector. Before clicking play:I am using: