Download

How to trigger an event when a file changes, and get the path to this file?

Hello everyone! I got such a problem. How can I trigger an event when a file changes? I know that I need to use the “DirectoryWatcher”, but I don’t understand how exactly I should do it. What should the constructor look like for everything to work as it should?

devenv_SKOenIwGta.png

In the screenshot you can see my attempts to make this work…



private:
    static FDelegateHandle WatcherHandle;


cpp:



static FDirectoryWatcherModule &DirectoryWatcherModule = FModuleManager::LoadModuleChecked<FDirectoryWatcherModule>(TEXT("DirectoryWatcher"));

FDelegateHandle FMyClass::WatcherHandle = FDelegateHandle();


DirectoryWatcherModule.Get()->RegisterDirectoryChangedCallback_Handle
(
    MY_FOLDER_PATH
    , IDirectoryWatcher::FDirectoryChanged::CreateRaw
    (
        this, &FMyClass::OnProjectDirectoryChanged
    )
    , FMyClass::WatcherHandle
    , IDirectoryWatcher::WatchOptions::IncludeDirectoryChanges
);




void FMyClass::OnProjectDirectoryChanged(const TArray<FFileChangeData> & Data)
{
    // ... do stuff
}


Not working :frowning:

devenv_gMuCtrFgJO.png


You should try create the static handle on global scope and register the callback in a class constructor instead of doing that.

And your directory should be an absolute path, not relative.

I use that code on a few editor extensions and works just fine.

Ok, I’ll try to do it, thanks. )