How to get the player's position onto another script in a different scene

Godot Version

4.2.2

Question

I am somewhat new to Godot and coding in general so i am quite puzzled by this issue. My game has a bullet that you can fire which I want the player to be able to teleport to. To be able to teleport, I need both the position of the player and the bullet. How might I get the players position onto the bullet’s script. Extra info: to teleport I want the player to click on the bullet to teleport, this is a top down shooter

Hi.
This error on your screenshot is caused by case sensitivity i guess (Player != player)

Is this already the answer?

1 Like

Which screenshot is that on?

In the upper right one.

You can define a class called “Globals” and put it in auto load.

With the click handler on the bullet, the bullet then updates Globals.player_pos.

The player_pos has a setter function, that emits a player_position_changed signal, which is what the player listens to and updates it’s position if it happens.

At least that’s how I would do it I suppose.

1 Like

Thats how I would probably do it.

You can make a new script called “Globals” or “Global.”
Screenshot 2025-02-05 154406

Then you can go to the project settings, globals, and add that script into the Autoload.

Then you can simply call the variables, by calling the script name then variable you want

That works with integer values independent from other nodes but I need the position of player or dagger to be in the global script. I do have a global script but I can’t get the global position of nodes on there

1 Like

ah, I didn’t see that when I looked through your screenshots.

Another method would be to use signals that carry the positional data instead.

Suppose you have a signal that sends out when you press the ‘swap’ action, that signal can then be sent to the bullet scene containing some arguements, such as the players position.

I myself, am not too well versed in custom signals, so I recomend looking into the docs as well. Good Luck, feel free to ask anymore questions.

Edit:
Through my own testing as well, I made a mock up (as I dont have you full files) of my own player and Global script, and I was able to hold the player position in the Global script. Am I perhaps understanding you wrong?

This probably could work if i was good with signals but I’m sadly not. Could you send screenshots of your testing?

note: Thank you for all the help so far. I really appreciate it