Variables in other files?

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

Hey there!
I’m used to working in JS for like discord bots ext. and I’d always be exporting functions and variables to make the code cleaner, and easier to read. and of course, exporting functions in scripts would be very pointless. More of an annoyance than anything, but variables on the other hand especially onready or even export variables take up a lot of space. They are a sort of annoyance, so I was wondering if there’s anything GDScript or Godot as a whole for exporting variables.

If you can help big thanks!

:bust_in_silhouette: Reply From: Ninfur

Global variables can be stored in a singleton script, which can be accessed from anywhere.

If you want to separate some reusable variables to a different script, you can give that script a class_name and create instances of said class in your other script(s). This could look something like:

entity_data.gd

class_name EntityData

var velocity
var health

func _init(health):
	self.health = health

entity.gd

var data

func _ready():
	data = EntityData.new(100)
	data.velocity = Vector3.ZERO
	print(data.health) # 100