Unreal Engine (UE4) Custom Blueprint Type Converter

Preface

In blueprint development, many people should have encountered the need to print integer data to the screen. We know that the blueprint output log node can only output string type data. If you want to output integer data, this is not allowed from the perspective of strongly typed language features. But in actual use, blueprints only need to drag and drop to complete the operation. This is why blueprints are easy to learn.

Figure 1

In fact, this is not a function that should be taken for granted. The main reason why blueprints are always said to be simple is that the designer has thought of all your needs, making you feel that these are all taken for granted when using them. The advantage is that it is simpler, but the disadvantage is that you will become more and more dependent on these designers.

If I add my own type, can it be converted into a string? Or even other types? Of course it is possible. In fact, if we hover the mouse over the conversion node, you will find the secret!

Figure 2 (Source code location prompt of the node)

From the figure, you can see the source code location of the node, jump to the source code

Figure 3 (Example header file part)

Steps

1. Create a blueprint function library class (must be completed in the blueprint function library class)

2. Design a custom type (the type is arbitrary, must be recognizable by the blueprint, and declared in the header file), taking the custom structure as an example.

1USTRUCT(BlueprintType)
2struct FStrawberry
3{
4    GENERATED_BODY()
5 
6    UPROPERTY(BlueprintReadWrite, EditAnywhere)
7    FString MyName;
8     
9};

3. Write the conversion function (The function needs to be added to the blueprint function library)

Code in the header file

1UFUNCTION(BlueprintPure, meta=(DisplayName = "ToString (Strawberry)", CompactNodeTitle = "->", BlueprintAutocast), Category="Utilities|String")
2static FString Conv_StrawberryToString(FStrawberry InStrawberry);

Code in the source file

1FString UMyBlueprintFunctionLibrary::Conv_StrawberryToString(FStrawberry InStrawberry)
2{
3    return InStrawberry.MyName;
4}

4. After compiling, you can use the conversion node in the blueprint

Image 4

Of course, you can design any conversion type you want, as long as you need

Tips

  • The function must be written in the blueprint function library
  • The function must be a public static member function
  • The function name is arbitrary, and for clarity, it is recommended to follow the engine naming format structure
  • The conversion target type must be the return type
  • The source type must be the first incoming parameter (there can be multiple parameters)
  • The function must use the BlueprintPure tag to hide the input and output execution pins
  • CompactNodeTitle = "->" can be omitted, this tag is used as the node title name
  • The BlueprintAutocast tag must be retained, marking it as a conversion node

Engine version: 4.27.2

Translations:

Comment