Hello everyone, In my first project on the godot engine, i've created a cavern level. In this cavern i've crystals and i want them to emit light. I'm asking if there's a simple way to make my crystal tile have a default Light2D associated to it or am i doomed to manualy add a node centered on my tile. Thank you for yours answers
Light on specific tile
- Edited
Updated: I've added a script to the parent node in wich i access the tilemap (here $Deco), but the problem now is that my script doesn't seems to run ( i've tried to print something in the ready() and physic_process(delta) functions, but nothing show). I also still need to implement the light
var cell_positions = $Deco.get_used_cells()
for pos in cell_positions:
var tile_id = $Deco.get_cellv(pos)
var tile_name = $Deco.get_tileset().tile_get_name(tile_id)
if tile_name == "crystal_tile":
# implement light
Udated II:
Now my script is running by magic (i've done nothing but now it works), and i've created a specific tile (not an atlas) for the crystal (id: 1). I've adapted my code with $Deco.get_used_cells_by_id(1)
and now i just have to figure how to create light at a certain position
You can create a Light2D node using something like this:
var new_light = Light2D.new()
add_child(new_light)
new_light.global_position = $Deco.map_to_world(pos)
Or if you want to spawn an instanced scene, then instead of Light2D.new()
you would want scene_to_instance.instance()
where scene_to_instance
is a loaded/preloaded .tscn
file.
From my experience, one of the big things with spawning nodes/scenes from code is to position them after adding them to the scene, as I have found it's the most reliable way to get it at the exact position. Sometimes setting it prior to adding to the scene can lead to offset, or at least it did several versions ago (might have been a bug, thinking about it...)
- Edited
Maybe i'm doing something wrong , but no light seems to show with the first method (the second lead to an error ). My first thought was "I need to enable it with code" but nothing change. The code is the same as yours but i've addeda function to change the light color, energy , etc Here's my tree (with the script attached to 'Level' node, and the crystal tile in the 'deco' tilemap) and the look of a crystal from this tilemap ( the other crystal has manually installed light)
!
!
Maybe the light is hidden behind the Tilemap or background? Maybe try adding new_light.z_index = 10
, as that should push it in front of all the other elements.
- Edited
Even with that the light doesn't show up.
Maybe by making the light child of deco tilemap to make it inherit the layer should resolve the problem but i'm not sure.
Otherwise i've tried to manually set a Light2D at the position given by $Deco.map_to_world(pos)
and the position is right so i dont think it's the probem.
Updated:
I'm such an idiot, i've forgot to set a texture to my light ...
Now that i've done that, my light appear somewhere it should'nt be with the wrong color, energy, etc
!
Update II: The problem is due to .color attribute of light; i've set it to Color(106,93,110,255) --> full white instead of Color8(106,93,110,255) I've also a big offset that i need to correct.
Update III:
The offset problem come from the fact that i import my scene 'Level' in a other one name 'Game' so the tilemap is centered in (0,416) . A simple correction is that i need to adjust the light offset with a Vector2(0,416) . But there's an kind of occluder (that i didn't set ) that make my light hidden behind.
!
Last Update:
The occluder came from the fact that i've set the shadow_enable = true