Trying to add a SpriteComponent to an Actor results in member of dll interface class may not be...

Hello everyone,

I’m fairly new to C++ in general so I might just do something stupidly wrong.
As said, I’m trying to create an actor with a sprite to be able to place it in the scene.

After that I will try to display the text of the actor in a 2d caption to give more info about what’s displayed and a link between the text and the position of the object.

I pretty much copied the code of a Note and included the “BillboardComponent.h” and the “Components/ArrowComponent.h”.

It will be easier with the code so here is the header :


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Components/BillboardComponent.h"
#include "Components/ArrowComponent.h"
#include "abbVieAppTooltipActor.generated.h"

UCLASS(hidecategories = (Input), showcategories = ("Input|MouseInput", "Input|TouchInput"))
class ABBVIEAPP_API AabbVieAppTooltipActor : public AActor
{
	GENERATED_UCLASS_BODY()
	//virtual void BeginPlay() OVERRIDE;

#if WITH_EDITORONLY_DATA
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Note, meta = (MultiLine = "true"))
	FString Text;

	// Reference to sprite visualization component
private_subobject :
	UPROPERTY()
	class UBillboardComponent* SpriteComponent;

	UPROPERTY()
	class UArrowComponent* ArrowComponent;
#endif // WITH_EDITORONLY_DATA

public:
	UPROPERTY(EditAnywhere)
	AActor* CameraLink;

#if WITH_EDITORONLY_DATA
	/** Returns SpriteComponent subobject **/
	ENGINE_API class UBillboardComponent* GetSpriteComponent() const;
	/** Returns ArrowComponent subobject **/
	ENGINE_API class UArrowComponent* GetArrowComponent() const;
#endif
};


And here is the cpp :


// Fill out your copyright notice in the Description page of Project Settings.

#include "abbVieApp.h"
#include "abbVieAppTooltipActor.h"

AabbVieAppTooltipActor::AabbVieAppTooltipActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	USceneComponent* SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
	RootComponent = SceneComponent;
	RootComponent->Mobility = EComponentMobility::Static;

#if WITH_EDITORONLY_DATA
	ArrowComponent = CreateEditorOnlyDefaultSubobject<UArrowComponent>(TEXT("Arrow"));
	SpriteComponent = CreateEditorOnlyDefaultSubobject<UBillboardComponent>(TEXT("Sprite"));

	if (!IsRunningCommandlet())
	{
		// Structure to hold one-time initialization
		struct FConstructorStatics
		{
			ConstructorHelpers::FObjectFinderOptional<UTexture2D> SpriteTexture;
			FName ID_Tooltips;
			FText NAME_Tooltips;
			FConstructorStatics()
				: SpriteTexture(TEXT("/Engine/EditorResources/S_Note"))
				, ID_Tooltips(TEXT("Tooltips"))
				, NAME_Tooltips(NSLOCTEXT("SpriteCategory", "Notes", "Notes"))
			{
			}
		};
		static FConstructorStatics ConstructorStatics;

		if (ArrowComponent)
		{			
			ArrowComponent->ArrowColor = FColor(150, 200, 255);

			ArrowComponent->ArrowSize = 0.5f;
			ArrowComponent->bTreatAsASprite = true;
			ArrowComponent->SpriteInfo.Category = ConstructorStatics.ID_Tooltips;
			ArrowComponent->SpriteInfo.DisplayName = ConstructorStatics.NAME_Tooltips;
			ArrowComponent->Mobility = EComponentMobility::Static;		
		}

		if (SpriteComponent)
		{			
			SpriteComponent->Sprite = ConstructorStatics.SpriteTexture.Get();
			SpriteComponent->SpriteInfo.Category = ConstructorStatics.ID_Tooltips;
			SpriteComponent->SpriteInfo.DisplayName = ConstructorStatics.NAME_Tooltips;
			SpriteComponent->SetupAttachment(RootComponent);
			SpriteComponent->bIsScreenSizeScaled = true;
			SpriteComponent->Mobility = EComponentMobility::Static;
			//SpriteComponent->bVisible = true;
			//SpriteComponent->bAbsoluteScale = true;
		}
	}
#endif // WITH_EDITORONLY_DATA

	bHidden = false;
	bCanBeDamaged = false;
}

// Called every frame
/*
void AabbVieAppTooltipActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}*/

#if WITH_EDITORONLY_DATA
/** Returns SpriteComponent subobject **/
UBillboardComponent* AabbVieAppTooltipActor::GetSpriteComponent() const { return SpriteComponent; }
/** Returns ArrowComponent subobject **/
UArrowComponent* AabbVieAppTooltipActor::GetArrowComponent() const { return ArrowComponent; }
#endif


I already googled and looked everywhere but the solutions to add a sprite for an actor are usually outdated or not the way I want.

Thanks for trying to help :stuck_out_tongue:

Maybe you could use WidgetComponent instead.
I mean,

  1. add a widget component to the customize actor;
  2. create a create a userwidget blueprint, including the text and textures that you want to display;
  3. bind the userwidget blueprint created in step2 to the widget component by using SetWidgetClass();
  4. use SetWidgetSpace(EWidgetSpace::Screen) to display these things in 2d.

some code:
.h



UPROPERTY()
class UWidgetComponent* WidgetComp;


.cpp

Constructor:



WidgetComp = CreateDefaultSubobject<UWidgetComponent>(TEXT("WidgetComp"));
if (WidgetComp)
{
WidgetComp->SetRelativeLocation(FVector(0.f, 0.f, 100.f));
WidgetComp->SetWidgetSpace(EWidgetSpace::Screen);
WidgetComp->SetupAttachment(RootComponent);

UClass* WidgetCompClass = LoadClass<UUserWidget>(NULL, TEXT("WidgetBlueprint'/Game/UI/SpriteBP.SpriteBP_C'"));
//the userwidget created in step2
if (WidgetCompClass)
{
WidgetComp->SetWidgetClass(WidgetCompClass);
}
}


First of all, you should be putting those #includes in the .cpp file where possible and use forward declaration in the header (which is actually already there, UArrowComponent* and UBillboardComponent* have the ‘class’ keyword before them.)

Putting #includes in the header file can increase compilation time and result in circular dependencies. Wherever possible, try to put them in the .cpp’s instead.


Without posting the error, it’s not really possible to help much. I will say that code looks pretty complex for just adding a sprite component to the actor.