How to extend custom classes in Godot?

Say you want to create a generic class Animal that extends Node2D (or any other basic Godot class), with generic methods and property, and then a bunch of Nodes that extend that custom class. How do you do that?

The answer is easy, but the syntax is weird.

First create your Animal class in a file like this:

res://animal.gd
extends Node2D  # Note that we are not naming the class

var size   = 0
var mammal = False

func _ready():
    # do whatever you want...

Next, add to the Node that will extend Animal a new script like this and you’re done:

res://myscript.gd
extends 'res://animal.gd' # Weird, right?

# And now do what you want...

Okay, not gonna lie, I’m kinda grossed out by this syntax which mixes up class inheritance and import/include mechanisms. But, hey, it works as expected so I will refrain to complain too much. :]

Hope that helped!

As always for comments, suggestions or insults, use my twitter account.


Written by Lertsenem in godot on Wed 25 January 2017. Tags: godot, gamedev, technical, short,