Hey there,
Im trying to make a game which involves picking up power ups. I have gotten almost all the functionality to work apart from the replication on the client side. When the server character picks up an item, it is destroyed. However when a client character picks up a power up it stays. Below i have included what i think is every important line for replication from the header file and CPP file so you can see my code. The character class call the Glowing Object function RemoveObject();
How can i make it that when the client picks up an object, the object is also destroyed? where am i going wrong?
GlowingObject.h
class MazeGame_API AGlowingObject : public AActor
public:
void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override;
protected:
UPROPERTY(ReplicatedUsing = OnRep_IsActive)
bool bIsActive;
UPROPERTY(ReplicatedUsing = OnRep_IsAlive)
bool bIsAlive = true;
UFUNCTION()
virtual void OnRep_IsActive();
UFUNCTION()
virtual void OnRep_IsAlive();
public:
UFUNCTION(Blueprintpure)
bool IsActive();
UFUNCTION(BlueprintCallable)
void setActive(bool NewState);
UFUNCTION(BlueprintCallable)
void RemoveObject();
UFUNCTION(BlueprintCallable)
void setAlive(bool NewState);
UFUNCTION(Blueprintpure)
bool IsAlive();
};
GlowingObject.cpp
AGlowingObject::AGlowingObject() {
bReplicates = true;
PrimaryActorTick.bCanEverTick = true;
bReplicateMovement = true;
PickupMesh = CreateDefaultSubobject(TEXT(“PickupMesh”));
BT_Pickup = CreateDefaultSubobject(TEXT(“BTPickUp”));
BT_Pickup->SetGenerateOverlapEvents(true);
BT_Pickup->OnComponentBeginOverlap.AddDynamic(this, &AGlowingObject::inGlowingRadius);
BT_Pickup->OnComponentEndOverlap.AddDynamic(this, &AGlowingObject::notInGlowingRadius);
}
void AGlowingObject::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
if (bIsAlive == false) {
Destroy();
}
bool AGlowingObject::IsActive() {
return bIsActive;
}
bool AGlowingObject::IsAlive() {
return bIsAlive;
}
void AGlowingObject::setActive(bool NewState) {
if (Role == ROLE_Authority) {
bIsActive = NewState;
}
}
void AGlowingObject::setAlive(bool NewState) {
bIsAlive = NewState;
}
void AGlowingObject::RemoveObject() {
setAlive(false);
}
void AGlowingObject::OnRep_IsActive() {
}
void AGlowingObject::OnRep_IsAlive() {
if (Role != ROLE_Authority) {
Destroy();
}
if (Role == ROLE_Authority) {
Destroy();
}
}
void AGlowingObject::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AGlowingObject, bIsAlive);
DOREPLIFETIME(AGlowingObject, bIsActive);
}