Pass a class instance from GDScript to c++ module class

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

I’m trying out a sample c++ module. How can I pass an instance from GDScript to c++ module class? In the sample below, showNumber() uses the default returned value of A.getNumber() instead the returned value of SubA.getNumber().

sample.h

#ifndef SAMPLE_H
#define SAMPLE_H

#include "reference.h"

class A : public Reference
{
    GDCLASS(A, Reference);

protected:
    static void _bind_methods();

public:
    virtual int getNumber();
};

class B : public Reference
{
    GDCLASS(B, Reference);

protected:
    static void _bind_methods();

public:
    int showA(Ref<A> a);
};

#endif

sample.cpp

#include "sample.h"
#include "variant.h"

// A
int A::getNumber() 
{
    return 100;
}

void A::_bind_methods()
{
    // ClassDB::bind_method(D_METHOD("getNumber"), &A::getNumber);
    BIND_VMETHOD(MethodInfo(Variant::INT, "getNumber"));
}

// B
int B::showA(Ref<A> a)
{
    return a->getNumber();
}

void B::_bind_methods()
{
    ClassDB::bind_method(D_METHOD("showA", "a"), &B::showA);
}

gdscript

extends Node

class SubA extends A:
	func getNumber():
		return 50
	
func _ready():
	var sub = SubA.new()
	print("SubA: %d" % sub.getNumber())
	
	var b = B.new()
	print("SubA passed to B: %d" % b.showA(sub))

Output:

OpenGL ES 3.0 Renderer: Mesa DRI Intel(R) Sandybridge Desktop 
SubA: 50
SubA passed to B: 100

I would first start asking then irc then I would recommend

reporting it to the issue page.

at least report it to godot-docs

they would probably tell you what is wrong faster

https://github.com/godotengine/godot/blob/master/core/class_db.h#L389

I am looking at the bind_method code and it does nothing when tools are disabled.

hungrymonkey | 2018-05-09 23:19