I have several blueprint classes inheriting from a single cpp class. Let’s call one of them BP_Class, and say it’s parent is A_CPP_Class. I want to be able to do something like: /* ...logic to choose class to instantiate... */ A_CPP_Class* instance = new BP_Class();
I’ve read that in order to make a new blueprint class you have to use: Type* instance = NewObject< Type >();
but I still don’t actually have Type to use there.
I also read that you can get the class by exposing a UClass* to blueprints, and assign the class there. Given that, I tried: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UClass* m_BP_Class;
And then setting the class in the editor. Obviously, this doesn’t work. I can’t just substitute m_BP_Class for Type because it’s not actually a type.
Any information on this would be much appreciated. Thanks in advance.
But i see you class have “A” prefix which means it’s a actor, there diffrent function for actors as they need some extra work to be initiezed. It’s a function in UWorld* and you can access world instance from any actor using () function, so you do this:
()->SpawnActor<A_CPP_Class>(m_BP_Class);
You may wonder why UE4 dont simply use “new”, UE4 has managed object system called UObject and all classes derived from UObject need to be properly initized so UE4 can keep track on it, as make class usable in editor.
Works like a charm, thanks! I especially like that using TSubclassOf self documents the code better than just a UClass*, as well as limiting the drop down options in the editor. Very neat, thank you.