I have a sprite which I want to attach to my character. Sooo… how do I do that?
Add a sprite component to your character.
I know but how do I do that in code?
In your character class header, you have to create a variable of a type UPaperSpriteComponent*
as shown below:
UCLASS()
class MYPROJECT_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
...
private:
UPROPERTY(EditAnywhere)
UPaperSpriteComponent* CharacterSprite;
}
Then, in your constructor, you have to construct the component and attach it to your RootComponent.
AMyCharacter::AMyCharacter()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CharacterSprite = CreateDefaultSubObject<UPaperSpriteComponent>(TEXT("CharacterSprite"));
CharacterSprite->SetupAttachment(RootComponent);
}
Now you should be able to spawn your character in editor and set the source sprite for it. Good luck!