+2 votes

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

asked Nov 8, 2019 in Engine by ibell (14 points)

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

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

Please log in or register to answer this question.

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 How to use this Q&A? before posting your first questions.