How can i make the brightness of a point light correspond to the brightness in a room?

Godot Version

godot 4

Question

i have a muzzle flash point light in my player scen for my 2d game and want the brightness to be high when i’m in a dark room in the level scene but low when i’m in a bright room. Currently it just stays the same

I assume you use a CanvasModulate node for changing the light level? Change the energy of the point light based on the current brightness of the CanvasModulate color (color.v). The energy of the light should be lower when color.v is higher and vice versa.

i have a canvas modulate covering the entire scene, and then i have rooms with individual point lights of varying brightness. when i printed the color.v it stayed the same all the time so the brightness wont change

Edit. It seems I misunderstood your issue. So all rooms are in the same scene covered with the same canvas modulate and you meant you wanted to adjust the point light based on overall light level from canvas modulate + other lights. Sorry, in that case this won’t work.

Sorry, I was a bit unclear. If the brightness (color.v) of CanvasModulate does not change, it doesn’t really matter. You still would need to adjust the energy of the point light based on color.v of the CanvasModulate node. In dark rooms, the canvas modulate has a dark color, right? You need a way to change the energy of the point light based on that. You could use a signal to pass the color.v value to the point light and then change the energy. Define the min and max energy the light can have and then lower the value x amount.

A quick example (didn’t test in the editor but something like this should work):

extends PointLight2D

var min_energy = 0.5
var max_energy = 2.0

func change_energy(color_v: float):
  energy = clamp((1.0 - color_v) * max_energy, min_energy, max_energy)

it’s getting closer to what i want but it seems to instead now make it darker as a move into darkness when i want the opposite. This is the code i’m using in my player script.

muzzle_flash_light.energy = clamp((1 - get_tree().get_first_node_in_group("CanvasModulate").color.v), 0.2, 2)


it’s the right effect but i want it reversed. How can i do that?