Godot Version
4
Question
I am having a problem with set_cell. In one function for mouse click, it works perfectly well and drops a new tile.
In another function triggered by a signal, however, everything works EXCEPT for the line containing set_cell. All the print lines work for the something_happened function, for example.
Why is this (not) happening with set_cell? It is getting fed the exact same parameters.
Here is the main scene script.
…
extends Node2D
onready var tile_map : TileMap = %TileMap
var ground_layer : int = 1
var obstacle_layer = 2
func _on_main_player_something_happened():
if Input.is_action_pressed("ui_right"):
var mouse_pos : Vector2 = get_global_mouse_position()
var tile_mouse_pos : Vector2i = tile_map.local_to_map(mouse_pos)
var source_id : int = 0
var atlas_coord : Vector2i = Vector2i(3,4)
print("ground layer is ", ground_layer )
print("tile mouse position is ", tile_mouse_pos )
print("source id is ", source_id )
print("tile ac is ", atlas_coord )
tile_map.set_cell(ground_layer, tile_mouse_pos, source_id, atlas_coord)
func _input(event):
if Input.is_action_pressed("click"):
var mouse_pos : Vector2 = get_global_mouse_position()
var tile_mouse_pos : Vector2i = tile_map.local_to_map(mouse_pos)
var source_id = 0
var atlas_coord : Vector2i = Vector2i(3,4)
print("ground layer is ", ground_layer )
print("tile mouse position is ", tile_mouse_pos )
print("source id is ", source_id )
print("tile ac is ", atlas_coord )
tile_map.set_cell(ground_layer, tile_mouse_pos, source_id, atlas_coord)
…
Here is the code for the script emitting the signal:
…
extends CharacterBody2D
signal something_happened
#emulating grid by using an array
var currPos = Vector2(160, 160) # this is going to be our x and y...
func _input(event):
var move_dir = Vector2.ZERO
if event.is_action_pressed("ui_right"):
move_dir.x += 8
get_node("CharacterBlue").look_at(self.position + Vector2(1,0))
emit_signal("something_happened")
move(move_dir)
func droptilefunc():
print("player moved now drop a tile")
func move(move_dir):
var new_pos = currPos + move_dir
var collision = move_and_collide(move_dir)
if collision:
pass
else:
currPos = new_pos
self.position = currPos
…