Problem getting player position from another scene

Godot Version

4.2.1 Stable

Question

The problem is that I need to get the global position of the Player in order to optimize the light and turn it off when the player is far away. However, I use the light scene separately and the main level separately, I’m new and I’m at a dead end because I don’t know how to get the global position of the player from another scene

The way I keep track of the player is like this:

Create a global script:

class_name Global extends Node

static var player = null

Then when instantiating the player or spawning it, set Global.player

In the player.gd:

func _on_enter_tree():
  Global.player = self

Or in a spawner:

func spawn_player():
  var player = player_scene.instantiate()
  player.global_position ... etc rest of your spawn code...
  Global.player = player

Then you can access it in any other script like this:

Global.player

And get .global_position or whatever you need.

1 Like

Oh my God. I didn’t expect such a detailed answer. Thanks a lot!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.