How do I set the position of my player to an different node?

Godot Version

4.2.2

Question

Hello Godot Community!

I am trying to program a feature into my game where the player can switch positions with the bullet that they fired. I have my player’s position set in a global script and now all i need to do is to find a way to change its position.

P.S. I asked a question previously about a similar topic and this is the same game

This really depends on how this feature is supposed to work (is the bullet somehow selected, is there always one bullet and it’s triggered by a key press, …).

In general it always boils down to having a reference to the player and the bullet and then update their position. You could have some parent node manage the switching, you could have the player keeping track of the bullets and implement the switching there, you could have the bullet inform the player/set the position (e.g., when selected), …

The system I was going for was for the player to click on the bullet for the swap to initiate, so there may be many bullets on the screen. I’m somewhat new to coding, so how would I have the player keep track of the bullets positions and same for the bullet?

How did you define the player’s position in the global? Vector3s are copied meaning if you did something like this, then it will only store the player’s starting position.

# in global
var player_position

# in player script
func _ready() -> void:
    Global.player_position = position

You will have to store the player reference instead, where you can access and modify the position freely

# in global
var player

# in player script
func _ready() -> void:
    Global.player = self

# in bullet script
func swap() -> void:
    var temporary := Global.player.position
    Global.player.position = position
    position = temporary