Put Light to a specific position with gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Blueberry

Hi all,

I want to add a glowing lava tile to my tilemap, while the other tiles should not glow.
I figured out, one way to do that, is to add a light to the postition of those lava tiles.

With the following code I created a light for each lava element, but the light always stays at Vector2i(0,0) and I was not able to move it to e.g. Vector2i(5,5) where my lava tile is located.

func create_light_source_for_tile(cell: Vector2i):
	var new_light: PointLight2D = PointLight2D.new()
	new_light.texture = preload("res://src/assets/etc/light.png")
	new_light.position = Vector2i(5, 5)
	add_child(new_light)

The light is created, but it stays at 0,0 and I’m not able to move it.

If you have any idea pls help.

A quick test using the provided code puts new_light at (5, 5) so perhaps here your parent is changed its children, or some other process is interacting with the child placement. Perhaps you also intended to use cell in the create_light_source_for_tile function?

spaceyjase | 2023-04-11 11:14

Your right, after testing with a quick dummy scene the result looks like I want it to be

See: Screenshot-2023-04-11-141556 hosted at ImgBB — ImgBB

But why it’s not working with my tilemap? How to position it correct to the parrent?

Blueberry | 2023-04-11 12:18

Are you sure it isn’t in the correct place? I took the example script and put it on a TileMap (that is extends TileMap), the light is in the correct position (Remote tab shows this).

Perhaps it doesn’t look as expected? Is the light interacting with the TileMap? Maybe it isn’t shown due to layers or color modulation? Do you have an example project?

spaceyjase | 2023-04-11 14:03

:bust_in_silhouette: Reply From: Blueberry

I found the error. My tilemap is using a different positioning system after adding “map_to_local” it worked!

create_light_source_for_tile(map_to_local(Vector2i(x,y)))