user18513298
user18513298

Reputation:

getting position of a node in gdscript

hi I'm trying to make a frog that jumps when the player gets close I tried this

onready var playerpos = get_parent().get_node("player").position

And

onready var playerpos = get_parent().get_node("player").global_position

and get this error

Invaled get index 'position' (on base: 'null instance')

scene

Upvotes: 0

Views: 25357

Answers (2)

endaye
endaye

Reputation: 41

According to GDDoc below: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#literals

You can use the dollar sign to get node:

onready var player = $player
onready var player_pos = player.position
onready var player_global_pos = player.global_position

Or, you can get the player's position directly:

onready var player_pos = $player.position
onready var player_global_pos = $player.global_position

Upvotes: 0

Hole
Hole

Reputation: 46

Null instance means that the object you are trying to get, doesn't exist. And that is, because you are asking for its position, before it even enters the scene.

However, what you are trying to do, is completely unnecessary. You already have a main scene, so that should be able to access all of its nodes global position, without having to call the entire object.

Access your player's position from the main scene, not the frog scene.

That is, if you want the players position. What you want to do, however, does not require that.

You need to have an area node attached to the frog, that detects and sends a signal, when it detects a physics object. After which you simply check, whether the physics object is player or not, and execute the appropriate action.

Upvotes: 1

Related Questions