Why error: "Condition '!tile_set.is_valid()' is true" happen in GDScript?

Godot Version

4.2.2

Question

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)

EDIT: I try to put self.local_to_map(point), but not work either

where the touchscreenbutton located?

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

It’s a child of the landSpawnerMachine tileMap node. It’s created dynamically with other function, maybe that’s the problem?

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.

the buttons are set from scenes collection on my tileMap script

but from the code said you make a new tilemap instead of using the current tilemap

since i already see your tree nodes structure
i can tell you can change to this:

@onready var land_spawner =$".."

and it should work

It work, exist some other form to refer its parent? Just to know

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:

func finder(arr,temp):
	if arr.find(temp) != -1:
		#print('full')
		return
	else:
		#print('empty')
		self.set_cell(layer_1,temp,source_id_scenes,tile_zero,3)

um, where the .instantiate() and add_child or TouchScreenButton.new()? if the touchscreenbutton is created from code

It’s created from scenes collection directly on the tileMap

self.set_cell(layer_1,temp,source_id_scenes,tile_zero,3)

this is not creating touchscreenbutton node. this line of code only to set a cell of the tilemap, not spawning/instantiating new touchscreenbuttons

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.