Added support for "Optional" data type (TOptional<T>) in Blueprints

I recently added a new functionality that I find missing in UE: the support of the Optional type in Blueprints (aka. TOptional<T> in C++)

Optional1

I guess I am not the first one which seemed to look for that: Post 1, Post 2, Post 3

So I decided to do the hard work. After some non trivial changes in UE code, I finally managed to get this working, and landed a PR. Maybe UE Team will accept it (or maybe not, because there is many changes :wink:).

=> https://github.com/EpicGames/UnrealEngine/pull/12197

Feel free to test if you want !

For those who donā€™t have access to the UE Github, here is an extract of PR description, with a bit more context and info:

Hi,

I have noticed some changes last year related to implementing support for TOptional data type in the UObject system, essentially through a new FOptionalProperty, as well as some preliminary support for such properties on the GUI side.

Though, as of current development version of UE5, this is not yet fully implemented: FOptionalProperty is only supported at the very low level side (serialization), and the very high level side (the GUI). But there is no ā€œglueā€ between the two !

So I decided to bridge this gap, and implement full support of the FOptionalProperty to most of the UE subsystems, and in particular, to Blueprints.

Functionality Overview

Basically, you can now have Optional<T> variables/parameters in Blueprints, as well as exposing them from C++.

In the Editor, Optional are integrated as a new ā€œcontainerā€ type, like Arrays, Maps and Sets:

Optional1

Conceptually, an Optional can be viewed as a ā€œcontainerā€ of 0 or 1 item. This fits quite well with the existing implementation of the containers properties: they have an inner type (=> the Optional type), you can get items (=> get the Optional value), add an item (=> set the Optional value), check for emptiness (check if the Optional is set), clean the items (=> unset the Optional).

Optional variables are editable in the usual property editors thanks to the preexisting GUI support that was never used up until now (the ā€œSet to Valueā€ / ā€œUnsetā€ buttons were in fact already implemented since some time):

Optional2

All the existing common types are supported, as variables, local variables, function/event parameters, function return values, macros arguments, sub-graphs, etc. :

Optional3

Like with other containers types, it is not possible to nest Optionals in another container. You have to rely on an intermediate struct for that.

Speaking of structs, their are supported too: you can define attributes of an Optional type. Indirectly, this means Optionals are also supported in Data Tables (with a limitation, see next section).

A small set of functionalities have been implemented to support Optional manipulation from BP graphs, with the usual ā€œGet / Is Set / Unsetā€ functions, as well as support for ā€œSet By Refā€ node :

Optional4

Additionally, a convenience ā€œMake Optionalā€ node is provided too, modeled around the existing ā€œMake Containerā€ node :

Optional6

(on the picture, right-clicking on the ā€œMake Optionalā€ node shows either ā€œSet Valueā€ or ā€œUnset Valueā€ menu actions)

Incidentally, the support of the optional type in Blueprint allows for optional parameters/arguments to functions, as I have made optionals default-constructible (to an unset value):

Optional7

Finally in C++ code, optionals can be exposed as UPROPERTY(...) TOptional<T>, as well as parameters to UFUNCTIONS, for each supported function types: BlueprintCallable, BlueprintNativeEvent, BlueprintImplementableEvent

[ā€¦] Implementation details and notes in the PR

3 Likes

TOptional<> should absolutely be a supported property type in the engine. +1

1 Like