Is there a way to copy a nodes global_position without referencing it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By danielmeridew

I’ve been back to this issue several times now…

I hold a reference to the player in singleton Global.player.

There are times where I want to take the player’s global_position as a value so I can use the player’s previous position later e.g. for an air strike, or enemy charging movement.

There is no .duplicate() for global_position, there must be a simple one-liner for this kind of thing?

Any help appreciated.

تأسيس شركة في دبي

tasyeir | 2023-06-19 00:02

:bust_in_silhouette: Reply From: xdrenal

You could store it in a variable and then use it later, for example:

var playerPos = global.player.get_global_position()

if you want to store it once then just create a function that has that line of code. It should work!

:bust_in_silhouette: Reply From: HyperlinkYourHeart

Not sure what was unclear in the answer and comments to your previous question, but if you assign a vector to another variable, that “copies” it. If you really want to be explicit about it you can pass a vector to one of the vector constructors:

var player_current_position = Vector2(Global.player.global_position)