0 votes

I wanted to create my own "unit" object based on KinematicBody2D, and then use it to make other objects that will inherit from the "unit".
enter image description here
BUT, when I add a "unit" node to the scene, it already contains a script.
enter image description here
What's the point of plugins then?
Doesn't that violate the whole concept?
There is a "Node", "Node2d" is inherited from it, "KinematicBody2D" is inherited from "Node2d". So why does the created plugin lose the meaning of inheriting one node from another?

Godot version 3.2.3
in Engine by (41 points)

1 Answer

+1 vote
Best answer

It doesn't violate the concept, and you can still inherit from the custom node script instead of changing it directly or replacing it: Just right-click on the newly added Unit and select "extend script". The newly created script will start with something like

extends "res://Unit.gd"

and you will be able to use all functions of Unit.gd plus whatever you add in your extension.

by (1,742 points)
selected by

Hmm, a little confusing. The main thing is that it works.
Okay, thanks a lot, tovarishch!

One more question.
I want to make several child plugins on the basis of my "unit" plugin, for example "player" and "enemy". How do I add child classes to the hierarchy so that they appear as inherited from the "unit" class?
enter image description here

Of course, I can leave them in the "kinematicbody2d" branch, but I would like it to be beautiful

func _enter_tree():
add_custom_type("Player1",
    "KinematicBody2D",
    preload("res://addons/player1/player1.gd"),
    preload("res://addons/unit/ico_unit.png"))

I never tried it, but shouldn't it be enough to assign "Unit" as base node, like so?

func _enter_tree():
    add_custom_type("Player1",
        "Unit",
        preload("res://addons/player1/player1.gd"),
        preload("res://addons/unit/ico_unit.png"))

Alas, it doesn't work. I tried to prescribe different options, but nothing helps((

Ok, I played around a bit and finally got it to work with the following approach:

Do not use addcustomtype, but register your custom scripts as classes instead, with

class_name Unit, "unit_icon.png"

and

extends Unit
class_name Player, "player_icon.png"

There're some other disadvantages with this approach (described in https://github.com/godotengine/godot-proposals/issues/1876), but at least the hierarchical display in the node selection works as expected.

The good news is: The developers are well aware of the mess they created with the two different approaches, and @willnationsdev is already working on a redesign for 4.0: https://github.com/godotengine/godot-proposals/issues/22

Thank you very much! I will try!

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 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 webmaster@godotengine.org with your username.