UPROPERTY(EditDefaultsOnly, Category = “Bomb|Settings”)
TSubclassOf DamageType;
For these codes,what’s the type of DamageType?Of course it’s not a instance of UDamageType,so is it a instance of some subclass of UDamageType?Then actually what’s its type?
It’s basically a reference to a class that is of type UDamageType. This would mainly be used as a mean to spawn an object of that type, when using SpawnActor for example.
Here is an example from my project:
UPROPERTY(EditDefaultsOnly, Category = Hands, DisplayName = "Torch Blueprint")
TSubclassOf<AActor> mTorchClass;
//In code, where you need to spawn
AActor* mTorchActor = GetWorld()->SpawnActor<AActor>(mTorchClass);
The type is TSubclassOf in you example, that is any UClass* that is a UDamageType class.
Example could be : UDamageFire, UDamageIce, etc (I dont really know the damage type that are in the engine, just giving examples).
It is the same data you would get by using the ::StaticClass() on any object of your game : a UClass*!
Hope this was clear enough!
I’ve found it generally helps when explaining this as “Instead of like you usually have a variable referencing an instance of a class, this literally references a class or any class derived from it.”
Let’s talk about the example codes from your project:
what’s TSubclassOf:
Just like a definition: float f.So before the variable f,float is the type. For this case,TSubclassOf must be a type,so actually what’s the type?
Ok, I’ll try to explain better:
float f = 5.0f;
In this case, the variable name is “f”, it’s of type “float” and the value is “5.0”
Now let’s try this :
TSubclassOf<AActor> mTorchClass = ATorchActor::StaticClass();
In that case, the variable name is “mTorchClass”, the type is “TSubclassOf AActor” and the value is “ATorchActor::StaticClass()”.
Now, from what I understand, you may wonder what TSubclassOf AActor actually is.
For example “float” is a floating point number (a decimal number) that can be either positive or negative and as 32 bits of memory, making it a number between -3.4E+38 to +3.4E+38. I assume you already now that, I’m only drawing the parallel between the 2 so it becomes clearer for you.
Now “TSubclassOf AActor” is the representation of the default class, the “static” version of a class if you will that is either a AActor or one of it’s subclass. Let’s say for example that ATorchActor is a very simple actor (that derives from AActor) with a member that is a integer with default value is 5 : “int32 mSmallValue = 5;”
When I’m doing “mTorchClass = ATorchActor::StaticClass();” I’m assigning mTorchClass that “default” class value of ATorchActor, or the “StaticClass” like the name of the function implies. In our case, one the default value is mSmallValue = 5; See it as a template if you will.
So, to continue the example, when finally I’ll do this:
ATorchActor* mTorchActor = GetWorld()->SpawnActor<ATorchActor>(mTorchClass);
By default, mTorchActor->mSmallValue will have value “5”.
Hope this is clearer!
Thank you for this. It came in handy!