Move Widget from PlayerCharacter to PlayerController

This commit is contained in:
baz 2024-11-16 02:22:14 +00:00
parent 6106a2dae6
commit 0c90ade615
4 changed files with 33 additions and 16 deletions

View File

@ -54,16 +54,6 @@ APlayerCharacter::APlayerCharacter()
void APlayerCharacter::BeginPlay() void APlayerCharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
if (PlayerHUD)
{
currentPlayerHUD = UUserWidget::CreateWidgetInstance(*GetWorld(), PlayerHUD, FName("Player HUD"));
if (currentPlayerHUD)
{
currentPlayerHUD->AddToViewport();
}
}
} }
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)

View File

@ -48,17 +48,12 @@ public:
FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f); FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f);
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<UUserWidget> PlayerHUD = nullptr;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UWidgetComponent* HealthBarWidgetComponent; UWidgetComponent* HealthBarWidgetComponent;
private: private:
FTimerHandle GarlicTimerHandle; FTimerHandle GarlicTimerHandle;
UUserWidget* currentPlayerHUD = nullptr;
public: public:
APlayerCharacter(); APlayerCharacter();

View File

@ -3,3 +3,22 @@
#include "VampirePlayerController.h" #include "VampirePlayerController.h"
#include "EXPComponent.h"
#include "Blueprint/UserWidget.h"
#include "Widgets/HUDWidget.h"
void AVampirePlayerController::OnPossess(APawn* aPawn)
{
Super::OnPossess(aPawn);
if (PlayerHUD)
{
currentPlayerHUD = CreateWidget<UHUDWidget, AVampirePlayerController*>(this, PlayerHUD.Get());
if (currentPlayerHUD)
{
currentPlayerHUD->AddToViewport();
}
}
}

View File

@ -6,12 +6,25 @@
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "VampirePlayerController.generated.h" #include "VampirePlayerController.generated.h"
class UHUDWidget;
/** /**
* *
*/ */
UCLASS() UCLASS(Abstract)
class VAMPIRES_API AVampirePlayerController : public APlayerController class VAMPIRES_API AVampirePlayerController : public APlayerController
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<UHUDWidget> PlayerHUD = nullptr;
private:
TObjectPtr<UHUDWidget> currentPlayerHUD = nullptr;
protected:
virtual void OnPossess(APawn* aPawn) override;
}; };