Download

How to create a component and initialize it from another component (C++) ?

I have been trying without success to create a Sprite Component in the constructor of another component and call setsprite on it. Like this:


UAPPaperTilesetAnimationComponent::UAPPaperTilesetAnimationComponent(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    AnimationIndex = 0;
    FString ComponentName = GetName().Append("Renderer");
    RenderComponent = CreateDefaultSubobject<UPaperSpriteComponent>(*ComponentName);

    // This crashes
    RenderComponent->SetSprite(AnimationSprite);


}

What am I missing? Any documentation or tutorial that explains how to do this exactly? Thank you.

CreateDefaultSubobject should be all you need. What is AnimationSprite? Looks like it might be nullptr there.

Yes, you were right. It was nullptr. AnimationSprite is just an UPaperSprite. And in constructor that UProperty is nullptr.

Do you know which is the first function that will always be called that has the UProperties correctly loaded? I tried with PostInitProperties but… is nullptr there too (maybe wrong name for that function? >_<U )

Thanks you.