Declare function in both extended script and script it extends from

Godot Version

4.3 stable Windows 11

Question

I’d like to be able to have builtin functions like _process(delta) and _ready in two scripts, even if one script extends from the other.

This is because I’m using a base script for every player, and then each variation of player will have its own script that extends from everything in the player script, in addition to its own code.

For example, I’d like to use the _process(delta) function in some character scripts to flip the sprite based on direction - but not others in case I don’t want that for that character, but I can’t do that because I already use _process(delta) to animate the UI in the universal player script.

One workaround I tried is this:

func _process(delta):
    animate_UI()
    if self.has_method("character_specific_function"):
        character_specific_function()

But that didn’t work, as you can’t call a function from a script that your current script extends from… not even if you check for that function, apparently.

Is there a way to accomplish this? I’d really like to know. Any help is appreciated.

It’s called super

You would do something like this

func _process(delta):
  # call parent class version of _process
  super(delta)
  # do local class function 
  ...

I haven’t ever used super in gdscript, i know it’s possible for _init so i also hope it it works for _process.

Super is a common keyword for class inheritance behavior in many languages.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.