Ðé»Ã4C++ѧϰÎĵµ·­Òë4 ÏÂÔØ±¾ÎÄ

ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2026/9/20 14:39:29ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£

´´½¨ºÍNPCµÄ¶Ô»°´°Ìå

ÎÒÃÇÐèÒª´´½¨Ò»¸ö¼Ì³Ð×ÔHUDµÄÀà

UCLASS()

class CPLUSLEARN_API AMyHUDMessage : public AHUD {

public : };

void AMyHUDMessage::DrawHUD() {

DrawText(\»¶Ó­À´µ½Ðé»ÃµÄÊÀ½ç£¡£¡\, FVector2D(0, 0), hudFont, FVector2D(1, 1), DrawLine(200, 300, 400, 500, FLinearColor::Blue); Super::DrawHUD();

virtual void DrawHUD() override;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont) UFont *hudFont; GENERATED_BODY()

FColor::White); }

ÎÒÃÇÔÚ×Ô¼ºµÄHUDµÄÀàÖж¨ÒåÁËÒ»¸ö×ÖÌ壬²¢ÇÒÔÚ½çÃæÉÏ»æ³öÒ»¸öÏß¿òºÍÎÄ×Ö

ÖØÐ±àÒëÏîÄ¿£¬ÎÒÃǽøÈëUE4ÖУ¬´´½¨Ò»¸ö¼Ì³Ð×ÔMyHUDµÄÀ¶Í¼À࣬ͬʱÉèÖÃÎÒÃÇ×Ô¼ºµÄ×ÖÌåÐÅÏ¢¡£×îºóÔÚÎÒÃǵÄÓÎϷģʽÖн«Ä¬ÈϵÄHUD¸Ä³ÉÎÒÃÇ×Ô¼º´´½¨µÄHUD

ÏÂÃæÎÒÃÇÒªÓÃÒ»¸öÒ»¸öÁбíÀ´´æ·ÅÕâЩ¶Ô»°µÄÐÅÏ¢£¬ÎÒÃÇ´ò¿ª×Ô¼º´´½¨µÄC++µÄHUDÀà

void AMyHUDMessage::DrawHUD() {

//DrawLine(200, 300, 400, 500, FLinearColor::Blue); Super::DrawHUD();

}

void AMyHUDMessage::addMessage(Message msg) { }

messages.Add(msg); }

//µ±¸ÃÌõÏûÏ¢µÄʱ¼äСÓÚ0µÄʱºò½«¸ÃÌõÐÅÏ¢ÒÆ³ý if (messages[c].time < 0) { }

messages.RemoveAt(c);

//ʱ¼äÖð²½Ëõ¼õ

messages[c].time -=GetWorld()->GetDeltaSeconds();

//ʹÓÃ×ÖÌ廿֯³öÎÒÃǵÄÐÅÏ¢(ÐÅÏ¢£¬ÑÕÉ«£¬¿í¶È+±ß¾à£¬¸ß¶È+±ß¾à£¬×ÖÌå)

DrawText(messages[c].message, messages[c].color, x + pad, y + pad, hudFont);

//»æÖƳö±³¾°

DrawRect(FLinearColor::Black, x, y, Canvas->SizeX, messageH);

float messageH = outputHeight + 2.f*pad;//ÐÅÏ¢µÄ¸ß¶È¾ÍÊÇÊä³ö¸ß¶È+2±¶µÄ¼ä¸ô float x = 0.f, y = c*messageH;

for (int c = messages.Num() - 1; c >= 0; c--) {

float outputWidth, outputHeight, pad = 10.f; //¶¨ÒåÊä³öµÄ¿í¶È¸ß¶ÈºÍ¼ä¸ô

//»ñÈ¡³öÐÅÏ¢ÄÚÈÝ´óС

GetTextSize(messages[c].message, outputWidth, outputHeight, hudFont, 1.f); //DrawText(\

ÏÂÃæÎÒÃDZØÐëÒªÍê³ÉÎÒÃǵÄNPCʵÌå

UCLASS()

class CPLUSLEARN_API ANonePlayerEntity : public ACharacter

{ public: };

//NPCËùÐèÒª´«µÝµÄ»°

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)

FString NPCMessage; //ÅöײÆ÷´¥·¢Æ÷

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)

TSubobjectPtr ProxSphere; //Åöײʼþ

UFUNCTION(BlueprintNativeEvent, Category = \)

void Prox(AActor* otherActor, UPrimitiveComponent* OtherComp, int32 // Called to bind functionality to input

virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) // Called when the game starts or when spawned virtual void BeginPlay() override;

// Called every frame

virtual void Tick( float DeltaSeconds ) override; // Sets default values for this character's properties ANonePlayerEntity(); GENERATED_UCLASS_BODY()

override;

OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

Õâ¸öÊÇÎÒÃÇ×Ô¼ºµÄNPCÀàµÄÍ·Îļþ£¬ÎÒÃÇÐÂÌí¼ÓÁËÅöײ¼ì²âʼþ£¬ÀïÃæµÄ²ÎÊý¶¼Êǹ̶¨µÄAPIµÄ²ÎÊý£¬Ö¸¶¨ÊÇCollisionÀà±ðµÄÅöײÆ÷²Å»á´¥·¢Õâ¸öÅöײʼþ

ANonePlayerEntity::ANonePlayerEntity(const class FPostConstructInitializeProperties &PCIP) :Super(PCIP) {

// Set this character to call Tick() every frame. You can turn this off to improve PrimaryActorTick.bCanEverTick = true; performance if you don't need it.

}

// Called when the game starts or when spawned void ANonePlayerEntity::BeginPlay() { }

// Called every frame

void ANonePlayerEntity::Tick( float DeltaTime ) { }

// Called to bind functionality to input

void ANonePlayerEntity::SetupPlayerInputComponent(class UInputComponent* InputComponent) { }

void ANonePlayerEntity::Prox_Implementation(AActor * otherActor, UPrimitiveComponent* othrComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) {

//½«ÆäËûÍæ¼Ò½øÈë¸ÃÇøÓòת»¯Îªavatar£¬Èç¹ûÊÇ¿ÕÎïÌåµÄ£¬ÄÇôֱ½Ó·µ»Ø if (Cast(otherActor) == nullptr) {

return;

//Åöײ·¢ÉúÊÂÇ飬µ±Óн»»¥Ê±

Super::SetupPlayerInputComponent(InputComponent); Super::Tick( DeltaTime ); Super::BeginPlay();

ProxSphere = PCIP.CreateDefaultSubobject(this, TEXT(\//·ÅÖÃÔÚ¸ù½ÚµãÏÂÃæ

ProxSphere->AttachTo(RootComponent); //ÉèÖÃÅöײ°ë¾¶

ProxSphere->SetSphereRadius(32.f); //µ±ÓÐÈ˽øÈ¥Åö×²ÇøÓò

ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANonePlayerEntity::Prox); NPCMessage = \£¬ i am Owner\;//ÉèÖÃĬÈϵÄNOC¶Ô»° Sphere\));