something is nearby
When the player is near the
When the player is near the NPC sphere collision volume, diNPC sphere collision volume, display a message to thesplay a message to the HUD that alerts the player about what the
HUD that alerts the player about what the NPC is saying.NPC is saying. This is the
This is the complete implementation ofcomplete implementation of ANPC::Prox_Implementation ANPC::Prox_Implementation::
void ANPC::Prox_Implementation( AActor* OtherActor, void ANPC::Prox_Implementation( AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult )
const FHitResult & SweepResult ) {
{
// if
// if the overlapped the overlapped actor is actor is not the not the player,player, // you
// you should just should just simply retusimply return from rn from the functionthe function if( Ca
if( Cast<AAvatar>( st<AAvatar>( OtherActor OtherActor ) == ) == nullptr )nullptr ) { { return; return; } } APlayerController*
APlayerController* PController PController = = GetWorld()-GetWorld()- >GetFirstPlayerController();
>GetFirstPlayerController(); if(
if( PController PController )) {
{
AMyHUD
AMyHUD * * hud hud = = Cast<AMyHUD>( Cast<AMyHUD>( PController->GetHUD() PController->GetHUD() );); hud->addMessage(
hud->addMessage( Message( Message( NpcMessage, NpcMessage, 5.f, 5.f, FColor::White FColor::White ) ) );); }
} } }
The
Thefifirst thing we do in this function is to castrst thing we do in this function is to cast OtherActorOtherActor (the thing that came near (the thing that came near
the NPC) to
the NPC) to AAvatar AAvatar. The cast succeeds (and is not. The cast succeeds (and is notnullptrnullptr) when) whenOtherActorOtherActor is an is an
AAvatar
AAvatar object. We get the HUD object (which happens to object. We get the HUD object (which happens to be attached to the playerbe attached to the player controller) and pass a message from the NPC to the HUD. The message is displayed controller) and pass a message from the NPC to the HUD. The message is displayed whenever the player is within the red bounding sphere surrounding the NPC. whenever the player is within the red bounding sphere surrounding the NPC.
Owen's greeting Owen's greeting
Exercises
Exercises
1.
1. Add Add aaUPROPERTYUPROPERTY function name for the NPC's name so that the function name for the NPC's name so that the name of thename of the NPC is editable in blueprints, similar to the message that the NPC has for the NPC is editable in blueprints, similar to the message that the NPC has for the player. Show the NPC's name in the output.
player. Show the NPC's name in the output. 2.
2. Add Add aaUPROPERTYUPROPERTY function (type function (typeUTexture2D*UTexture2D*) for the NPC's face texture.) for the NPC's face texture. Draw the NPC's face beside its message in the output.
Draw the NPC's face beside its message in the output. 3.
Solutions
Solutions
Add this property to the
Add this property to the ANPC ANPC class: class:
// This is the NPC's name // This is the NPC's name
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage) FString name;
FString name;
Then, in
Then, in ANPC::Prox_Implementation ANPC::Prox_Implementation, change the string passed to the HUD to:, change the string passed to the HUD to:
name + FString(": ") + message name + FString(": ") + message
This way, the NPC's name will be attached to the
This way, the NPC's name will be attached to the message.message. Add
Add thisthis property to the property to the ANPC ANPC class: class:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage) UTexture2D* Face;
UTexture2D* Face;
Then you can select face icons to be attached to the
Then you can select face icons to be attached to the NPC's face in blueprints.NPC's face in blueprints. Attach a texture to your
Attach a texture to your struct Messagestruct Message::
UTexture2D* tex; UTexture2D* tex;
To render these icons, you need to add a call to
To render these icons, you need to add a call to DrawTexture()DrawTexture() with the right with the right texture passed in to it:
texture passed in to it:
DrawTexture( messages[c].tex, x, y, messageH, messageH, 0, 0, 1, 1 DrawTexture( messages[c].tex, x, y, messageH, messageH, 0, 0, 1, 1
); );
Be sure to check whether the texture
Be sure to check whether the texture is valid before you render it. The icons shouldis valid before you render it. The icons should look similar to what is shown here, at the top of the
This is how a function to draw the player's remaining health in a bar will look: This is how a function to draw the player's remaining health in a bar will look:
void AMyHUD::DrawHealthbar() void AMyHUD::DrawHealthbar() {
{ //
// Draw Draw the the healthbar.healthbar. AAvatar
AAvatar *avatar *avatar = = Cast<AAvatar>(Cast<AAvatar>( UGameplayStatics::GetPlay
UGameplayStatics::GetPlayerPawn(GetWorld(), 0) erPawn(GetWorld(), 0) );); float
float barWidth=200, barWidth=200, barHeight=50, barHeight=50, barPad=12, barPad=12, barMargin=50;barMargin=50; float
float percHp percHp = a= avatar->Hp vatar->Hp / av/ avatar->MaxHp;atar->MaxHp; DrawRect( FLinea
DrawRect( FLinearColor( 0, rColor( 0, 0, 0, 1 0, 0, 1 ), Canvas->SizeX ), Canvas->SizeX - barWidth - barWidth -- barPad - barMargin, Canvas->SizeY - barHeight - barPad -
barPad - barMargin, Canvas->SizeY - barHeight - barPad - barMargin, barWidth + 2*barPad, barHeight + 2*barPad ); barMargin, barWidth + 2*barPad, barHeight + 2*barPad ); DrawRect( F
DrawRect( FLinearColor( 1LinearColor( 1-percHp, percHp-percHp, percHp, 0, , 0, 1 ), 1 ), Canvas->SizeXCanvas->SizeX - barWidth - barMargin, Canvas->SizeY - barHeight - barMargin, - barWidth - barMargin, Canvas->SizeY - barHeight - barMargin, barWidth*percHp, barHeight ); barWidth*percHp, barHeight ); } }
Summary
Summary
In this chapter, we went through a lot
In this chapter, we went through a lot of material. We showed you how to create aof material. We showed you how to create a character and display it on the
character and display it on the screen, control your character with axis bindings, andscreen, control your character with axis bindings, and create and display NPCs that can post messages to the HUD.
create and display NPCs that can post messages to the HUD.
In the upcoming chapters, we will develop our game further by adding an
In the upcoming chapters, we will develop our game further by adding an InventoryInventory System and Pickup Items
System and Pickup Items in in Chapter 10Chapter 10, as well as the code and the concept to account, as well as the code and the concept to account for what the player is carrying. Before we do that, though, we will do an in-depth for what the player is carrying. Before we do that, though, we will do an in-depth exploration of some of the UE4 container types in
exploration of some of the UE4 container types in Chapter 9Chapter 9,, Templates and CommonlyTemplates and Commonly Used Containers