Make all instances of a scene react when one is activated

Hey team,

I’m trying to make doors react. What i want is when a room is cleared all the instances of the doors to open.

Hi,

The most straightforward way of doing that is having a reference to your doors, and looping through them. Considering you have a class Door, this would look like this:

class_name Room

@export var doors: Array[Door] = []

func on_cleared():
    for door in doors:
        door.open()

You can export the doors array to reference them by hand in the inspector, or find some way to get them on room start.

Another way is using a signal: once cleared, the room emits a signal, and doors are listening to it to know that should open themselves.
This would look something like this:

class_name Room

signal cleared

func on_cleared():
    cleared.emit()
class_name Door

@export var room: Room

func _ready():
    room.cleared.connect(open)

func open():
    # opening behaviour

This will make the room more independent as it does not have to know about any doors, but the doors dependent of the room they belong to. Again, you would have to reference the room in the doors inspector or get it dynamically. For instance, if the doors are children of the room, you could do something like this:

func _ready():
    (get_parent() as Room).cleared.connect(open)

That’s two ways you can do that.
Let me know if that helps or if you need a more precise answer based on your exact context!

1 Like

That’s perfect, cheers mate :smile:

1 Like