Hi
What I implemented is custom grid map, also I have implemented Theta* algo for pathfinding which returns vector of points which unit should follow. What would be best way to implement movement of this character?
Additional requirements are that during movement path can be updated. what I tried is something like this
void AMyPawn::OnPathFound(const TArray<FVector> waypoints, bool sucess)
{
FScopeLock Lock(&PathMutex);
pathToFollow.Empty();
pathToFollow = waypoints;
waypoint = pathToFollow[0];
waypointIndex = 0;
pathUpdated = true;
}
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (pathUpdated)
{
UpdatePath();
}
if (movePawn)
{
UpdatePawnLocation(DeltaTime);
}
}
Reason why OnPathFound is that this path is calculated by other thread and then returned by Promise to this function
Promise->GetFuture().Next([unitToMove](const TArray<FVector> path)
{
if (path.Num())
{
unitToMove->OnPathFound(path, true);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("There are no points in path"));
}
});
Problem is that OnPathFound and TIck can cause data races, so I put Lock into Tick, but this way Tick is never unlocked and other thread can never update this path.
Would it be better solution to use Timers? when OnPathFound is call it stops timer, and after updating timer is resumed?