This is a bit complicated and I am not sure if I get it.
newempty is an inside function variable. Every time You run valid_move function You introduce this variable as null, than iterate through all positions and newempty is set during this iteration. So the reason nothing happens after click and newempty is still null must be that all of Your positions meta become “ocupied” at some point.
The problem propably lies in line : set_meta(“occupied”, false) . Try to print it.
Other than that, maybe there is misconception in movement. What is the code for Input of click ? Are all balls moved_valid() in the same time ?
I replaced new_empty with empty_pos which I declared it outside the function. It didn’t make any difference though. I also tried to print the empty_pos.name and the “occupied” meta after set it to false.
Those don’t help because I get the correct positions and “occupied” value(false) until the empty_pos to become null. Then I get error for trying to set_meta in a nil instance(because empty_pos became null). Seems that something happens with the positioning but I don’t see any wrong positioning visually, until I get the null crash.
The on click code has nothing except the valid_move() method.
The balls don’t move together. Every time the clicked ball moves.
Here is the input function:
for child in all_balls():
child.connect("input_event", self, "_on_Ball_input_event", [child] )
func _on_Ball_input_event(viewport, event, shape_idx, node):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and not event.is_echo() and event.is_pressed():
dancaer69 | 2021-04-09 06:27
After some more debuging I found that the problem starts in line which I set the empty_pos. There I use a function to get the Position2D on that node’s position. So I added a print statement in this function and I found that in some point(when I get the error) the node’ s position doesn’t match with any of the positions. I also printed the position which doesn’t match.
I don’t know if this is accurate, but I get that position and from the editor on mainScene I added a sprite(just for test) and I give it the non matching position. I found that the position isn’t the same with the Position2d position. I checked 2 of them and the first was 4 pixels right from the Position2d and the second was about 2 pixels right and up
So, seems that at some point the ball’ s global_position isn’t the same with the position2d in which is moved. But, because I print the positions also, until then the old and new position are exactly the same.
Seems that the problem with shifting positions comes from the circular platforms when rotating. I have set the center property on but again the rotation isn’t pefect and because the position2d are children of them after some rotations the positions of them change a bit.
The problem is that I don’t know how to get perfect rotations. I tried to correct the textures and reselect the areas from atlas picture and it’s better now but still not perfect rotation, so the errors occure still.
dancaer69 | 2021-04-09 11:50
After many other things I tried and couldn’t get get rid of the error, I thought that maybe this was to complicated and need to find a simpler way.
So, Instead of use the Position2D positions, I created a sprite with a transparent texture and use this as the empty spot.
Then I just swap the clicked ball position with the empty(transparent) sprite position:
var nodepos = node.global_position
var node_parent = node.get_parent()
var hole_parent = hole.get_parent()
node.global_position = hole.global_position
hole.global_position = nodepos
And this works fine. Now just need to find a way to actually define the valid moves.
dancaer69 | 2021-04-10 12:05