I am in a pickle. My file path’s are valid to save a string to a text file, but no matter what, the operation fails. Nothing gets written and it returns false. When using the IFileHandle write method, everything crashes instead. The files involved are simply 2 text files, that I verify are valid paths before operating on them. I’m really confused as to why nothing seems to work when using FFileHelper::SaveStringToFile. I know I have it repeated multiple times, but none of them are writing.
//.h file
UENUM(BlueprintType) //"BlueprintType" is essential to include
enum class Achievements : uint8
{
CrabSlayer UMETA(DisplayName = "CrabSlayer"),
Rain UMETA(DisplayName = "Rain")
};
//
//
USTRUCT(BlueprintType)
struct FAchievementData : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
Achievements Enum;
UPROPERTY(BlueprintReadWrite)
FString Name;
UPROPERTY(BlueprintReadWrite)
FString How;
UPROPERTY(BlueprintReadWrite)
UPaperSprite* Sprite;
};
UCLASS()
class PROTOTYPE_API UAchievementFile2 : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
//UAchievementFile2();
//UFUNCTION(BlueprintCallable, Category = "File", meta = (Keywords = "Initialize"))
// static void Init();
// functions for use
UFUNCTION(BlueprintCallable, Category = "File", meta = (Keywords = "AchievementUnlocked"))
static bool AchievementUnlocked(FAchievementData data, Achievements value, AActor* actor);
UFUNCTION(BlueprintCallable, Category = "File", meta = (Keywords = "AchievementMissed"))
static void AchievementMissed(Achievements value);
UFUNCTION(BlueprintCallable, Category = "File", meta = (Keywords = "UpdateAchievementList"))
static void UpdateAchievementList();
private:
//
static const FString EnumToString(const TCHAR* Enum, Achievements EnumValue);
template <typename ObjClass>
static ObjClass* LoadObjFromPath(const FName& Path);
};
//.cpp
bool UAchievementFile2::AchievementUnlocked(FAchievementData data, Achievements value, AActor* actor)
{
FString path(FPaths::GameDir() + "Content/Data/List.uasset");
if (FPaths::FileExists(path))
{
FString AchievementFile = FPaths::GameDir() + "AcheivementList.txt";
FString CurrentRunFile = FPaths::GameDir() + "CurrentAchievements.txt";
if (FPaths::FileExists(AchievementFile) && FPaths::FileExists(CurrentRunFile))
{
// open the achievement file, Find the appropriate Enum:
IPlatformFile& file = FPlatformFileManager::Get().GetPlatformFile();
IFileHandle* Currenthandle = file.OpenWrite(*CurrentRunFile, true, true);
IPlatformFile& fileRead = FPlatformFileManager::Get().GetPlatformFile();
IFileHandle* Achievehandle = fileRead.OpenRead(*AchievementFile, true);
FString time;
FString FileData;
// loading our file to check for contains
FFileHelper::LoadFileToString(FileData, *CurrentRunFile);
// converting enum to Strong
const FString enumString = EnumToString(TEXT("Achievements"), value);
// checks if in the current achievements whether our enum is in there
if (FileData.Contains(*enumString, ESearchCase::IgnoreCase))
{
//dont do anything
}
else
{
FName ConvertedFString = FName(*enumString);
//FAchievementData* data = AchievementList->FindRow<FAchievementData>(ConvertedFString, TEXT("FindingRowForData"));
IFileManager& FileManager = IFileManager::Get();
FString DiskFilename = FileManager.GetFilenameOnDisk(*CurrentRunFile);
bool outcome = FFileHelper::SaveStringToFile((data.Name) + FString("\n")
, *CurrentRunFile);
outcome = FFileHelper::SaveStringToFile((data.Name) + FString("\n")
, *DiskFilename);
outcome = FFileHelper::SaveStringToFile((data.Name) + FString("\n")
, *FString("CurrentAchievements.txt"));
outcome = FFileHelper::SaveStringToFile((data.Name) + FString("\n")
, *(FPaths::ConvertRelativePathToFull(CurrentRunFile)));
Achievehandle->Write((const uint8*)TCHAR_TO_ANSI(*(data.Name + FString("\n")))
, data.Name.Len() + 1);
float realtimeSeconds = UGameplayStatics::GetRealTimeSeconds(actor->GetWorld());
FTimespan timeSpan(/*Hours*/(int)realtimeSeconds / 360,/*Minutes*/((int)realtimeSeconds % 360) / 60,
((int)realtimeSeconds % 60) /*Seconds*/);
time = timeSpan.ToString();
Achievehandle->Write((const uint8*)TCHAR_TO_ANSI(*(time + FString("\n\n")))
, data.Name.Len() + 2);
}
FDateTime temp;
time = temp.Now().ToString();
FFileHelper::LoadFileToString(FileData, *CurrentRunFile);
if (!FileData.Contains(*enumString, ESearchCase::IgnoreCase))
{
FFileHelper::SaveStringToFile(enumString + FString(" : ") + time + FString("\n"), *AchievementFile, FFileHelper::EEncodingOptions::AutoDetect,
&IFileManager::Get(), FILEWRITE_Append);
}
delete Currenthandle;
delete Achievehandle;
return true;
//}
//return false;
}
else
{
return false;
}
}
else
{
return false;
}
}