0 votes

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 https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html, 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?

Godot version 3.5.1 mono
in Engine by (49 points)
edited by

2 Answers

0 votes

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.

by (2,017 points)

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

+1 vote

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

https://docs.godotengine.org/en/stable/tutorials/scripting/nodes_and_scene_instances.html?highlight=get_node

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

by (3,326 points)

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

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.

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.

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) 

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.
https://imgur.com/a/dShT8x4

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.

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.