Problem binding a method of a custom module that takes a reference to same type

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Will
:warning: Old Version Published before Godot 3 was released.

OK, I’m going to confess, I’m still new to C++ but learning as I go, anyway. I’m trying to implement a simple 2d collision check using rectangles (my own type “Rectangle2d”) that doesn’t require the physics engine and I want to check for overlaps against another rectangles. Sounds simple but I hit a problem when trying to bind the method and compile. I’ve got modules working before so this is a specific problem.

so far I have the header, very simple …

public:
bool overlaps(const Rectangle2d& p_other) const;

I have the implementation in the cpp file (defaults true for now)

bool Rectangle2d::overlaps(const Rectangle2d& p_other) const {
return true;}

with the method bind

ObjectTypeDB::bind_method(_MD("overlaps","other"),&Rectangle2d::overlaps);

But when I go to compile, I get the following error

‘return’: cannot convert from ‘const Variant’ to ‘Rectangle2d’

Am I not a allowed to pass a reference to the same type, or am I missing something more fundamental? I have been trying to follow other examples in the code base where a Vector3 might be passed in, I would appreciate any help or even a link to an example where something like this (self type reference passing) has been done before that I could follow along with.

Thanks.

Not an answer for the binding issue, but you know that Shape2D already have that simple overlap thing?

eons | 2016-11-27 16:45

:bust_in_silhouette: Reply From: Zylann

It looks like when you expect a type that is not derived from Object, Godot tries to convert it to Variant.
In fact, all types that don’t inherit Object are listed in Variant https://github.com/godotengine/godot/blob/master/core/variant.h

I doubt you want to inherit Object though, because you want your Rectangle2d to be a value type, not a reference type.

I don’t know what can be done to add a value type without adding it to Variant, but there is already a Rect2 with intersection methods so your class would just be redundant.