I have a C++ class derived from UObject called UCharacterStatistics where I have put all the stats for a character.
In the C++ class for my character called AFirstPlayer and derived from ACharacter I have a pointer to UCharacterStatistics . In AFirstPlayer I set up a get function for my stats.
class MYPROJECT_API AFirstPlayer : public ACharacter
{
GENERATED_BODY()
class UCharacterStatistics* CharacterStastistics;
public:
UFUNCTION(BlueprintPure)
class UCharacterStatistics* GetCharacterStatistics() const
{
return CharacterStastistics;
}
[...]
}
If I call the get function and use the reference it returns to check if it’s valid or not, when I press compile the editor crashes, giving me in the report an Assertion failed: Index >= 0 on file Runtime\CoreUObject\Public\UObject/UObjectArray.h
For some reason I am not able to just set the variable a UPROPERTY and use it in the editor (more [here][2] if you’re interested) so I needed a get function, which works just fine inside the event graph.
I would like to know why I am not allowed to call that function in the construction script, since I’m sure that the class constructor is called and CharacterStatistics initialized with CreateDefaultSubobject.
The CharacterStatistics variable is initialized in the constructor of the FirstPlayer using CreateDefaultSubobject. To be superclear, in the constructor:
I’m using the same pattern all over the place and it always works, as a weird suggestion, can you add the call to parent construction script before this check?
Sorry I don’t understand your request. And with the same pattern you mean setting a variable with UPROPERTY and use it inside the event graph of the editor?
Spawning component, store it in protected property and access thru getter function.
But, I went over some code, while I do the same, I never do it in exactly same manner, I use interfaces(BlueprintNativeEvent or BlueprintCallable pure virtual function).
I have UGameplayStatsComponent which holds current, max health and implements DealDamage(), also I have IGameplayStatsComponent_Holder which is an interface to implement into character or door which should have the functionality of being damaged. So if you willing to pay virtual function call, you can try those, those works, and not wait for a resolution from epic.
The problem is that I was thinking of using the construction script graph in some way in order to save the state of my character during the loading of a new level. But if the getter function doesn’t work and I cannot use the UPROPERTY properly on a class derived from a UObject, I need to find another way around this. Bummer…
I have a question if you can answer it: what is the base class of your UGameplayStatsComponent?
UCLASS(MinimalAPI, Blueprintable, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class UGameplayStatsComponent : public UActorComponent, public IGameplayStatsComponent_Interface
{
//body
}
from holder interface, which can be implemented to return proper property in c++ or blueprints. UGameplayStatsComponent can be forward declared in the interface, so you don’t recompile the whole project which includes holder interface.