44 - Variable Replication

This commit is contained in:
Kingsmedia 2022-04-30 12:26:25 +02:00
parent 7acda3fe1c
commit fe64e11281
4 changed files with 78 additions and 7 deletions

View File

@ -3,10 +3,12 @@
#include "BlasterCharacter.h" #include "BlasterCharacter.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/WidgetComponent.h" #include "Components/WidgetComponent.h"
#include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h" #include "GameFramework/SpringArmComponent.h"
#include "Net/UnrealNetwork.h"
ABlasterCharacter::ABlasterCharacter() ABlasterCharacter::ABlasterCharacter()
@ -29,16 +31,21 @@ ABlasterCharacter::ABlasterCharacter()
OverheadWidget->SetupAttachment(RootComponent); OverheadWidget->SetupAttachment(RootComponent);
} }
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
}
void ABlasterCharacter::BeginPlay() void ABlasterCharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
} }
void ABlasterCharacter::Tick(float DeltaTime) void ABlasterCharacter::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
} }
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
@ -83,3 +90,32 @@ void ABlasterCharacter::LookUp(float Value)
{ {
AddControllerPitchInput(Value); AddControllerPitchInput(Value);
} }
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}

View File

@ -15,7 +15,7 @@ public:
ABlasterCharacter(); ABlasterCharacter();
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -33,6 +33,13 @@ private:
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UWidgetComponent* OverheadWidget; class UWidgetComponent* OverheadWidget;
public:
UPROPERTY(ReplicatedUsing = OnRep_OverlappingWeapon)
class AWeapon* OverlappingWeapon;
UFUNCTION()
void OnRep_OverlappingWeapon(AWeapon* LastWeapon);
public:
void SetOverlappingWeapon(AWeapon* Weapon);
}; };

View File

@ -39,6 +39,7 @@ void AWeapon::BeginPlay()
AreaSphere->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); AreaSphere->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
AreaSphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap); AreaSphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
AreaSphere->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnSphereOverlap); AreaSphere->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnSphereOverlap);
AreaSphere->OnComponentEndOverlap.AddDynamic(this, &AWeapon::OnSphereEndOverlap);
} }
if (PickupWidget) if (PickupWidget)
@ -55,8 +56,26 @@ void AWeapon::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent,
const FHitResult& SweepResult) const FHitResult& SweepResult)
{ {
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor); ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor);
if (BlasterCharacter && PickupWidget) if (BlasterCharacter)
{ {
PickupWidget->SetVisibility(true); BlasterCharacter->SetOverlappingWeapon(this);
}
}
void AWeapon::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor);
if (BlasterCharacter)
{
BlasterCharacter->SetOverlappingWeapon(nullptr);
}
}
void AWeapon::ShowPickupWidget(bool bShowWidget)
{
if (PickupWidget)
{
PickupWidget->SetVisibility(bShowWidget);
} }
} }

View File

@ -22,7 +22,8 @@ class BLASTER_API AWeapon : public AActor
public: public:
AWeapon(); AWeapon();
void ShowPickupWidget(bool bShowWidget);
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -35,6 +36,14 @@ protected:
bool bFromSweep, bool bFromSweep,
const FHitResult& SweepResult const FHitResult& SweepResult
); );
UFUNCTION()
virtual void OnSphereEndOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex
);
private: private:
UPROPERTY(VisibleAnywhere, Category="Weapon Properties") UPROPERTY(VisibleAnywhere, Category="Weapon Properties")