Godot Version
godot 4.3
Question
i’ve been trying to get the position of a node via it’s unique name (%) but it shows me an error that i suppose means the node doesn’t have a position ?
godot 4.3
i’ve been trying to get the position of a node via it’s unique name (%) but it shows me an error that i suppose means the node doesn’t have a position ?
line 6 happens before @onready
, you would have to also delay this variable with @onready
or override the _ready()
function for finer order of operations.
var player: CharacterBody2D
var player_starting_position: Vector2
func _ready() -> void:
player = %player
player_starting_position = player.position
i’ve tried to change the code so that the player position is assigned after the player variable is assigned but it stills returns the same error, also if it can help in this context the position will be used to know where the player is so that the monster (skeletton in this case) knows where to go
Aha. Unique names are sadly only useful within the scene the script is used. %player
only works for the script on your node “level”. Not for spawned enemies.
You will need a new way to find the player, groups aren’t a bad bet, you could try this once you assign your player to a group
var player: CharacterBody2D
var player_starting_position: Vector2
func _ready() -> void:
player = get_tree().get_first_node_in_group("MyPlayerGroup")
player_starting_position = player.position
that works, thank you really much !
also i did not know that groups were a thing at all so i learned something useful that’ll probably help me quite a lot so thanks again !
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.