+1 vote

Let's say I build a custom button and have a script I've written that extends the button class, but includes several additional functions. I'd like to make several instances of this custom button class with their own unique scripts that extend the custom button script. Is there a way to tie the custom button script behind the scenes to the custom button scene? I want to have access to all the custom button script's features when a new scene is instanced without a script, and have a newly added script automatically extend the custom button script when added. Essentially, I want to build my own node. Can this be done?

Godot version 3.4.2
in Engine by (93 points)

1 Answer

+1 vote
Best answer
class_name

use this keyword right after extendsline, followed by your chosen name. From this moment on You will be able to see Your class among other nodes in editor, it will also be autocompleted when typing in editor. When You drag it into viewport or create it on run by code with .new(), it will basically be the node You extended it from with all your additional code.
If you modified exported buttons properties in editor and saved it as a scene, these changes will not be remembered by class script. Still You can just add your class script to the modifed button scene and save it again. From this moment on your other custom button classes can inherit from whole scene.

by (8,097 points)
selected by

Thank you so much!!!

When I do this and create a new node of the class, it automatically creates the node with the script that I have attached to the base scene and doesn't automatically let me create a new node extending the new node type. Am I doing something wrong?

When you choose your custom node from node list and add it to the tree, it should look scriptless, script is behind the scenes, and adding new script should automatically extend your custom class. Now You can use classname once more to create subclass, and so on

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.