I want to transform a local position into map coords using local_to_map(), the problem is when I click a button and send the position of that button when is pressed, console write this error:
E 0:00:02:0679 land_spawner_machine.gd:31 @ create_tile(): Condition "!tile_set.is_valid()" is true. Returning: Vector2i()
<C++ Fuente> scene/2d/tile_map.cpp:3946 @ local_to_map()
<Rastreo de Pila>land_spawner_machine.gd:31 @ create_tile()
spawner_btn.gd:19 @ _on_pressed()
I’m trying to make that when user click the button, set a new tile in the position of that button. This is my code:
extends TouchScreenButton
@onready var land_spawner = landSpawnerMachine.new()
func _on_pressed():
var point = Vector2(self.position)
land_spawner.create_tile(point)
class_name landSpawnerMachine
extends TileMap
var tile_zero = Vector2i(0,0)
# variables for layers and ID's
var source_id_tileSet = 0
var source_id_scenes = 1
var layer_1 = 1
# Called when the node enters the scene tree for the first time.
func _ready():
print(local_to_map(Vector2(309,148.0843)))
# this print (18,0)
func create_tile(point):
print(self)
var cell_point = local_to_map(point)
# ths should print (18,0) but it doesn't, print (0,0)
print(point)
print(cell_point)
#self.set_cell(layer_1,tile_zero,source_id_scenes,tile_zero,1)
it’s error because you try to create new object of the tilemap without add child it into scene tree during runtime
also it’s strange way to reference the tilemap, it shouldnt work like this
you just need to reference the current active tilemap to the touchscreen button, so you can tell current active tilemap to create/set the tile. not a new tilemap on touchscreenbutton
you can get the active tilemap by either passing it directly as paramater when you create the touchscreenbutton Or actually put the active tilemap in a Group, then get the first node of the group by scenetree to get the active tilemap
I don’t know what exactly are you referring… I can’t convert local to map coords on the touchScreenButton script, so I pass the position to his parent (my current tileMap) and work from there.
it’s generally not a good practice to use get_parent() in godot,
you can actually pass the tilemap as variable to this land_spawner variable as well. did you generate the touchscreenbutton from code? what’s the code?
If it’s not, then you can use @export var land_spawner, and assign(drag and drop) the LandSpawner Node from tree to inspector of each TouchScreenButton
Don’t want to put all my code here, but the function that create this buttons just need an array with the current used tiles and a coord to check. If the coord doesn’t exist, create the button: