Best way to bind systems and their related pop-up GUI?

Hey,

Say I have a Keypad behavior with a 4-digit pin code that’s definable in the Inspector. If the player interacts with those keypads, it brings up the Keypad GUI which is its own class and they can input a pin code; then the Keypad script checks if it’s correct and calls the appropriate event — which can do things like opening a door on success or triggering an alarm on failure. Fairly straightforward, and it works fine.

Still, I can’t think of a way to achieve this that doesn’t involve the Keypad instance passing itself as reference to the Keypad GUI; that is, the latter needs a way to access the former. The Keypad GUI itself doesn’t know anything about the various Keypad instances, after all.

I could have a hard reference to the GUI in my Keypad class, but that basically means having a “Keypad GUI” field that needs to be filled on every instance even though I only have one unchanging Keypad GUI. And what if I decide to change the GUI at some point? I have to go back and change all those Keypad GUI fields on every keypad, possibly missing some in the process. Clearly, that’s a brittle architecture.

I could use a ScriptableObject-based event system, with the Keypad GUI listening to the OnKeypadInteraction event called from the Keypad class. That’s better already: the coupling is looser and it’s much less error-prone, but I still have to manually add that event to every instance of the keypad. That won’t do.

I could also try and find the Keypad GUI with its name or tag from code in the Keypad class, and store it in a private static variable for all instances of Keypad to have access to. That gets rid of having fields dedicated to finding the GUI on every instance, but it introduced its own set of problems related to the error-prone and change-averse nature of relying on strings for finding stuff.

The only solution I can think of is a singleton: calling the static instance of the Keypad GUI directly from the code in the Keypad class. But I try to avoid using singleton: they introduce tight coupling and end up concealing dependencies, which can quickly turn the testing of individual system in a nightmare among other drawbacks.

What is the best way to bind the keypad logic (of which there are many instances in the game world) to the keypad GUI (of which there is only one single instance) without resorting to the patterns I have described? Assistance would be greatly appreciated. Cheers!

I hate dragging and connecting crud all over my scenes. It’s so fragile and prone to error, and when something misbehaves it requires painstakingly digging through the scene looking for stuff out of place.

I would way rather just have my code respond to known user intentions, whatever they are.

That’s why I wrote my Datasacks system.


Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

1 Like

My apologies for the late reply. Thank you for your response, I’ll be sure to check this out.