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"

SphereComponent implementation

Hello guys, I am having a trouble with a blueprintnativeevent (I'm following this book: Learning C++ by Creating Games with UE4). My classes look like this: NPC.cpp ->

 // Fill out your copyright notice in the Description page of Project Settings.
 #include "MyFirstCpp.h"
 #include "NPC.h"
 #include "MyHUD.h"
 #include "Avatar.h"
 
 ANPC::ANPC(const FObjectInitializer& PCIP) : Super(PCIP)
 {
     ProxSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("Proximity Sphere"));
     ProxSphere ->AttachParent = RootComponent;
     ProxSphere->SetSphereRadius(32.f);
     //Code to make ANPC::Prox() run when this proximity sphere overlaps
     //another actor.
     ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::Prox);
     NpcMessage = "Hi, I'm a NPC";
 }
 
 void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
 {
     if (GEngine)
     {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Calling Prox Event"));
     }
 
     if (Cast<AAvatar>(OtherActor) == nullptr)
     {
         return;
     }
 
     APlayerController* PController = GetWorld()->GetFirstPlayerController();
     if (PController)
     {
         AMyHUD* hud = Cast<AMyHUD>(PController->GetHUD());
         hud -> addMessage(Message(NpcMessage, 5.f, FColor::White));
     }
 }


NPC..h:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "GameFramework/Character.h"
 #include "NPC.generated.h"
 
 UCLASS()
 class MYFIRSTCPP_API ANPC : public ACharacter
 {
     GENERATED_BODY()
     // Sets default values for this character's properties
 public:
     ANPC(const FObjectInitializer& ObjectInitializer);
     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
     USphereComponent* ProxSphere;
     // This is the NPC's message that he has to tell us.
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
     FString NpcMessage;
     // When you create a blueprint from this class, you want to be
     // able to edit that message in blueprints,
     // that's why we have the EditAnywhere and BlueprintReadWrite
     // properties.
     UFUNCTION(BlueprintNativeEvent, Category = Collision)
     void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
     
     virtual void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
 };
 

HUD.cpp:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "MyFirstCpp.h"
 #include "MyHUD.h"
 
 void AMyHUD::DrawMessages()
 {
     for (int c = messages.Num() - 1; c >= 0; c--)
     {
         float outputWidth, outputHeight, pad = 10.f;
         GetTextSize(messages[c].message, outputWidth, outputHeight, hudFont, 1.f);
         float messageH = outputHeight + 2.f*pad;
         float x = 0.f, y = c*messageH;
 
         DrawRect(FLinearColor::Black, x, y, Canvas->SizeX, messageH);
         DrawText(messages[c].message, messages[c].color, x + pad, y + pad, hudFont);
         messages[c].time -= GetWorld()->GetDeltaSeconds();
         
         if (messages[c].time < 0)
         {
             messages.RemoveAt(c);
         }
     }
 }
 
 void AMyHUD::DrawHUD()
 {
     Super::DrawHUD();
     //DrawText("Greetings from Unreal!", FVector2D(1, 1), hudFont, FVector2D(3, 3), FColor::White);
     DrawMessages();
 }
 
 void AMyHUD::addMessage(Message msg)
 {
     messages.Add(msg);
 }
 

HUD.h:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "GameFramework/HUD.h"
 #include "MyHUD.generated.h"
 
 /**
  * 
  */
 struct Message
 {
     FString message;
     float time;
     FColor color;
     Message()
     {
         time = 5.f;
         color = FColor::White;
     }
     Message(FString iMessage, float iTime, FColor iColor)
     {
         message = iMessage;
         time = iTime;
         color = iColor;
     }
 };
 
 UCLASS()
 class MYFIRSTCPP_API AMyHUD : public AHUD
 {
     GENERATED_BODY()
 public:
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont)
     UFont* hudFont;
 
     TArray<Message> messages;
 
     virtual void DrawHUD() override;
 
     void DrawMessages();
     
     void addMessage(Message msg);
 
     
 };
 

Now my problem is that the sphere does nothing. I don't know if it isn't implemented well or something and I am searching through internet for answer but can't find a solution. I'll give you some photos of the principal assets. alt text alt text

Product Version: UE 4.10
Tags:
more ▼

asked Mar 03 '16 at 02:33 PM in C++ Programming

avatar image

Forjaser
1 1 2 3

avatar image Sean L ♦♦ STAFF Mar 03 '16 at 06:04 PM

Hello,

I have a few questions regarding this issue:

  • Are you using a class based on Character?

  • If so, have you tried using a class based on Pawn instead?

  • What exactly are you expecting the Sphere Component to do?

avatar image Forjaser Mar 03 '16 at 06:34 PM

I have the NPCs with the Sphere Component in the same BP. What I want is to "print" in the HUD the string "NPCMessage" when I overlap them.

What's the problem with classes based on Character?

avatar image Forjaser Mar 04 '16 at 12:15 PM

Wow!! That was fast, thank you very much I'm still a newbie in unreal engine :D

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

1 answer: sort voted first

Character-based classes that use the character movement component require the capsule component to function properly. This means that no other collisions that are added to that class will work, because at this time the character class only supports the basic capsule collision by default. If you try using a pawn class, I'd be curious to hear the results.

more ▼

answered Mar 03 '16 at 06:37 PM

avatar image

Sean L ♦♦ STAFF
43.5k 485 152 442

(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