set_cellv doesn't seem to work

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

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.

:bust_in_silhouette: Reply From: kidscancode

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)

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.

xofox | 2019-09-10 02:26

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

xofox | 2019-09-10 02:46