Unreal Engine FDirectoryWatcherModule catching multiple events per file operation

27 Views Asked by At

I followed this post to implement a delegate on a directory that notifies me when a change has happened (add file, delete file or remove file).

This works fine, though the issue is that for each operation, multiple events are triggered. In the Data array containing information from the operation, I see the following behaviour:

  • When adding an image, Data size is 5 and contains one FCA_Added and 4 FCA_Modified.
  • When deleting an image, Data size is 1 and contains a FCA_Removed.
  • When adding multiple images (for example 2 images), Data size is 10 and contains one FCA_Added and 4 FCA_Modified for the first image and one FCA_Added and 4 FCA_Modified for the second image.

Note: Sometimes adding an image will result in fewer FCA_Modified. For example, in another PC, each image results in one FCA_Added and two (rather than four) FCA_Modified.

In any case, the issue remains that Data is providing a large number of events for each operation. If I want to control this with a switch for example, it means each image will trigger my UpdateDynamicMaterialArray multiple times when only one call is required per file.

I attached an image with the console output for a 2 file addition operation. enter image description here

Edit: To clarify the console output image, Element action 1 is a FCA_Added and an Element action 2 is an FCA_Modified.

The function I use to control the events:

/**
 * Will trigger when a change (addition, replacement, deletion) is performed on ResourcesDirPath
 * @param Data Array of type FFileChangeData indicating the nature of the change observed
 */
void AMyController::OnProjectDirectoryChanged(const TArray<FFileChangeData>& Data) {
    UE_LOG(LogTemp, Warning, TEXT("Data num: %d"), Data.Num());
    for (FFileChangeData Element : Data) {
        UE_LOG(LogTemp, Warning, TEXT("Element filename: %s"), *Element.Filename);
        UE_LOG(LogTemp, Warning, TEXT("Element action: %d"), Element.Action);
        switch (Element.Action) {
            case FFileChangeData::FCA_Added:
                UpdateDynamicMaterialArray(&Element.Filename, FFileChangeData::FCA_Added);
                //MyReferenceManager->MyHUD->AddTile();
                break;
            case FFileChangeData::FCA_Modified:
                UpdateDynamicMaterialArray(&Element.Filename, FFileChangeData::FCA_Modified);
                //MyReferenceManager->MyHUD->RefreshTile();
                break;
            case FFileChangeData::FCA_Removed:
                UpdateDynamicMaterialArray(&Element.Filename, FFileChangeData::FCA_Removed);
                //MyReferenceManager->MyHUD->RemoveTile();
                break;
            default: ;
        }
    }
}

FFileChangeData docs and EFileChangeAction did not help.

Why does one operation result in multiple events of different type? I suspect this is OS related (I use Windows) and that there is not much I can do. Is this true?

0

There are 0 best solutions below