Break out Part of Character into a Separate Class and Instantiate it as a Blueprint

I have what may be a pretty specific question. I have a character called MyCharacter. I want to create a UObject class called MyCharacterStats. MyCharacterStats will have the characters stats like health, strength and stamina and getters and setters for each stat.

So all the above is fine, now it gets a little tricky. I want to add a

UPROPERTY(EditDefaultsOnly, Category = Character) 
class UMyCharacterStats* CharacterStats ;

What I want to do is create a Blueprint instantiation of MyCharacterStats. Then in the editor I can easily assign the MyCharacterStats blueprint to MyCharacter. This will make it easy to create multiple stat load out that we can swap in and out and easily change

Now the problem I am having. The blueprint of MyCharacterStats is a different type than the:

    UPROPERTY(EditDefaultsOnly, Category = Character) 
    class UMyCharacterStats* CharacterStats ;

Which means I can’t assign the blueprint to that property in the editor. So I did the following:

class UMyCharacterStats* CharacterStats 

UPROPERTY(EditDefaultsOnly, Category = Character)
TSubclassOf<class UMyCharacterStats> CharacterStatsTemplate;

This allowed me to assign the MyCharacterStats blueprint to the UPROPERTY of MyCharacter, in the editor. Now lastly I want to do this:

CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate);

But when I do this CharacterStats is all NULL. It seems like things aren’t being initialized. if anyone has any experience with this or any ideas I would really appreciate the help!

Cheers!

I just figured it out. Instead of:

CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate);

Do this instead:

CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate.GetDefaultObject());

This actually isn’t correct. This gets you the default object which means when ever you modify a value, you are actually changing the defaults. This won’t work.

If you want to have a mutable instance per-character, then you should be able to create one using NewObject since what you have in your CharacterStatsTemplate is essentially a UClass*.

CharacterStats = NewObject<UArenaCharacterStats>(this, CharacterStatsTemplate);

If you’re inside your constructor, you may need to use ObjectInitializer.CreateDefaultSuobject rather than NewObject.

I could kiss you! This is exactly what I was looking for, thanks.

Also make sure that your CharacterStats variable is marked as a UPROPERTY. This will prevent the GC system from thinking your newly allocated object is unreachable and deleting it.