- Home /
Switches and doors - avoid circular dependencies
I’m stuck on a problem and I’m even having a hard time articulating what the problem is. Here goes:
I want to create one or more “switches” that a player can land on. When the player lands, I’ll use the OnCollisionEnter method to detect the collision and invoke a UnityEvent (or maybe just a C# Event). That part is simple: user collides, a script detects collision and invokes and event.
The object(s) that have the event listener will fire off a method to perform some action. In a simple example, a user lands on a switch described above and a door opens. I have this working with no issue. Where I’m stuck is how to deal with a door that requires multiple switches? I could create an array of required switches on the door. Then, when the UnityEvent is invoked from a switch, the script attached to the door could loop though the required switch array and check a property on each switch for a bool value. If switches all have a true value for example, open the door. If only some have a true value, perhaps play a sound effect or do nothing.
Would setting this up cause some sort of circular dependencies between the switches and doors? Is there a better or simpler way to solve this that I’m just not considering?
Answer by troien · Mar 07, 2019 at 03:24 PM
Depends on what you mean with circular dependency.
If you code this in a sensible way, you can't get a circular dependency in code, so you can't get stackoverflow exceptions from this logic. There is no need to call anything resursively. So as long as doors don't switch toggles that doors depend on when you open doors, you're safe.
But you can get a 'circular dependency' in the way that if you set it up so that:
Door A depends on door B to be open
Door B depends on door C to be open
Door C depends on door A to be open
Door D depends on door A, B and C to be open
Then although the code will not go into an infinite loop, you'll logically not be able to open any doors, unless A, B or C starts opened already (although that would mean that closing that door would get people stuck).
As switches and doors are not the same in your case, you could read this as, "the switch(es) to open door A is behind door B", etc.
Example:
public class MySwitch : MonoBehaviour
{
public bool Toggled;
private List<Door> doors = new List<Door>(); // Filled by doors on startup, so no need to set in inspector :)
public void AddDoor(Door door)
{
doors.Add(door);
}
public void Toggle() // Call this whenever switch is toggled on
{
for (int i = 0; i < doors.Count; i++)
{
doors[i].Open();
}
}
}
public class Door : MonoBehaviour
{
public MySwitch[] dependencies; // list of switches that need to be 'on'
private void Start()
{
for (int i = 0; i < dependencies.Length; i++)
{
dependencies[i].AddDoor(this);
}
}
public void Open()
{
for (int i = 0; i < dependencies.Length; i++)
{
if (!dependencies[i].Toggled)
{
// NOT allowed to open
return;
}
}
// Allowed to open
}
}
Answer by Casiell · Mar 07, 2019 at 08:15 AM
From what you are saying, door doesn't need to know about all the switches. It just has to keep track of how many switches are pressed at the moment.
Door could have a counter and a max value (let's say 3).
When you press a switch and a method will be called. In this method door will increment the counter by 1. When it increments a third time, the door will open.
You would probably need a second event for when you "unswitch" your switch and it will decrement your counter by 1
Your answer
![](https://koobas.hobune.stream/wayback/20220612193106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Referencing a variable to a prefab from another script that is applied to multiple game objects 0 Answers
Is Merging GameObject Arrays Possible? 2 Answers
Removing GameObjects from list (with List.Remove()) doesn't seem to be removing them 2 Answers