So I want to know how I can get reference to a BP that inherits from my class. Say I have a ACharacterBP which inherits from a ACharacter C++ class. How would I get a reference to that ACharacterBP?
I know the following pieces of code might help, but I’m not sure how to distinguish between them and what each means or even if they’re the right code to use for getting a reference to a BP:
UPROPERY(EditAnywhere, BlueprintReadWrite, Category = "Hello")
class ACharacter* TheCharacterBP;
//OR
UPROPERY(EditAnywhere, BlueprintReadWrite, Category = "Hello")
ACharacter* TheCharacterBP;
//OR
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Hello")
TSubclassOf<class ACharacter> TheCharacterBP;
There are references to a blueprint class and there are references to an instance of a blueprint class. TSubclassOf<ACharacter> is an example of the former, it is a UClass object that represents any subclass of ACharacter and could be a blueprint class. If you spawn an instance of this, for example using UWorld::SpawnActor(SomeBlueprintClass) where SomeBlueprintClass is of type TSubclassOf, this will be an instance of the blueprint class you created in the editor. The type of the spawned actor is a pointer of your C++ base class, in this case ACharacter*. In C++ you can access the variables and functions that are defined in the C++ base class, but variables and functions that were added in the blueprint subclass via the editor are harder to access (but not impossible I believe).
To access variables and functions that are defined in the blueprint subclass, its usually easiest to declare the variables and functions in C++ and then set them in blueprint, or override them in case of functions. For example for functions declare in C++:
UFUNCTION(BlueprintImplementableEvent)
void MyFunction();
So that C++ is aware that the function exists, then override it in blueprint by implementing Event MyFunction. Hope that helps!
Another thing, class ACharacter* and ACharacter* are actually the same type. However, class ACharacter* is a forward declaration, telling the compiler that the variable is a pointer of type ACharacter but it doesn’t need to know in the scope of your file what ACharacter actually is. By using “class ACharacter” you don’t have to include the Character’s header file. Ultimately that improves compile times.
Thank you both of you for the help. I understand now that TSubclassOf<> is a reference to a blueprint class itself, and the other one is an reference to an actual instance of the class.
That’s it! You’re welcome from both of me. 