Help aligning dungeons using while loop

Godot Version

4.3 stable

Question

I’m aligning dungeons and I can’t get this while loop to work, I do it by moving the dungeons until two points are aligned, and the game doesn’t show that they are moving, and I don’t know what’s happening, can you help me? (here’s the code):

while Room.get_node("Exit_Entreance/Entreance").global_position.y != pre_Room.get_node("Exit_Entreance/Exit").global_position.y: align() if Room.get_node("Exit_Entreance/Entreance").global_position.y == pre_Room.get_node("Exit_Entreance/Exit").global_position.y: break

func align() -> void: if Room.get_node("Exit_Entreance/Entreance").global_position.y < pre_Room.get_node("Exit_Entreance/Exit").global_position.y: print("Down") Room.position.y += 1 print(Room.get_node("Exit_Entreance/Entreance").global_position.y) if Room.get_node("Exit_Entreance/Entreance").global_position.y > pre_Room.get_node("Exit_Entreance/Exit").global_position.y: print("UP") Room.position.y -= 1 print(Room.get_node("Exit_Entreance/Entreance").global_position.y) if Room.get_node("Exit_Entreance/Entreance").global_position.y == pre_Room.get_node("Exit_Entreance/Exit").global_position.y: print("Aligned")

Please don’t use screenshots to show your code, use three ` at the beginning and the end of code
It’s almost impossible to read and point to code from a photo, especially when you’re using this font, I highly recommend using a more standard font like Jet Brains Mono so you don’t get dizzy while coding!

1 Like

To use markdown syntax to format your code you should use triple quotes:

```
your code here
```

What does align do?

If you believe align should make the global_position.ys equal, you won’t need the if and break because it’s the while’s condition.

1 Like

The problem is that if I remove the if for the break, the dungeons still don’t line up properly.

Can you copy your code back into the post? One of the common bugs in the forum is that lines merge after switching to code mode…

in photo or in text?

In text

1 Like

You changes Room.position.y in align but you compares something’s global_position.y?

I’m not sure what you are actually doing but that might not work as expected.

Maybe show us what have been printed?

I am comparing the y position of the exit point of the previous room to the y position of the entry point of the room that is being loaded

I think this code is much better and of course cleaner, try this:

@onready var entreance = Room.get_node("Exit_Entreance/Entreance") 
@onready var exit = pre_Room.get_node("Exit_Entreance/Exit") 

func _ready():
  while entreance.global_position.y != exit.global_position.y:
    align()

func align() -> void:
  if entreance.global_position.y < exit.global_position.y:
    print("Down")
    Room.position.y += 1 
    print(entreance.global_position.y)
  if entreance.global_position.y > exit.global_position.y:
    print("UP")
    Room.position.y -= 1 
    print(entreance.global_position.y)
  if entreance.global_position.y == exit.global_position.y:
    print("Aligned")

it crashes, maybe i can use the for loop with the distance from the entrance and the exit?

That’s it, it doesn’t crash anymore using while, thanks for the help anyway

I don’t think you need a while loop here at all. If I may suggest, I think this piece of code will do exactly the same as your whole while loop and align() function.

func _ready():
  var difference: float = exit.global_position.y - entreance.global_position.y
  Room.position.y += difference