Bugfix Crash on Second Play Through

This commit is contained in:
Philip W 2024-02-07 16:32:06 +00:00
parent f239743e7f
commit 9b971d1ae9
2 changed files with 32 additions and 23 deletions

View File

@ -144,30 +144,11 @@ void ASeagullGameCharacter::StartGame()
}
GetWorld()->GetTimerManager().SetTimer(GameTimerHandle, this, &ASeagullGameCharacter::EndGame, GameTime, false);
FTimerHandle CravingTimerHandle;
GetWorld()->GetTimerManager().SetTimer(CravingTimerHandle, [this]()
if (GetWorld()->GetTimerManager().IsTimerActive(CravingTimerHandle))
{
TSubclassOf<AItemActor> ItemActor = ItemActors[FMath::RandRange(0, ItemActors.Num() - 1)];
if (IsValid(CravingItemActor)) CravingItemActor->Destroy();
CravingItemActor = GetWorld()->SpawnActor(ItemActor);
CurrentCraving = Cast<AItemActor>(CravingItemActor)->ItemType;
CravingItemActor->SetActorEnableCollision(false);
if (!IsValid(John)) return;
UStaticMeshComponent* MeshComponent = Cast<UStaticMeshComponent>(John->GetComponentByClass(UStaticMeshComponent::StaticClass()));
if (!MeshComponent->HasAnySockets()) return;
CravingItemActor->AttachToComponent(MeshComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, "CravingSocket");
FString CravingString = UEnum::GetValueAsString(CurrentCraving);
CravingString.Split(FString("::"), nullptr, &CravingString);
for (int i = 0; i < CravingString.Len(); i++)
{
if (CravingString[i] >= 'A' && CravingString[i] <= 'Z')
{
CravingString.InsertAt(i, " ");
i++;
}
}
CravingItemName = CravingString;
}, 60, true, 2);
GetWorld()->GetTimerManager().ClearTimer(CravingTimerHandle);
}
GetWorld()->GetTimerManager().SetTimer(CravingTimerHandle, this, &ASeagullGameCharacter::SetCravingItem, 60, true, 2);
}
void ASeagullGameCharacter::EndGame()
@ -205,6 +186,30 @@ void ASeagullGameCharacter::Scroll(const FInputActionValue& Value)
}
}
void ASeagullGameCharacter::SetCravingItem()
{
const TSubclassOf<AItemActor> ItemActor = ItemActors[FMath::RandRange(0, ItemActors.Num() - 1)];
if (IsValid(CravingItemActor)) CravingItemActor->Destroy();
CravingItemActor = GetWorld()->SpawnActor(ItemActor);
CurrentCraving = Cast<AItemActor>(CravingItemActor)->ItemType;
CravingItemActor->SetActorEnableCollision(false);
if (!IsValid(John)) return;
UStaticMeshComponent* MeshComponent = Cast<UStaticMeshComponent>(John->GetComponentByClass(UStaticMeshComponent::StaticClass()));
if (!MeshComponent->HasAnySockets()) return;
CravingItemActor->AttachToComponent(MeshComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, "CravingSocket");
FString CravingString = UEnum::GetValueAsString(CurrentCraving);
CravingString.Split(FString("::"), nullptr, &CravingString);
for (int i = 0; i < CravingString.Len(); i++)
{
if (CravingString[i] >= 'A' && CravingString[i] <= 'Z')
{
CravingString.InsertAt(i, " ");
i++;
}
}
CravingItemName = CravingString;
}
void ASeagullGameCharacter::PickupItem()
{
OnPlayerPickupItem.Broadcast();

View File

@ -100,6 +100,8 @@ public:
UFUNCTION(Category = "General")
void Scroll(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Item")
void SetCravingItem();
UFUNCTION(BlueprintCallable, Category = "Item")
void PickupItem();
UFUNCTION(BlueprintCallable, Category = "Item")
@ -126,6 +128,8 @@ public:
FString CravingItemName;
UPROPERTY(BlueprintReadOnly, Category = "Item")
FString HeldItemName;
UPROPERTY(BlueprintReadOnly, Category = "Item")
FTimerHandle CravingTimerHandle;
UPROPERTY(BlueprintReadOnly, Category = "John")
AActor* John;