The paths are different when using absolute paths and relative paths! (The mySprite node uses a relative path). This causes the script object instances to be different. If I change it to both use relative paths then the script object becomes the same instance. I believe this is a bug which could be causing unnecessary loading of resources multiple times.
The above will print True. The important thing is to use “res://path/to/MySprite.gd” as using “/path/to/MySprite.gd” results in the path being resolved to “res:///path/to/MySprite.gd”.
I don’t know if it is a bug or not, but it seems best practice is to use “res://path/to/thing” style paths for everything in GDScript.
This is because it looks like others have also had problems with non res:// style paths:
Also there are not so many problems with relative paths, I’m using them all the time. One of the issues you had linked is more related to the fact that relative paths in Godot should not be started with “/” character.
kubecz3k | 2016-04-11 14:48
Yes, I guess relative paths most of the time should be ok (except for issue 4026 above). In any case that should be fixed eventually.
chanon | 2016-04-11 14:54
Note: In Godot 3.0 and later, the extends keyword for the purposes of type comparisons has been replaced with is:
if get_node("MyNode") is KinematicBody2D:
print("MyNode inherits from KinematicBody2D.")
For comparisons I would use extends keyword instead, in that case you don’t even need to use get_script() at all, just:
const MySpriteClass = preload("res://path/to/MySprite.gd")
var sprNode = get_node("mySprite")
(...)
var nodeExtendingSpriteClass = sprNode extends MySpriteClass; #true/false
print("spr node is an instance of MySpriteClass: ", str(nodeExtendingSpriteClass))
If you really need to get exactly “Class” of a scipt then I don’t know if there is any direct method to do this… But you still can get path to the script file with: var scriptFilePath = node.get_script().get_path()
So if a name of a file is a name of a class then it would be: var scriptName = node.get_script().get_path().get_file()
Yes, actually I just figured that out while fixing my code and came to update my answer.
I had the right idea right at the beginning … it was just due to the absolute path resolution bug.
chanon | 2016-04-11 14:49
If you think this answer is better you should choose it, this way any person that will come in the future will be able to benefit.
kubecz3k | 2016-04-11 15:01
Sorry, I edited my answer already … I’ll choose yours anyway
chanon | 2016-04-11 15:09
Note: In Godot 3.0 and later, the extends keyword for the purposes of type comparisons has been replaced with is:
if get_node("MyNode") is KinematicBody2D:
print("MyNode inherits from KinematicBody2D.")