Calling one GDNative C++ class from another GDNative C++ class

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

We are currently using the Godot 3.1 and we try to get the instance of one GDNative C++ Class inside another:

Our test looks like this:

We have 2 Nodes (NodeA,NodeB)
Both of them have their own NativeScript which inturn have their own GDNativeLibary.
The GDNativeLibaries point to their own dll

In 1 dll we have the following class

class ButtonPressed : public Control {
	GODOT_CLASS(ButtonPressed, Control)

	float m_duration = {0.0};
	static void _register_methods();


	void _init(); // our initializer called by Godot
    void _process(float delta);

	void set_duration(float newDuration);
	float get_duration();

Its job is to adjust m_duration and allow access to this variable.
Everything compiles and works fine. When we try to access this class via GDScript everything also works as expected.

Now to our other dll

class LabelTextFiller : public Control {
	GODOT_CLASS(LabelTextFiller, Control)


	static void _register_methods();


	void _init(); // our initializer called by Godot

	void _process(float delta);

Its job would be to accees the ButtonPressed class and fill a label text with the information
We tried the following

void LabelTextFiller::_process(float delta){
auto labelNode = static_cast<Label*>(get_node("DisplayTextLabel"));

auto mainNode = get_parent()->get_node("NodeA");

auto myNode = static_cast<ButtonPressed*> (mainNode);
//auto myScript = (ButtonPressed*)mainNode->get_script(); //does not work either

auto nodeDuration = myNode->get_duration();
//auto nodeDuration = myScript->get_duration(); //does not work either


Which compiles and also kinda works.

The problem we are having is that the values returned in LabelTextFiller do not match the values we set in the Script in NodeA. The call inside _process seems to return a different instance of ButtonPressed than what is used in NodeA
If I call NodeA inside a GDScript from NodeB everything works

get_parent().get_node("NodeA").m_duration #this returns the proper value

Thanks in advance :3

Did you ever find a solution to this? I’m dancing around a similar problem trying to get two rust scripts to interact.

TaylorHadden | 2020-02-23 01:02

Sadly no. Only a dirty workaround by making the variables static

ibell | 2020-02-24 08:08

I have similar problems: it seems that if I try to access to member variable of a class from another class, the values of Class_A are completely different when accessed from Class_A with respect to the same values accessed from Class_B. Also the game crashes when I try to access to functions of Class_A from Class_B in GDNative C++.

TheZioba | 2021-04-11 16:59

:bust_in_silhouette: Reply From: TheZioba

Have you tried instead

Label* labelNode = Object::cast_to< Label>(get_node(“DisplayTextLabel”));


ButtonPressed* myNode = Object::cast_to< ButtonPressed> (mainNode);

In similar situations, this cast works for me.

I dont think we have tried this varient. Sadly I cannot confirm if it works as we have scraped the idea of using GDScript.
Still thanks for your answer :slight_smile:

ibell | 2021-04-12 07:40

I can confirm this works!

Bengt Söderström | 2021-05-23 14:23