Access variable from another node

Godot Version

newest

Question

how do i get a variable from one node to use it in another? i have a variable called aim_position in a raycast3D node in my player scene, i need the variable in my main scene

func _ready() -> void:
    print("help me pretty please")

Get a reference to the node using get_node(node_path) or $node_path and then use the dot syntax to access its property/variable.

You’ll need to supply the valid path from the node that’s running the script to the target node. Node paths use pretty much the same logic and syntax as file/folder paths on your hard drive. More about node paths: NodePath — Godot Engine (4.5) documentation in English

what would that look like?

how exactly would i do that? i have this

extends MeshInstance3D

func _ready():
var aim_point = get_node(“$Head/RayCast3D.aim_point.aim_point”)

func _on_timer_timeout():
print(aim_point)

and it prints an error

We’ll need to see the screenshot of your scene tree and where are the two relevant nodes in it.

the raycast in the controller scene is where the variable i want is

and meshinstance3D2 is where i want the variable in the main scene

Show the main scene as well.

sorry i was gonna upload it but it said i couldn’t =/

Why would a mesh instance need something from a raycast?

for shooting a bullet, the raycast takes the coords and the bullet goes to them. thats my best idea on how to make a bullet, im not that good at godot so i dont know anything better =/. if you have a better ideal tell me.

What is MeshInstance3D2 representing? You should rename it adequately.

its the bullet

The bullet shouldn’t be in the scene from the start. Instead the player controller should instantiate the bullet when fire is pressed.

You don’t really need a raycast for this.

I’d suggest stepping back from this for a moment and going through introductory tutorials in Godot’s docs to familiarize yourself with basics of how Godot works:

that sounds good. thank you

1 Like