I’ve been trying to use variables and information from one script to another. I have checked the forums and they all tell me to do generally the same thing yet when I try it, it doesn’t work. I’ll put an example of what my code looks like below
script.gd:
var text = "text"
main.gd:
@onready var script = $script
var text = script.text
Im trying to access the variable ‘text’ across different scripts yet nothing I do recognizes it.
Created a script called global and set it to autoload.
var score = 0
var player_current_attack = false
var last_position = Vector2(0, 0)
var wizard_fuelrod = 1
var grave_fuelrod = 1
var gnome_fuelrod = 1
var gnome_convos = 0
var brazier_fuelrod = 1
I can then access any of the variables in my other scripts:
if global.wizard_fuelrod == 1:
global.score += 1
global.wizard_fuelrod = 0
just put at the top of the other script class_name somelass
Example: utils.gd in same folder as your main script
class_name Utils
var score = 0
var player_current_attack = false
var last_position = Vector2(0, 0)
var wizard_fuelrod = 1
var grave_fuelrod = 1
var gnome_fuelrod = 1
var gnome_convos = 0
var brazier_fuelrod = 1
func somefunc(d: Variant):
print(d)
Then to access it like this:
var xx:=Utils.new()
xx.gnome_convos += 1
xx.player_current_attack = true
xx.somefunc(xx.gnome_convos)
@nickl, if you want the variables to be constant, as in of you change the variable in one script any other references to that variable are updated, use a static variable, else use a normal variable like @wyattbiker said. Both require a custom class.