Unable to instance scene on certain tiles

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

As the title states, I am trying to instance a scene (pickup) on every one of the specified tiles (pink). Here is my code so far:

onready var pickup_scene = preload("res://Pickup.tscn")

func spawn_pickup(tile_position: Vector2):
	var instance = pickup_scene.instance()
	instance.position = tile_position
	add_child(instance)

func generate_pickup() -> void:
	for tile_position in get_used_cells():
		if get_cellv(tile_position) == tile_set.find_tile_by_name("pink"):
			spawn_pickup(tile_position)

this code is located at the very bottom of my tilemap’s script. I also have an enum with my tilemap name’s and id’s at the top which is where my pink tile is named.

When I run this code I get no error, but I also have no pickups spawning on my pink tiles. What am I doing wrong?
Thanks in advance.

Just to make sure, you are calling generate_ pickup() at some point?

get_ used _cells _by _ id() might be faster, where you dont have to pull up the entire tile map, although it probably it does.

MattMakingAGame | 2021-10-20 06:52

:bust_in_silhouette: Reply From: Inces

You are not using map_to_world and world_to_map translations. Tile position is its X and Y index, not real global position.

All You have to do is :

instance.global_position = map_to world(tile_position)

So I’ve tried implementing this like so:

onready var pickup_scene = preload("res://Pickup.tscn")

func spawn_pickup(tile_position: Vector2):
    var instance = pickup_scene.instance()
    add_child(instance)
    instance.position = tile_position

func generate_pickup() -> void:
	for tile_position in get_used_cells():
		  if get_cellv(tile_position) == tile_set.find_tile_by_name("pink"):
		    var tile_world_position = map_to_world(tile_position)
		    spawn_pickup(tile_world_position)

And I’m still receiving 0 errors and no pickups spawning.

TheWomboKombo | 2021-10-20 18:04