How to make a tileset editor in game?

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

How can i make a tileset editor in game?
i am trying to do this but it doesn’t quiet work:
func _input(event):
if Input.is_action_just_pressed(“click”):
Global.click_pos = get_global_mouse_position
func _physics_process(delta):
$Tilemap.set_cell(Global.click_pos.x , Global.click_pos.y , 0)

:bust_in_silhouette: Reply From: Zylann

You have to convert mouse coordinates into tilemap coordinates using world_to_map: TileMap — Godot Engine (stable) documentation in English

Note, world_to_map does not actually take a world position, but a position relative to the origin of the Tilemap node. However it should be fine if it is at (0,0).

Assuming your tilemap is at the origin:

func _input(event):
	if Input.is_action_just_pressed("click"):
		Global.click_pos = get_global_mouse_position()

func _physics_process(delta):
	var cell_pos = $Tilemap.world_to_map(Global.click_pos)
	$Tilemap.set_cell(cell_pos.x, cell_pos.y, 0)

Also, you don’t need a global variable and two separate functions to set a cell in a tilemap. You can do it all in _input and use set_cellv as a shortcut:

func _input(event):
	if Input.is_action_just_pressed("click"):
		var cell_pos = $Tilemap.world_to_map(get_global_mouse_position())
		$Tilemap.set_cellv(cell_pos, 0)

Note: to format your code on this website, either indent it by one tab, or select your code and use the “Code Sample” button. Otherwise it’s hard to tell your code indentation and variable names…