UE5

UE Json File 불러와서 UStruct에 저장

우대비 2023. 6. 20. 12:55
반응형

USTRUCT

USTRUCT(BlueprintType)
struct TPSPROJECT_API FSkillInfo 
{
	GENERATED_BODY()
public:
    FString SkillName;
    FString SkillInfo;

    TArray<int> powerValues;
    TArray<float> coolDownValues;

    SkillSectionType SectionType = SkillSectionType::None;
    SkillType skillType = SkillType::None;

    int isAbility = false;
    int point = 0;

	int powerValue = 0;
	int coolDownValue = 0;

};

 

Json File

{
  "FSkillInfo": [
    {
      "SkillName": "SwordProficiency",
      "SkillInfo": "hello",
      "powerValues": [ 10, 25, 50, 70, 100 ],
      "coolDownValues": [],
      "SectionType": "firstSkillSection",
      "SkillType": "swordProficiency",
      "isAbility": "true"
    },

    {
      "SkillName": "bowProficiency",
      "SkillInfo": "hello",
      "powerValues": [ 5, 12, 25, 40, 55 ],
      "coolDownValues": [],
      "SectionType": "thirdSkillSection",
      "SkillType": "bowProficiency",
      "isAbility": "true"
    },

    {
      "SkillName": "gunProficiency",
      "SkillInfo": "hello",
      "powerValues": [ 2, 6, 10, 17, 25 ],
      "coolDownValues": [],
      "SectionType": "secondSkillSection",
      "SkillType": "gunProficiency",
      "isAbility": "true"
    },
    
    {
      "SkillName": "LightningStrike",
      "SkillInfo": "Hello",
      "powerValues": [ 50, 80, 110, 150, 200 ],
      "coolDownValues": [ 10.0, 7.50, 5.0, 3.5, 2.0  ],
      "SectionType": "secondSkillSection",
      "SkillType": "LightningStrike",
      "isAbility": "false"
    },

C++

FString JsonString;
FFileHelper::LoadFileToString(JsonString, *(FPaths::ProjectContentDir() + "\\Assets\\Json\\SkillInfoJson.json"));
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());

if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
{
    TArray<TSharedPtr<FJsonValue>> JsonValueArray = JsonObject->GetArrayField(TEXT("FSkillInfo"));

    for (auto jsonValue : JsonValueArray) {
        TSharedPtr<FJsonObject> jsonValueObject = jsonValue->AsObject();

        FSkillInfo tempSkillInfo;
        tempSkillInfo.SkillName = jsonValueObject->GetStringField(TEXT("SkillName"));
        UKismetSystemLibrary::PrintString(GetWorld(), tempSkillInfo.SkillName);


        tempSkillInfo.SkillInfo = jsonValueObject->GetStringField(TEXT("SkillInfo"));

        TArray<TSharedPtr<FJsonValue>> powerValues = jsonValueObject->GetArrayField(TEXT("powerValues"));
        for (auto powerValue : powerValues) {
            int tempN = FMath::CeilToInt(powerValue.Get()->AsNumber());
            tempSkillInfo.powerValues.Add(tempN);
        }

        TArray<TSharedPtr<FJsonValue>> coolDownValues = jsonValueObject->GetArrayField(TEXT("coolDownValues"));
        for (auto coolDownValue : coolDownValues) {
            tempSkillInfo.coolDownValues.Add(coolDownValue.Get()->AsNumber());
        }

        tempSkillInfo.SectionType = ConvertSectionType(jsonValueObject->GetStringField(TEXT("SectionType")));
        tempSkillInfo.skillType = ConvertSkillType(jsonValueObject->GetStringField(TEXT("SkillType")));
        tempSkillInfo.isAbility = jsonValueObject->GetBoolField(TEXT("isAbility"));
        skillInfos.Add(tempSkillInfo);
    }
}
else
    UKismetSystemLibrary::PrintString(GetWorld(), TEXT("Json Load Failed"));
반응형
LIST