Trying to access the player position in my first game

Godot Version

godot-4

Accessing the player node to get player position

I am new to Godot and programming and don’t know how to access the player in Game.gd:

extends Node

var inventoryAmount = 0
var inventory = []
var book = preload("res://collectables/book.tscn")

func  _input(event):
	if event.is_action_pressed("ui_accept"):  
		var player = get_node("./player")   # This does not work
		print("Player is: " + str(player))   # Player is: <Object#null>	

How do I access the player? I want to find out the player’s position so that when they drop an object, I know where to put it.

Is there a different / better way to do this?

Here is the scene panel, if that is useful:

Godot-scene

Thank you!

i dont see your Game.gd attached to any of these scene’s nodes
is this autoload?

assign the player node to a group called “player”
then use this to get the player node

var player = get_tree().get_nodes_in_group("player")[0]

you can also try to get node by absolute path string
change this

to this:

var player = get_node("/root/World/player")
1 Like

Thank you, that worked!

Thank you so much, you helped me a lot!

1 Like