Change the position of a Node compared to another

Godot Version

4.22

Question

Hey there,

I’m quite new to using Godot and wanted to share a problem and my solution with you in the hope of getting feedback so I can learn from it.

Take the following root structure in mind:

  • game
    • pausable
      • pinklin(player)
    • pause_menu

When I display the pause_menu I want it display itself at the player’s posistion.
I ended doing the following:
When the pause menu is triggerd (pressing esc) I’m sending the signal ‘gamePaused’ from the pause_menu to pinklin. In pinklin there is now a func _on_pause_menu_game_paused. In there I get the player position and send a new signal ‘playerPos’ with the posistion added as a vector assigned to a variable player_position. To the pause_menu. Where in turn I get the new func _on_pinklin_player_pos wich sets the position of the pause_menu to the player_position.

In code:
pause_menu script:

signal gamePaused

func _input(event):
	if event.is_action_pressed("pause"):
		gamePaused.emit()

func _on_pinklin_player_pos(player_position):
	var pos = Vector2(player_position)
	pos.y -= 35
	self.position = pos

pinklin script:

signal playerPos

func _on_pause_menu_game_paused():
	var player_position: Vector2 = self.get_global_position()
	playerPos.emit(player_position)

Im very eager to hear what you guys think about how I handeld this and what can be done better!

Hi, if you put the pause menu on a canvaslayer it will appear at the same spot on the screen no matter where the player is in the world, so you will not have to change the position of the pause menu.

1 Like

Thank you so much for you response!

I have been thinking way to difficult about it.

I just read a bit into the canvaslayer and it looks exactly like the thing I need.
the pause_menu is a instanced scene. Would you suggest making the canvas_layer the root of the pause_menu or just add it when needed?

You can do both at the same time, but I would just have to pause menu always loaded but hidden when the game is not paused.

1 Like

OMG…

It is so much easier. To use the canvas. I don’t have to fiddle with the scale anymore. You are a life saver!

Thank you again so much for your time and response.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.