Spawn actor with class name in string

Hi,

I have some game logic in an external process from which I’d like to spawn actors. The idea is that the external process sends the name of the actor class it wants to spawn and some other info, and then in my c++ project I find the corresponding class name and do SpawnInstance. At the moment my code looks a bit like this:




auto klass_str = TEXT("SuperActor_C");
UClass* uClass = FindObject<UClass>(ClassPackage, klass_str);



There’s two problems with this. The first is the one I’ve been struggling with the longest. The FindObject call will always return nullptr, unless an object of that class is already in the level (or has been in the level). So I think the class is dynamically loaded or something and only after that’s been done it can find the class.

The second is when I actually do world->SpawnActor on the uClass that I get from that code it crashes. I haven’t researched that much more because I only just discovered I can get the FindObject not return null in that situation.

Is this the best way of getting a UClass from a string? Should I do some more hacking to get a more reliable way of achieving this?

Kind regards,

I took a look at this for a bit this evening and even with quite a few examples in the codebase I wasn’t able to get FindObject to do what I wanted to. I’m sure there’s a solution here that I’m just not handy with.

I think what you’re doing is a bit unusual compared to the normal use-case, typically you grab any classes you need in your object constructor using FClassFinder and stow them away for later use. I’m sure it can be done, hopefully someone brighter than me can chime in.

Try using LoadObject instead of FindObject. FindObject will only return true if it is already in memory, whereas LoadObject will load it if it doesn’t. Also keep in mind, that if you’re loading it you will need the full path. Something like /Game/Blueprints/SuperActor.SuperActor_C

Hi Marc, thanks a lot for your hint. I appreciate that there’s a LoadObject that is more reliable, but it’s a annoying that it needs the whole path.

Is there a chance we could lookup unloaded classes simply by their name? Perhaps that we could compile a list of blueprints with their class names and their filepaths and have that accessible as a map somewhere?

Alternatively, could it be possible if we could mark classes with ‘always load’ or something like that? That way there wouldn’t be a performance hit the first time an object is dynamically loaded.

Just some thoughts, I don’t know how much demand there is for dynamically loaded objects.