How to CONTINUOUSLY access property of node in a different scene?

Godot Version

Godot 4

Question

I have 3 main scenes, the player scene, the gun scene, and the test scene. In the Gun scene I have a state machine to handle different animations and states because the gun will have different states to the player. I’m trying to get the Gun to follow the player while in a state. I have the code for it that works well, I’m using preload to load the scene and creating an instance of it…BUT it only works at the start of the test scene, from then the gun does not follow the player around. I can tell the code itself to move the gun is working correctly but I somehow need to get PERMANENT and CONTINUOUS access to the player.position variable?

Before you say use add_node(), it just does not work, a new player is spawned everytime I move and the gun doesn’t even follow one.

I also tried to use onready and that just plain doesn’t work.

I thought I should somehow create the player position as a global variable but I’m not sure how to do that. Please any help will be so helpful.

Try to use an autoloads/global script for it, pass the data from each scenes to each or single global script.

hummm…I’m not sure I may be of help but it looks like problems I have sometimes…It would be easier with your code, though!
But, where did you set the position of the gun? Sometimes, I would initialize the position of a node in the _ready() function and forget to update it in the _process() for example.
If it’s not your problem : you can either do as already suggested : use an autoload script where you put your player’s position OR grap a reference to the Player’s node (with get_node() )in your gun’s script and get the player.position property from here!
But maybe, it would be easier to help with your code under the eyes!
Cheers!

Thanks I really appreciate the help! I realised for my specific case I was being pretty dumb, as I could just attach the movement script to the actual gun, and the let the state machine handle animations and transitions :slight_smile: so I just did that and it works well!

1 Like