Doesn’t work, and prints an error (“SpawnActor failed because BlueprintGeneratedClass is not an actor class”).
Now, all the articles online only mention how to spawn objects using hardcoded path, FStaticObjectFinder and FStringAssetReference.
There’s also TAssetPtr which require you to babysit object loading.
How do I spawn thing via TSubclassOf pointer? basically, I want the same kind of workflow as I had in unity, where you specify base class you want to reference, drag content browser asset to it, then clone it at runtime to make a copy.
This does not do what you assumed it does and intended to do. actorPrefab->GetClass() will call the GetClass() method on the UClass currently assigned to the TSubclassOf<> variable, the class of a blueprint class is BlueprintGeneratedClass. To retrieve the actual UClass (actor class) assigned simply use “*actorPrefab”.
Would you mind explaining the logic here? If you have the time.
Usually (in C++) templated wrapper that stores pointer to something redirects calls to contents via operator-> and operator* would be doing the same thing.
You say that in this case -> redirects to something that is not the class specified as template parameter? Why?
It should be clear if you look at the definition of the TSubclassOf template. What it holds is actually a UClass*, not an instance of AMyActor. It simply uses the tempate argument to do some type checks (both compile time and runtime) for safety, but essentially a TSubclassOf is just a fancy UClass*.
So, given that it represents a UClass and not an instance, you don’t want to be calling GetClass on it.
Basically, if in unity prefab is a “sample object”(that acts like object instance) that needs to be duplicated, in UE4 blueprint is treated as separate object class and reference to blueprint essentially just marks instance of which class you’d like to construct. Interesting.