+1 vote

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?

in Engine by (156 points)

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.


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?

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.

1 Answer

+1 vote
Best answer

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.

by (7,934 points)
selected by

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?

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.

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

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.