system
1
|
|
|
|
Attention |
Topic was automatically imported from the old Question2Answer platform. |
|
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!
system
2
|
|
|
|
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.
system
3
|
|
|
|
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.