Accessing data from another script

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

So I have one script:

extends Node

class Stuff:
    var text
    var size

    func _init(t, s):
        text = t
        size = s

In another script, I access it with:

extends Node

onready var stuff = load("res://stuff.gd")

func _ready():
    var inst = stuff.Stuff.new("fred", 100)
    print(inst.text)
    print(inst.size)

Is there any way to access Stuff without using stuff.Stuff? It just seems so clumsy!

:bust_in_silhouette: Reply From: bitwes
onready var Stuff = load("res://stuff.gd").Stuff
func _ready():
  var instance = Stuff.new()

“best practice” is to name your variables that are references to classes using PascalCase.

:bust_in_silhouette: Reply From: Zylann

Alternatively, you can also change your stuff.gd to not extend anything:

stuff.gd

var text
var size

func _init(t, s):
    text = t
    size = s

Then you can access it like that:

extends Node

const Stuff = preload("res://stuff.gd")

func _ready():
    var inst = Stuff.new("fred", 100)

If you prefer to use subclasses with the class keyword, you can do so, and then append .Stuff after the preload like bitwes did.