Any way to inherit boilerplate code in all my UI controls using just one script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By squished-01

I have about 50 lines of code that I want every UI control in my game to have. What I’d LIKE to do is something like this for a given UI control:

“PlayerNameLineEdit.gd”:

extends LineEdit, "res://Boilerplate.gd"

But

gdscript doesn’t support multiple inheritance. So what I’m currently doing is:

“PlayerNameLineEdit.gd”:

extends "res://LineEditBoilerplate.gd"

“LineEditBoilerplate.gd”

extends LineEdit

But I have to duplicate LineEditBoilerplate.gd for each control type… TextEdit, MenuButton, and change the “extends” line in each. Every time I want to change my boilerplate code, I have to make a change to 10+ nearly identical scripts.

Is there a better way?

:bust_in_silhouette: Reply From: kozaluss

Maybe You could make that boilerplate.gd a separate class and just preload and new() it in every control so that it works as a member variable. If You need access to object of control, You can pass it in some setup method for boilerplate class and work with the object similar way as if it was straight inheritance.