0 votes

I filled up a TileMap and then attached this code to try and erase it ingame.

extends TileMap
onready var map = get_node("TileMap")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var mousePosition = Vector2()
# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.
func _process(delta):
    mousePosition = get_global_mouse_position()
    map.set_cellv(mousePosition, -1)

Nothing happened though after I ran it and none of the previously determined cells were changed. What am I doing wrong that is causing the cells to not change.

in Engine by (92 points)

1 Answer

+1 vote
Best answer

get_global_mouse_position() is in pixel coordinates. set_cellv() needs map coordinates. Something happened, it's just that the tile you're changing is way off screen.

You need to convert using world_to_map():

func _process(delta):
    mousePosition = map.world_to_map(get_global_mouse_position())
    map.set_cellv(mousePosition, -1)
by (20,673 points)
selected by

Awesome thanks! I appreciate the answer. I thought that it wasn't changing. Now in this other block of code still nothing seems to be happening. Wanna look over it for me? Sorry for being a hassle.

func start_point():
    var startLocation = Vector2(floor(rand_range(2,5)),floor(rand_range(2,5)))
    set_cellv(startLocation,0)
    player.position = map_to_world(startLocation)
    print(startLocation)

Thanks again I really appreciate it.

Hey I found my problem. I was trying to reference a different scene from one scene. my bad. Thanks for the help!

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.