0 votes

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

in Engine by (68 points)

1 Answer

+4 votes
Best answer
ResourceLoader::get_singleton()->load(<path>)

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

(Ref<[Type]>)
by (69 points)
selected by

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.

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");

Oh, okay. That works. Thanks.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.