to UStruct in C++

Hi,

I tried to figure it out on my own, but it appears my knowledge is subzero on C++ for Unreal…

I would like to read out a file, which contains the following information:

{
    "Level": {
            "Name": "Level001",
            "Description": "This is the introductory level with full support."
   }
}

I do this using the following code:

FLevelInfo LevelInfo;

	FString JsonString = ReadFile(Path);
	TSharedPtr<FJsonObject> JsonObject;
	TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(JsonString);
	UE_LOG(LogTemp, Warning, TEXT("Load  %s"), *Path);
	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
		// This nicely shows the dump of the entire  file
		UE_LOG(LogTemp, Warning, TEXT("Level  %s"), *JsonString);
		
		// Up until this point all goes as planned.

		// This is supposed to parse the  String into the LevelInfo UStruct
		FJsonObjectConverter::JsonObjectStringToUStruct(*JsonString, &LevelInfo, 0, 0);

		// I would expect the variable des to contain the Description text but it is empty...
		FString des = LevelInfo.Description;
		UE_LOG(LogTemp, Warning, TEXT("Description %s"), *des);

	}

So, instead of reading out the information from the JSonObject into the LevelInfo UStruct, nothing is happening…

This is the UStruct:

USTRUCT(BlueprintType)
struct FLevelInfo
{
		GENERATED_BODY()

public:
	UPROPERTY(BlueprintReadWrite, Category = "Level")
		FString Name;

	UPROPERTY(BlueprintReadWrite, Category = "Level")
		FString Description;
};

I kept this experiment simple to understand how to do this. However, even this simple UStruct cannot be read using this method…

Any ideas what I am doing wrong?

Thanks!

Seems like a related question to this: How to obtain a ustruct from a json with an array - Programming & Scripting - Epic Developer Community Forums. The structs mentioned are different, but it parses a to a pre-defined USTRUCT.

Hmm do you plan to use this just for level meta data? is it intended for server use or soemthing? because there might be some alternative way that embeds level meta data to level it self

Hi all!

Thanks for your answers. In the mean time I found out the root of the problem. I had a small syntax error in my which resulted in the code not being able to load it into the UStruct. I added “Level:” as a top struct, but did not reflect that in the UStructs. Removing “Level:” fixed the issue.

Thanks for this tool, I noticed this issue:

https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1678755-easy-way-to-work-with--with-c

The goal of the is to dynamically load the level settings along with a bunch of asset parameters. This way, I can easily adjust scenarios without having to rewrite code.

A bit like Data Driven Gameplay:

but then with also loading the actual data dynamically.

It now is working like a charm :slight_smile:

Thanks again!

Ah ok ^^ i was thinking about using world settings but if level reuse then it not it. If i not mistaken there Game Mode accepts parameter from level url, but i didn’t explore it myself

If you solve it i mark it as resolved