Is it possible to call a function in a parent class' parent class?

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

You can call a parent class’ function like this:

.example_function(variable_thingy)

But is it possible to call that parent class’ parent class’ function (and so on) directly? And if not, why? I don’t see any mention of it in GDScript reference — Godot Engine (stable) documentation in English, so it made me curious.

EDIT: I see that I have not asked that question correctly. I am referring to a more specific scenario. Take this situation, for instance:

extends Node2D

func some_random_stuff():
    print("stuff")

Let’s call the node above Enemy. Now, I make a node derived from the above node and overwrite this function:

extends "res://Enemy.gd"

func some_random_stuff():
    .some_random_stuff()
    print("different stuff")

This one would be called Enemy_Drone. Now I make a node inheriting from THAT node and overwrite the overwritten function:

extends "res://Enemy_Drone.gd"

func some_random_stuff():
    .some_random_stuff()
    print("even more different stuff")

And this node is called Enemy_Drone_Special.
Now, if I call .some_random_stuff() inside of Enemy_Drone_Special, the result will be

stuff
different stuff
even more different stuff

because .some_random_stuff() will be calling Enemy_Drone’s function (which also calls Enemy’s function), but I want to call Enemy’s function directly instead of using Enemy_Drone’s so that the result is like this:

stuff
even more different stuff

Is that possible?

:bust_in_silhouette: Reply From: magicalogic

It doesn’t matter whether it’s a parent or a parent of a parent, inherited functions behave the same way and you call them using the same syntax.
That is why you are able to call functions such as look_at() on any 3D node, while the function is inherited from way up the hierarchy in the Spatial class.

I believe I have not explained the issue I have correctly and I see why. Allow me to modify the question.

Ox0zOwra | 2022-12-12 14:50

:bust_in_silhouette: Reply From: Gluon

You can use the get_node method to call a function. Documentation is here

so you can do

get_node("exampleNodeName").example_function(variable_thingy) 

however this is normally not a good idea. You are hard coding one node to use a function in another node which is not in the direct tree of the node calling it. If you delete the node with the function it will break your game and would be hard to track down the fault. Normally its better to use signals for this. A signal is something a node sends out to the whole game and then you can tell other nodes to look out for the signal and perform some code if they hear it. That way if you delete a node you wont break the whole game because it may stop a signal being sent but that will only mean the code associated with it isnt run.

https://docs.godotengine.org/en/stable/tutorials/scripting/visual_script/nodes_purposes.html?highlight=signal#signals

I have not written my question correctly, so this wasn’t really the answer I was looking for, but thank you anyway!

Ox0zOwra | 2022-12-12 15:03

I can see your updated response but the answer doesnt change. You can either use get_node or signals. Thats how you communicate and activate functions in other nodes like this.

Gluon | 2022-12-12 15:06

I’m not trying to say that the nodes themselves are children of the particular node. It’s true that this is how you communicate between nodes in Godot, but this is not about Godot’s node trees, but more about inheritance.

I’m trying to say they’re class children, so that they inherit functions of their class parents. Or am I not getting it and you’re saying you can use get_node() for class children to use functions of their class parents like this? If this is what you’re saying, I apologize for not understanding before.

Ox0zOwra | 2022-12-12 15:17

enter code hereYou can pull functions directly from the parent with a line like this;

.example_function(variable_thingy)

because the godot game engine sees this as shorthand for the get_node with the parents name in it. You are in effect using get_node with the above it is simply that the shorthand in gdnative allows you to pull from the parent. If you want to get a function from another node you need to specify which one with a line like

get_node("exampleNodeName").example_function(variable_thingy) 

Gluon | 2022-12-12 15:20

I read your post multiple times and the contents of the links you directed me to and I believe we may have a small misunderstanding.
Imgur: The magic of the Internet

This is the part of the documentation that I am talking about. You are talking about getting parent nodes already in the scene tree. I am talking about using functions from a class that inherits from another class.

Just to make sure, I tried using get_node() on the class’ name, but it didn’t work.

Ox0zOwra | 2022-12-12 15:37