x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.9
Tags:
more ▼

asked Oct 18 '15 at 06:04 AM in C++ Programming

avatar image

Darkhanis
8 2 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

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 :

alt text

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

more ▼

answered Oct 18 '15 at 10:54 AM

avatar image

Lilyël
96 2 7 10

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question