Step-by-step turorial

Godot Version

4.2

Question

is anyone able to provide a brief comment on a paragraph from creating your first script section in the step by step turorial?

Every GDScript file is implicitly a class. The extends keyword defines the class
this script inherits or extends. In this case, it’s Sprite2D , meaning our script will get access to all the properties and functions of the Sprite2D node, including classes it extends, like Node2D , CanvasItem , and Node.

what does it mean to be implicitly a class?
what does it mean to extend?
whats the difference between inherit and extend?

thanks,

  • duo
1 Like

In many other languages, you create a class by explicitly defining it. For example, in C++ you might have one header file which defines multiple related classes. In Godot, when you create a script file with the .gd extension, that file defines a class. If you write class_name MyClass then it’s name will be MyClass, but it doesn’t need a name to function. If you write extends Node or extends Sprite2D then it will inherit from that class (yes, extending and inheriting are the same). If you don’t write extends Something however, your class is still a class, and in fact it will automatically inherit from RefCounted.

Basically, that paragraph is saying that if you create a GDScript, you have created a class.

Inheritance is a subject with a lot to it. I think it might be better for me to link you to a tutorial explaining inheritance in Godot.

1 Like

Just as the tutorial says, when you create any .gd files they are actually a class, but implicit, your project don’t know how to get those unless you specified the file location.

But by the moment you do this

class_name FoeSpawner extends Node

^ Now that class become explicit, and your whole project now knows that a class with name FoeSpawner exists

In the realm of Godot, extending and inheriting are practically the same. So when you extends,
image
The built-in Docs, will see it as inheritance
image

1 Like

thanks, i will check out this video later!

The docs in editor update with new custom classes you create?

maybe? that makes sense. im still new to this though.

thats what i got from the docs. thanks for clarifying!

image

image

2 Likes

Yes it does work on your custom classes. It will list all the members and functions of that custom class, as well as the description that you write. To do that, you prepend it above the declaration with a comment with double hash ##

## Class that spawns enemy to the world
class_name FoeSpawner extends Node

And the parent class will also knows what classes are inheriting from it

1 Like

I was meaning when you look at the parent class like in @Yes answer, I really didn’t noticed that happens, but your examples demonstrate it as well, thanks!

kinda. i am more concerned with how each gdscript is a class. like how does that work? i get you can extend said file/class. is that the only benefit of having a file essentially be a class? asking because im a noob

In GDScript, almost every class can be instantiated by using new(). Whatever it is, if you can new() on it, then it is a class.

Here is a demonstration to make it clear

  • Go make a file called “new_script.gd” the content can be any valid gdscript syntax.
  • At this point, that class ^ is undiscoverable. It is there, but your project can’t reach it.
  • So now on your main scene, write something like this
class_name Main extends Node

func _ready() -> void:
	var script := load("res://new_script.gd") # Point it to your script
	var inst = script.new() # Look! each gdscript file is a class, so you can do this
	print(inst) # <RefCounted#-9223372009289808686>

Hope it clears

Each Script is a class as in they can hold data, let it be variables, and they can also run code trough functions.

They can also be instantiated and created with var my_script = Script.new() as any object in any OOP, as @Yes said.

I mean you ought to have code written and run somewhere. :thinking:
And extending/inheriting properties is already good.

By them being classes already, you needn’t specify any class_name. If you don’t specify them with a new class_name, you cannot instantiate them by doing, let’s say, CharacterBody2D.new(), as previously mentioned.

They also become instantiated the time you attach the script to a Node, as long as they’re compatible. You may not attach a Script that extends from 3D to a Node2D.

You can run code by calling on a Node as long as the Script that is attached to it has the function you’re trying to call. No need for them to be a new_class.

Let’s say a Node called "Entity" has a Script that extends from CharacterBody2D like this. Notice I haven’t written class_name anywhere:

extends CharacterBody2D

func move_towards( this_direction :Vector2 ):
       print( "Look mom, I'm moving!" )
       pass

You can just get a reference to the Node and call a function without instantiating the Script (because as I said, they become instantiated the moment you drag and drop them into a Node).

@export var character := get_node( "Entity" )

func _ready() -> void:
      character.move_towards( any_vector2 )
      pass

Hope it helps!

1 Like