Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Beamer159 | |
Old Version | Published before Godot 3 was released. |
When attaching a script to a Sprite node, the script includes the line “extends Sprite” at the top. It seems like I can delete this line without any consequences. What is the purpose of this line?
Hi!
Basically you tell the script that the code inside is for a Sprite type node and not an AudioSampler Node
Check this link, where it says Classes.
Here is explained much better, since what I say is just a very basic idea, so that it is understood.
http://docs.godotengine.org/en/stable/reference/gdscript.html
Aquiles | 2017-01-24 15:54
I did a quick test. I created a Sprite node, added a script to it, and used the Sprite function set_flip_v(true). The code looks like this:
extends Sprite
func _ready():
set_flip_v(true) # A sprite-specific function
I then changed the extends line to “extends AudioStream” which gave me an error, which I expected. Then, I changed the extends line to “extends CanvasItem” which had no error and ran just fine. Deleting the extends line also ran fine. What I don’t understand is why I can change the extends line to CanvasItem, which does not have the set_flip_v(true) function in it. Also, why am I allowed to delete the line altogether with no repercussions?
Beamer159 | 2017-01-24 16:39
The classes in Godot are inherited from other classes with affinity, CanvasItem has affinity with a Sprite, but not a SamplePlayer, since it is an audio inherit different classes, it may work well without that first line, but eventually when your code is more Extensive and with more functions, you can throw errors since it is not defined “The context of the Node”
For the moment, it is recommended to leave that line and not erase it. I hope it helps.
Aquiles | 2017-01-24 17:21