Skip to content

Examples

Jonathan Windgassen edited this page Feb 2, 2023 · 3 revisions

UnrealYAML Examples

The UnrealYAML Plugin largely follows the syntax of the yaml-cpp library it is based on. Its documentation can be found here.

Below are some more examples, that show the basic functionality for C++ and Blueprints.

Creating a YAML File

Here, we will create some simple YAML File using UnrealYAML

C++

FYamlNode Node; // Create an empty Node

// Add Attributes by using the subscript operator
// It will automatically convert from and to various types.
Node["message"] = "Hello, World!";
Node["answer"] = 42;
Node["origin"] = FVector(1, 2, 3);  // Accepts Unreal Types

// Create arrays using a TArray or by pushing
FYamlNode Fibonacci(TArray<int32>({0, 1, 1, 2, 3, 5}));
Fibonacci.Push(13);
Fibonacci.Push("The End!");
Fibonacci.SetStyle(EYamlEmitterStyle::Flow); 
Node["fibonacci"] = Fibonacci;

// Accessing and comparing types
check(Node.Type() == EYamlNodeType::Map)
check(Node["fibonacci"].IsSequence())

// Write to file
UYamlParsing::WriteYamlToFile(FPaths::ProjectDir() + "example1.yml", Node);

Blueprint

image

Result

message: Hello, World!
answer: 42
origin: [1, 2, 3]
fibonacci: [0, 1, 1, 2, 3, 5, 13, The End!]

Reading a YAML File

Reading in the file created above, we can read and convert the contents:

C++

FYamlNode Node;
UYamlParsing::LoadYamlFromFile(FPaths::ProjectDir() + "example1.yml", Node);  // Load and Parse from file

// Accessing and converting an attribute
FString Message = Node["message"].As<FString>();
int32 Answer =    Node["answer"].As<int32>();
FVector Origin =  Node["origin"].As<FVector>(FVector::ForwardVector);  // User ForwardVector if the conversion is not possible

// Iterate over Fibonacci's
int32 Sum = 0;
for (const auto& Fib : Node["fibonacci"]) {
    Sum += Fib.As<int32>(0);  // "The End!" cannot be converted, so 0 will be used
}
UE_LOG(LogTemp, Log, TEXT("%d"), Sum)

Blueprint

image

Note: UnrealYAML does not support converting Nodes of type Sequence to an Array of Nodes yet. We use conversion to an Integer Array here instead.

Parsing YAML Files into UStructs

Using the Reflection System that Unreal provides, we can automatically parse the following file:

Name: This is a String
Transform: [[1, 2, 3], [90, -180, 0], [5, 5, 5]]
SomeNumbers: [2, 3, 5, 7, 11, 13, 17, 19]
Substruct:
    Vector: [1, -1, 0]
    Flags:
        - Flag1
        - This is another Flag
        - Even More

into an instance of the following struct:

USTRUCT(BlueprintType)
struct FExampleSubstruct {
    GENERATED_BODY()

    UPROPERTY(BlueprintReadWrite)
    FVector Vector;

    UPROPERTY(BlueprintReadWrite)
    TArray<FString> Properties;
};


USTRUCT(BlueprintType)
struct FExampleStruct {
    GENERATED_BODY()

    UPROPERTY(BlueprintReadWrite)
    FString Name;

    UPROPERTY(BlueprintReadWrite)
    FTransform ComplexProperty;

    UPROPERTY(BlueprintReadWrite)
    TArray<int32> SomeNumbers;

    UPROPERTY(BlueprintReadWrite)
    FExampleSubstruct Substruct;
};

C++

FYamlNode Node;
UYamlParsing::LoadYamlFromFile(FPaths::ProjectDir() + "example2.yml", Node);  // Load and Parse from file

FExampleStruct Struct;
ParseNodeIntoStruct(Node, Struct);

UE_LOG(LogTemp, Log, TEXT("%s"), *Struct.Substruct.Flags[2])

Blueprint

image

⚠️ Note:

  • This does (currently) not work with Structs defined in Blueprints. Unreal will mangle the property names.
  • The Blueprint Node will modify the passed Struct. You cannot use a simple Make <Struct> Node as the input, as the changes can't be accessed again. Use a local variable instead.