#Global_Position - Unable To Update Global_Position of the Player Object

9 messages · Page 1 of 1 (latest)

smoky thistle
#

I created a small programmatic in-game debugger so I could see the player position. However whenever I move the player character on the screen the Velocity updates but the position X and Y remains the default position of the object when I start the scene. Is there a way get the current position of the player object using the onready var functionality? I believe I have provide the relevant data for my player script. Am I missing something about the how the onready var system works?

Player.gd

extends KinematicBody2D

onready var player = $"."
onready var playerPosition = player.position
var velocity = Vector2.ZERO

# Lots of code in-between this function and others
# Lots of code in-between this function and others
# Lots of code in-between this function and others

func UpdateLabel():
    var velocityX = velocity.x
    var velocityY = velocity.y
    var positionX = playerPosition.x
    var positionY = playerPosition.y
    PlayerDataLabel.text = str(
        "Velocity X: ",
        stepify(velocityX, 0.01),
        "\nVelocity Y: ",
        stepify(velocityY, 0.01),
        "\nPosition X: ",
        stepify(positionX, 0.01),
        "\nPosition Y: ",
        stepify(positionY, 0.01)
    )


sharp bane
#

Firstly, what's the point of the player variable?

#

It seems to just be getting the current node, which is unnecessary, as that's the node you're already running as. player.position == position

#

Secondly, the problem you are having is because you are creating a playerPosition variable, but vectors are not passed by reference. That is to say, playerPosition is a copy of the position of the player, and changing it will not change the original, and vice versa.

smoky thistle
sharp bane
#

You can just use position

#

It's a vector2 That exists on all 2d nodes, holding their current position

smoky thistle
#

I see. I think that solves my issue.

func UpdateLabel():
    var velocityX = velocity.x
    var velocityY = velocity.y
    var positionX = $".".position.x
    var positionY = $".".position.y
    PlayerDataLabel.text = str(
        "Velocity X: ",
        stepify(velocityX, 0.01),
        "\nVelocity Y: ",
        stepify(velocityY, 0.01),
        "\nPosition X: ",
        stepify(positionX, 0.01),
        "\nPosition Y: ",
        stepify(positionY, 0.01)
    )
sharp bane
#

You could do that, yes. You could remove the $".". part, as that is doing nothing, though.