• Godot HelpProgramming
  • use a variable from one script in another script - usar una variable de un Script en otro script

In English: Hello I have a question, I want to use a variable that I have in a Script in a separate script to make decisions based on that variable. Please Help

En español:Hola tengo una duda, quiero usar una variable que tengo en un Script, en otro script aparte para tomar decisiones en base a esa variable. Por favor ayuda.

In GDScript, you can access variables in other scripts if they are attached to nodes by getting the node, and then directly accessing the variable. For example, if you have the following scene tree:

  • Root_Node (has script root.gd)
  • * Child_Node (has script child.gd)

root.gd script:

extends Node
func _ready():
	var child = get_node("Child_Node")
	child.counter += 1
	print ("child node counter is: ", child.counter)

child.gd script:

extends Node
var counter = 0

You just need to have a reference to the node with the script, and then you can access any of the variables and functions in GDScript. Hopefully this helps a bit :smile:

@TwistedTwigleg said: In GDScript, you can access variables in other scripts if they are attached to nodes by getting the node, and then directly accessing the variable. For example, if you have the following scene tree:

  • Root_Node (has script root.gd)
  • * Child_Node (has script child.gd)

root.gd script:

extends Node
func _ready():
	var child = get_node("Child_Node")
	child.counter += 1
	print ("child node counter is: ", child.counter)

child.gd script:

extends Node
var counter = 0

You just need to have a reference to the node with the script, and then you can access any of the variables and functions in GDScript. Hopefully this helps a bit :smile:

Thanks

3 years later