-
-
Notifications
You must be signed in to change notification settings - Fork 23.4k
Description
Godot 3.1 Stable
When you load a script into a variable and then load the script again into a new variable, those variables share the same script.
var a = load("res://a.gd")
var b = load("res://a.gd")
# Outputs true due to same shared reference
print(a == b)
However if you don't store them anywhere they get unreferenced creating a different identity for the new one.
# These have different identities unlike the first one
print(load("res://a.gd"))
print(load("res://a.gd"))
This ends up creating a situation with inner classes that end up behaving erratically depending on whether or not their parent script exists in memory.
If you store the parent scripts in variables and load the inner classes from the stored variables, everything works fine.
var a = load("res://a.gd")
var b = load("res://a.gd")
var a_inner = b.A
var b_inner = b.A
# This will output true because the parent classes still exist in memory
print(a_inner == b_inner)
However if you load the inner classes directly, you instance a new parent script every time with a different identity causing the inner classes to also have different identities.
var a_inner = load("res://a.gd").A
var b_inner = load("res://a.gd").A
# This will be false because they have different instances of parent scripts.
print(a_inner == b_inner)
I understand why this happens but it doesn't sit right with me from a semantic point of view. As far as I'm concerned as a user I've already loaded A into memory so I should be getting a reference to the same A back. If I loaded a class once and stored it, I loaded a class once and stored it. Otherwise it isn't acting as a class*** but rather just a fancy instance variable. This can also run into issues when we want to use the "instance is class" expression.
***More accurately, a script since that is what inner classes are claimed to be.