Get Sprite X and Y using get_pos()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider
:warning: Old Version Published before Godot 3 was released.

I’m wanting to get the X and Y coordinates of the sprite I have jumping around relative to the viewport. How would this be accomplished?

:bust_in_silhouette: Reply From: Shin-NiL

Did you try get_global_pos()?

onready var player = get_node("MegaMan")
onready var pos = player.get_global_pos()
    
func _process(delta)
debug_xpos.set_text(str(pos.x))
debug_ypos.set_text(str(pos.y))

The above returns the starting positions, but is doesn’t change as I move about the stage.

9BitStrider | 2017-08-12 18:34

That’s because you’re only defining your position once on _ready. You need to re-define pos before every time you want to check it.
The simple answer is, to take your var pos out of global scope and place it as a local var inside _process:

func _process( delta ):
  var pos = player.get_global_pos()
  debug_xpos.set_text( str( pos.x ) )
  ...

Though, if you need to use this pos outside of _process, you can also define the var as a global member, but still update it in _process():

var pos = Vector2()
    
func _process( delta ):
  pos = get_global_pos()

YeOldeDM | 2017-08-12 23:02

:bust_in_silhouette: Reply From: Mrunalraj
onready var sprite = get_node("sprite_name")
onready var pos = gun.get_position()
#pos contains position in Vector format