From 0c90ade61539ca900d28856a5da242cbcba12022 Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 16 Nov 2024 02:22:14 +0000 Subject: [PATCH] Move Widget from PlayerCharacter to PlayerController --- Source/vampires/PlayerCharacter.cpp | 10 ---------- Source/vampires/PlayerCharacter.h | 5 ----- Source/vampires/VampirePlayerController.cpp | 19 +++++++++++++++++++ Source/vampires/VampirePlayerController.h | 15 ++++++++++++++- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index f5682f2..8a1beea 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -54,16 +54,6 @@ APlayerCharacter::APlayerCharacter() void APlayerCharacter::BeginPlay() { Super::BeginPlay(); - - if (PlayerHUD) - { - currentPlayerHUD = UUserWidget::CreateWidgetInstance(*GetWorld(), PlayerHUD, FName("Player HUD")); - - if (currentPlayerHUD) - { - currentPlayerHUD->AddToViewport(); - } - } } void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index ea7758c..a9013e0 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -48,17 +48,12 @@ public: FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f); - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - TSubclassOf PlayerHUD = nullptr; - UPROPERTY(EditAnywhere) UWidgetComponent* HealthBarWidgetComponent; private: FTimerHandle GarlicTimerHandle; - UUserWidget* currentPlayerHUD = nullptr; - public: APlayerCharacter(); diff --git a/Source/vampires/VampirePlayerController.cpp b/Source/vampires/VampirePlayerController.cpp index d0ccaee..69591c7 100644 --- a/Source/vampires/VampirePlayerController.cpp +++ b/Source/vampires/VampirePlayerController.cpp @@ -3,3 +3,22 @@ #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(this, PlayerHUD.Get()); + + if (currentPlayerHUD) + { + currentPlayerHUD->AddToViewport(); + } + } +} + diff --git a/Source/vampires/VampirePlayerController.h b/Source/vampires/VampirePlayerController.h index 756dfc2..e4330a7 100644 --- a/Source/vampires/VampirePlayerController.h +++ b/Source/vampires/VampirePlayerController.h @@ -6,12 +6,25 @@ #include "GameFramework/PlayerController.h" #include "VampirePlayerController.generated.h" +class UHUDWidget; /** * */ -UCLASS() +UCLASS(Abstract) class VAMPIRES_API AVampirePlayerController : public APlayerController { GENERATED_BODY() + +public: + UPROPERTY(EditAnywhere, BlueprintReadWrite) + TSubclassOf PlayerHUD = nullptr; + +private: + + TObjectPtr currentPlayerHUD = nullptr; + +protected: + virtual void OnPossess(APawn* aPawn) override; + };