Using an Actor within a Character

Hi Everyone!

I know this must be a really stupid question but i couldn’t find the solution. If this is a duplicate question, please link me the answer.

The problem is:

I made an actor called PickupItem

UCLASS()
class PROJECT_API APickupItem : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	APickupItem();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	FString Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	int32 Quatity;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	USphereComponent* ProxSphere;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	UStaticMeshComponent* Mesh;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	UTexture2D* Icon;
	
	UFUNCTION(BlueprintNativeEvent, Category = Collision)
	void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};

I made some pickup items from this class that when i walk nearby them, i collect the item.
Now i want a Character to be able to give me the item when i’m close to him. So i made an NPC class

UCLASS()
class PROJECT_API ANPC : public ACharacter
{
	GENERATED_BODY()

public:

	ANPC();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	USphereComponent* ProxSphere;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	FString NPCMessage;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	FString NPCName;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	UTexture2D* Face;
	
	UFUNCTION(BlueprintNativeEvent, Category = Collision)
	void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};

and tried to add

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	APickupItem* Item;

and on the constructor

Item = NewObject<APickupItem>();

And the Unreal Crashes (Even though the project actually compiles)
Also tried with

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
	AActor* Item;

But got the same result.

What i’m i doing wrong? i tried searching for something similar but all that i could found was on blueprint and parsing it to c++ didn’t even compile.

Basically what i want to do is to be able to expose those attributes to a blueprint so i can later define then as the item that the player will get.

Hello Darkhanis,

I tried to do a similar code as yours.
My unreal crashes too.
So I tried several things and eventually I success to do something which works.

Spawning actor in constructor seems not work so I moved the creation in the BeginPlay function.

NewObject didn’t work for me so I used [SpawnActor][1].

Here the code :

void ATestCharacter::BeginPlay()
{
	APaperCharacter::BeginPlay();

	// Test for Darkhanis
	FTransform Tr;
	Tr.SetLocation( GetActorLocation() + FVector( 150.0f, 0.0f, 0.0f ) );
	m_TestPlayerShot = (APlayerShot*)GetWorld()->SpawnActor( APlayerShot::StaticClass(), &Tr );
}

The header is similar as yours :

UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = Test )
	class APlayerShot* m_TestPlayerShot;

TestCharacter Is a paper character, equivalent to your NPC.
APlayerShot is a child from AActor, equivalent to your pickup.

So like this I can access to my actor from a blueprint extend from my character :

63126-blueprintaccess.png

I hope it will help you.
( Sorry for my bad english )