新建GameMode类继承自GameMode.base
MyGameMode.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "Engine/DataTable.h"
#include "MyGameModeBase.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FUserInfo
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Category = "UserInfo")
FString Name;
UPROPERTY(BlueprintReadWrite, Category = "UserInfo")
FString ThrearType;
UPROPERTY(BlueprintReadWrite, Category = "UserInfo")
int Score;
UPROPERTY(BlueprintReadWrite, Category = "UserInfo")
FDateTime SubmitTime;
};
USTRUCT(BlueprintType)
struct FUserInfoString
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Category = "UserInfo")
TArray<FString>InfoString;
};
UCLASS()
class TEMPLATE_THREAT_API AMyGameModeBase : public AGameModeBase
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "MyGameMode")
void ExportUserInfo(TArray<FUserInfoString>UserInfoString, TArray<FString>Header);
};
MyGameMode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameModeBase.h"
#include "FileHelper.h"
#include "Paths.h"
void AMyGameModeBase::ExportUserInfo(TArray<FUserInfoString>UserInfoString, TArray<FString>Header)
{
FString ExportedText;
//Write the header
ExportedText += TEXT("---");
for (auto& Str : Header)
{
ExportedText += TEXT(",");
ExportedText += Str;
}
ExportedText += TEXT("\n");
//Write the body
for (int32 Index = 0; Index != UserInfoString.Num(); ++Index)
{
ExportedText += FString::FromInt(Index + 1);
for (auto& Str :UserInfoString[Index].InfoString)
{
ExportedText += TEXT(",");
ExportedText += TEXT("\"");
ExportedText += Str.Replace(TEXT("\""), TEXT("\"\""));
ExportedText += TEXT("\"");
}
ExportedText += TEXT("\n");
}
//Save in utf-8
FFileHelper::SaveStringToFile(ExportedText, *(FPaths::ProjectDir() + "Score.csv"), FFileHelper::EEncodingOptions::ForceUTF8);
}
蓝图类SaveGame
BP_GameMode(继承自MyGameMode)
使用:
1.示例类似一个考核系统,首先将需要保存的信息先通过savegame存储(调用SubmitScore),比如示例中保存的是姓名、科目、成绩、提交时间 ;
2.在考核结束后,调用ExportData即可,在本地会出现一个Score数据表格记录成绩信息(FPaths::ProjectDir() + "Score.csv");
3.需要将信息传送到服务器端的话,启用VaRest插件跟着文档操作即可。
https://blog.csdn.net/qq_40997637/article/details/94591111
- UE4从服务器下载、向服务发送Json数据
- 链接: