Godot Version
4.5.1
Question
So I am making this game where a player gains light (life) in the light and loses it in the dark. I made this middle light source, where the player is able to heal. Now I made these "mirror" objects that should make another light source in which the player can heal. Now I want these mirrors to only work if they are in another light source, so they should radiate light if they overlap with other lights and outside of them it should not radiate any light.Now the problem is that I detect when the lights overlap and I try to turn off the mirror, but when I do that for multiple mirrors if one turns off it turns all of them off. How can I fix this? I’m open for code fixes or alternative solutions to my problem, I’ve been trying to fix my code for more than 10 hours already and it just wont work, so any help is appreciated.
screenshot of my tree.
This is the code:
extends RigidBody3D
@export var default_radius: float = 10.0
@export var radius_when_off: float = 0.0
var mirror_light: OmniLight3D
var area: Area3D
var collision_shape: CollisionShape3D
var active_light_sources: int = 0
func _ready():
mirror_light = find_child(“MirrorLight”, true, true)
area = find_child(“Area3D”, true, true)
collision_shape = find_child(“CollisionShape3D”, true, true)
update_light(false)
update_collision_radius(radius_when_off)
call_deferred(“_connect_light_sources”)
func _connect_light_sources():
var lights = get_tree().get_nodes_in_group(“light_source”)
for light in lights:
if light == self:
continue
var light_area: Area3D = light.find_child("Area3D", true)
light_area.body_entered.connect(_on_light_entered)
light_area.body_exited.connect(_on_light_exited)
func _on_light_entered(body):
if is_same(body, self):
active_light_sources += 1
update_light(true)
print(mirror_light.omni_range)
func _on_light_exited(body):
if is_same(body, self):
active_light_sources -= 1
if active_light_sources <= 0:
update_light(false)
print(mirror_light.omni_range)
func update_light(state: bool):
if state:
mirror_light.omni_range = default_radius
update_collision_radius(default_radius)
print(mirror_light.omni_range)
else:
mirror_light.omni_range = radius_when_off
update_collision_radius(radius_when_off)
print(mirror_light.omni_range)
func update_collision_radius(value: float):
if collision_shape and collision_shape.shape is SphereShape3D:
collision_shape.shape.radius = value
