0 votes

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.

Godot version 3.3stable
in Engine by (12 points)

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.

1 Answer

+1 vote

You are not using maptoworld and worldtomap 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)
by (2,406 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.