How do you load a resource in C++ GDNative?

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

The title says it all. Just using load() like in GDScript doesn’t work.

:bust_in_silhouette: Reply From: Ducku

And then you could cast it to the right type by prepending this


When I do that, I get an error that says this: “name followed by ‘::’ must be a class or namespace name.”
Which is weird, because it is a class name.

Edit: Turns out I just forgot to include the ResourceLoader header. However, I’m still having some issues. So I guess I should elaborate a little more on how I have my code set up:

In a header file, I have a variable declared, like this:
Object thing (am I supposed to change the Object to a (Ref<[Object]>)?)

And in the _init function of a cpp file, I need to set it.

So when I just do this:
thing = ResourceLoader::get_singleton()->load("path");
I get an error, saying the operand types are invalid, as makes sense. So the obvious reason for this is that I didn’t include the (Ref<[Type]>). But where am I supposed to include it? You said to prepend it, but prepend it where? Do I have to do it in the declaration like I said earlier? Do I have to put it before the variable initialization, like (Ref<[Object]>) thing = ResourceLoader::get_singleton()->load("path");? Am I supposed to put it inside the load()? I’m a little confused.

Coweh | 2020-10-23 14:49

Since load() returns a Ref, the type for thing has to indeed be Ref<[ResourceType]>
What I meant with prepend is to put it before the load statement.

You would get something like the following:
In your header file: Ref<[ResourceType]> thing; (I think a normal pointer works too but I’m not sure)
And where you want to load it: thing = (Ref<[ResourceType]>) ResourceLoader::get_singleton()->load("path");

Ducku | 2020-10-23 23:54

Oh, okay. That works. Thanks.

Coweh | 2020-10-24 00:23