The below code works for some moves but in some point the "new_empty" becomes null and balls stop change position with mouse click.
func valid_move(node):
for pos in all_positions():
if pos.get_meta("occupied") == false:
empty_pos = get_position2d(node.global_position)
print(empty_pos.name)
if "Red" in pos.name:
reparenting(node, $RedZone/RedBalls)
elif "Blue" in pos.name:
reparenting(node, $BlueZone/BlueBalls)
elif "Green" in pos.name:
reparenting(node, $GreenBalls)
elif "Center" in pos.name:
reparenting(node, $GreenBalls)
node.global_position = pos.global_position
#print(pos.name)
pos.set_meta("occupied", true)
empty_pos.set_meta("occupied", false)
print(empty_pos.get_meta("occupied"))
func reparenting(child, new_parent):
var old_parent = child.get_parent()
var pos = child.global_position
old_parent.remove_child(child)
new_parent.add_child(child)
child.global_position = pos
child.owner=new_parent
func get_position2d(pos):
var pos2d
for i in range(all_positions().size()):
if pos == all_positions()[i].global_position:
pos2d = all_positions()[i]
break
return pos2d
I cannot find why this position became null. I'm using position2d to place the balls which aren't move, but I have 2 platforms which rotated on mouse click 45 degrees. At first I had all positions in MainScene, but I had problems with reparent and the order of nodes(sometimes balls were bellow the platforms), so I have now 8 positions in the one platform, 8 in the other and 9 in the MainScene.
After this change all balls are and go to the correct empty(1 at a time) position2d position. But after some moves the empty position become null. How to debug this to find how this position becomes null? All positions are static and the balls spawned to these positions in _ready.