What does "extends Sprite" do?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Beamer159
:warning: 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

:bust_in_silhouette: Reply From: eons

It says that the script, as a class, extends from the Sprite class, meaning you can use any method from the Sprite class and up.

There should be consequences for removing or changing but is something like a bug, also is used for some hacky stuff too and it seems won’t be fixed soon.

Try to avoid touching it so you project is well organized, editor(s) can work with autocomplete and you or your team remember later what is that script for and which methods have available to use.

Is it a bug or is it working as intended? Since the node that the script is attached to is a Sprite node, it makes sense that it would have access to all Sprite functions, even if the script doesn’t extend Sprite explicitly. After all, all nodes can call their functions without a script necessarily attached to them.

Here’s the reason for the question. I have the following:

  • A Sprite node
  • A BaseScript.gd script that extends Node

I want to add a script to the Sprite node that extends the BaseScript.gd like this:

extends "res://BaseScript.gd"

Since scripts can only extend one thing, I had to remove “extends Sprite.” Is my implementation okay?

Beamer159 | 2017-01-24 16:23

Is a bug from the OOP perspective, and may get fixed one day.

Looks like the engine takes the script as an extension of the node where is attached while the script is from a parent class (up to Object I guess).


For a good design, try to avoid it, if your BaseScript is just for Sprites, will be better to extend it from Sprite instead of Node, or rethink the design.


But that “cheat” is currently allowed and useful if you know what are you doing, so, is up to de designer/developer in the end.

eons | 2017-01-24 18:54

Where is the bug? I don’t see one, it’s just that Godot doesn’t support multiple inheritance.

Zylann | 2017-01-25 13:01

Because it seems that should not be allowed (using a script extended from a parent class), -punt0 mentioned that somewhere but can’t find the comment, also said that the bug or whatever was used for some games and fixing it will break them.


And IMO, a node should not be able to use a script that is extended from a parent because it… does not make any sense if I look it with my myopic OO eyes.
I mean, you extend a Node2D with a script as a Node, I don’t understand what it is, the concept of it.

What I understand how can be used and abused though.

eons | 2017-01-25 16:36

Well, if your script only uses features of Node and is put on a Node2D, that’s fine and useful because it improves reusability.
However being able to use “implicitely downcasted” methods is indeed wrong from a strongly typed OOP design point of view.

Zylann | 2017-01-25 19:14