I have simplified another implementation that didn't work. It seems that I can get a location but I can't set it. So I click on a place on the screen and the right method is called to add the object in, but when I click there again the method to remove it is not called.
This is the relevant code:
func initBools():
for i in range(512):
tundat1.append([])
for j in range(512):
tundat1[i].append(false)
func get_location(var locx: int, var locy: int):
return tundat1[locx][locy]
func set_location(var locx: int, var locy: int, var mode):
tundat1[locx][locy] = mode
func _ready():
initBools()
func _input(event):
if event is InputEventMouseButton:
var loca: Vector2 = event.position
if loca.x < 1024 and loca.y < 1024:
var xprt = floor(loca.x / 8)
var yprt = floor(loca.y / 8)
var isthere = get_location(xprt + scoffx * 2, yprt + scoffy * 2)
isthere = !isthere
set_location(xprt + scoffx * 2, yprt + scoffy * 2, isthere)
if event.is_pressed():
if isthere:
var pasSpa: Sprite = spaRef.instance()
pasSpa.global_transform.origin.x = xprt * 8 + 4
pasSpa.global_transform.origin.y = yprt * 8 + 4
pasSpa.name = "square" + str(scrcnt)
add_child(pasSpa)
scrcnt += 1
sqposx.append(xprt)
sqposy.append(yprt)
else:
for i in range(scrcnt):
if xprt == sqposx[i] and yprt == sqposy[i]:
var delref = get_node("square" + str(i))
if delref:
self.remove_child(delref)
delref.queue_free()`