I’m trying to dynamically create a character in my game based on a string in the GameInstance subclass. For example if the character is “Alice” I need to find the blueprint “AliceBP” so I can call SpawnActor().
I’ve already discovered that calling the ConstructorHelpers::FObjectFinder() in a context other than a constructor causes the Unreal editor to die - it just vanishes from my screen after I hit “Play”. OK, so rules are rules, but it seems that finding objects such as blueprints is fundamental to the game lifecycle so maybe that’s a limitation that Epic might care to look at. What would be interesting to know is this:
Can ConstructorHelpers be called from any constructor in any context, or are there further rules?
Speficially, can I use them in my GameMode ctor?
The docs had very little useful information in them, sadly. It would be good if they were fleshed out.
As far as I know there shouldn’t be any problems using a constructorhelper in GameMode.
However if you’d like to load an object at any time, you can use StaticLoadObject(). E.g. loading a Blueprint and spawning a class of that blueprint type:
// Asset path in content browser - you could also dynamically build this string
FString blueprintPath = TEXT("Blueprint'/Game/Weapons/Arms/Melee/Basic/ASW_Melee_BAS.ASW_Melee_BAS'");
// Use StaticLoad to load the blueprint
UBlueprint* blueprint = Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), NULL, *blueprintPath));
if (blueprint)
{
// AYourBlueprintClassType* yourBlueprintInstance = GWorld->SpawnActor<AYourBlueprintClassType>(blueprint->GeneratedClass);
}
Thanks!
From what I read - and as a beginner in UE4 I am probably wrong - but the use of StaticLoadObject() is frowned upon because it doesn’t work if the game is cooked?
For what it’s worth, here’s what I’m trying to achieve. Solutions in C++ preferred, won’t reject Blueprint solutions though. When my level starts up, I query the GameInstance for which characters are involved in the level. Say there are 2 strings - “Alice” and “Bob”. Both are of type ACharacter with different blueprints (obviously). So I need to get the AliceBP and the BobBP from the program assets in order to spawn the characters at their start locations.
Oh my - that would be quite catastrophic for me if StaticLoadObject() doesn’t work in a cooked game. Will look into that.