How do I access and set script variable from a different script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By whoisda


I’m building a thing where I’m using the great lerp camera by kidscancode. I thought I’d use the same camera for different objects.

I’m trying to create “an entering a vehicle” mechanic where when the player presses the action button the camera switches over to the vehicle and the control set for that vehicle is activated at the same time deactivating the player objects control.

I have figured out switching the camera is a rudimentory way:

func get_input():
if Input.is_action_pressed("action_A"):
	target_path = get_node("/root/World/PolyPlane")
	target = target_path

I would now like to set the input for PolyPlayer inactive and Polyplane active. Since this is the camera’s script, I am unable to get and set the script variables I’ve created for the PolyPlane and PolyPlayer which I want to toggle.

Also, I want the camera to read and set the offset variables from those two objects (Which I will set individually in their scripts) when switched so this is also something I hope can be accomplished in some similar way.

Script Variable

Camera Switching

:bust_in_silhouette: Reply From: timothybrentwood

Kids can code also has a great article on node communication that is very well worth the read:

Be sure to check out the diagram linked in the info box at the top!

I would think that since you’re communicating between siblings you would emit a signal. You may also consider creating an auto-load singleton that handles your input gathering that other nodes can query to see the current input status.

Thanks for the reply. I did come across other resources that point to how to identify and get node names and variables.

I will look into auto-load singleton though I’m very new at this and all of what you said is difficult to understand.

whoisda | 2021-05-09 06:16