Here’s an update.
Current code:
void UMyLibrary::ReimportDT(
UDataTable* DT)
{
FString csvFile = FString("C:\\TestDir\\csvFile.csv");
if (FPaths::FileExists(csvFile))
{
//Print that path exists
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("Path exists"));
//create string variable.
FString FileContent;
FFileHelper::LoadFileToString(FileContent, *csvFile);
//Print if loadfile returns true
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("Load file returned true"));
//Print content of FileContent.
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, FileContent);
//Create table (same as update table?) and save any problems to an array.
TArray<FString> problems = DT->CreateTableFromCSVString(FileContent);
//get length of the problems array.
FString problemsnum = FString::FromInt(problems.Num());
//print Number of problems.
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, problemsnum);
//print each problem
for (int32 Index = 0; Index != problems.Num(); ++Index)
{
FString problem = problems[Index];
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, problem);
}
}
}
With the current setup, it writes to the csvFile and then printing each member of the updated data table, it does indeed import the csv file.
(NewRow_20 is the new member)
However, if I open the data table in the content browser, or click on its tab if it’s already open, the engine crashes.
If I change -
FFileHelper::LoadFileToString(FileContent, *csvFile);
//Print if loadfile returns true
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("Load file returned true"));
To -
if (FFileHelper::LoadFileToString(FileContent, *csvFile)) {
//Print if loadfile returns true
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("Load file returned true"));
}
Then the engine does not crash, but does also not reimport the DataTable.
I feel like I am getting close, but am obviously missing something. There’s a “ReimportDataTableFactory”, with the following code:
#include "CoreMinimal.h"
#include "UObject/ObjectMacros.h"
#include "Factories/CSVImportFactory.h"
#include "EditorReimportHandler.h"
#include "ReimportDataTableFactory.generated.h"
UCLASS()
class UNREALED_API UReimportDataTableFactory : public UCSVImportFactory, public FReimportHandler
{
GENERATED_UCLASS_BODY()
//~ Begin FReimportHandler Interface
virtual bool FactoryCanImport( const FString& Filename ) override;
virtual bool CanReimport( UObject* Obj, TArray<FString>& OutFilenames ) override;
virtual void SetReimportPaths( UObject* Obj, const TArray<FString>& NewReimportPaths ) override;
virtual EReimportResult::Type Reimport( UObject* Obj ) override;
virtual int32 GetPriority() const override;
//~ End FReimportHandler Interface
};
Do I need to use that?
Then there is also the matter of the directory. Right now it’s a hard coded string. The first time I reimported the data table through the content browser, I had to select the file but after that it always uses the same file. How can I get that directory?
There’s the “DataTableCSV” class, which has the following function and data:
class FDataTableImporterCSV
{
public:
FDataTableImporterCSV(UDataTable& InDataTable, FString InCSVData, TArray<FString>& OutProblems);
~FDataTableImporterCSV();
bool ReadTable();
private:
UDataTable* DataTable;
FString CSVData;
TArray<FString>& ImportProblems;
};
Where I think the CSVData is the directory.