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++)
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 ).
=> 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:
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):
All the existing common types are supported, as variables, local variables, function/event parameters, function return values, macros arguments, sub-graphs, etc. :
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 :
Additionally, a convenience āMake Optionalā node is provided too, modeled around the existing āMake Containerā node :
(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):
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