0

I would like to move an actor to a set of actors placed in the world. The set of actors and the moving actor have collision set up for them. Basically whenever the moving actor reaches one of its destinations the overlapped actor would set a property on the moving object's class that is the actor to which it moves to next.

Here are the important code snippets

Here is the property that should be updated:

UPROPERTY(EditAnywhere, BlueprintReadWrite)
ARAStaticMeshActor* CurrentTarget;

The Tick of the moving actor.

void ARAScrapPart::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (CurrentTarget != nullptr)
    {
        ARAGameState* gameState = GetWorld()->GetGameState<ARAGameState>();

        FVector targetLocation = CurrentTarget->GetActorLocation();
        FVector currentLocation = GetActorLocation();

        UE_LOG(LogTemp, Warning, TEXT("this %X CurrentTarget %X"), this, CurrentTarget);

        if (FGenericPlatformMath::Abs(currentLocation.X - targetLocation.X) > 5.f
            || FGenericPlatformMath::Abs(currentLocation.Y - targetLocation.Y) > 5.f)
        {
            if (targetLocation.X > currentLocation.X)
            {
                currentLocation.X += Speed * DeltaTime;
            }

            if (targetLocation.X < currentLocation.X)
            {
                currentLocation.X -= Speed * DeltaTime;
            }

            if (targetLocation.Y > currentLocation.Y)
            {
                currentLocation.Y += Speed * DeltaTime;
            }

            if (targetLocation.Y < currentLocation.Y)
            {
                currentLocation.Y -= Speed * DeltaTime;
            }

            SetActorLocation(currentLocation);
        }
    }
}

And the most important part, the overlap event:

void ARAStaticMeshActor::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    ARAScrapPart* scrapPart = Cast<ARAScrapPart>(OtherActor);

    if (scrapPart != nullptr)
    {
        ARAGameState* gameState = GetWorld()->GetGameState<ARAGameState>();

        for (size_t i = 0; i < gameState->conveyorFields.Num(); i++)
        {
            if (gameState->conveyorFields[i]->Actor == this)
            {
                if (i != gameState->conveyorFields.Num() - 1)
                {       
                    UE_LOG(LogTemp, Warning, TEXT("OtherActor %X , ScrapPart %X , CurrentTarget %X , nextField %X"), OtherActor, scrapPart, scrapPart->CurrentTarget, gameState->conveyorFields[i + 1]->Actor);

                    scrapPart->CurrentTarget = gameState->conveyorFields[i + 1]->Actor;

                    UE_LOG(LogTemp, Warning, TEXT("After update CurrentTarget %X"), scrapPart->CurrentTarget);
                }
                else
                {
                    scrapPart->Destroy();
                }

                break;
            }
        }
    }
}

As you can see I update the scrapPart->CurrentTarget = gameState->conveyorFields[i + 1]->Actor; target of the moving actor, but yet it doesn't get updated on the class itself.

I have confirmed that the scrapPart (moving part) that I am here trying to modify is the same as the one that Ticks, through UE_LOGs. (Checking the address of the scrapPart and the this in the Tick function is the same.) One additional very interesting behaviour is that in the scope of the overlap function after the modification of the CurrentTarget property, UE_LOG showed an updated value, but the value on the scrapPart's tick function stayed the same.

This is unreal engine 5.

Have anyone encountered something like this?

EDIT: Added the logging parts and also here is the output from the log:

LogTemp: Warning: OtherActor 3587A80 , ScrapPart 3587A80 , CurrentTarget 0 , nextField 3585080
LogTemp: Warning: After update CurrentTarget 3585080
LogTemp: Warning: OtherActor 3587A80 , ScrapPart 3587A80 , CurrentTarget 3585080 , nextField 3585080
LogTemp: Warning: After update CurrentTarget 3585080
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
geo10
  • 366
  • 2
  • 11
  • 1
    I think it might be useful to include the logging code (and what it prints out) as I can't see anything wrong with the code here! – T. Kiley Sep 09 '21 at 17:56
  • I have updated the logging. This is truly amazing to see, it's an anomaly. – geo10 Sep 09 '21 at 18:30
  • That is strange. I would make the field private (with a setter you use here) BlueprintReadOnly, check for writes within ARAScrapPart and see if anything else is trying to change it since when the first overlap is raised it is null, but in the tick it is a third value, suggesting it could have been updated to your thing and then later re-updated to something else. I can't see any reason why it'd be a problem, but do you know why the overlap fires twice? – T. Kiley Sep 10 '21 at 13:44

0 Answers0