then your request is not for Unreal, but for the C++ maintainers (the C++ standards Committee). keep in mind that there is a lag time between the maintainers defining a new specification (about every 3 years so the next one should be C++26), and it being implemented into the latest version of the compilers, often with about a 3-6 year gap between the standard being published and it being implemented into the compilers, though it is in the best interest of the compilerâs relevance to implement the features in a âreasonableâ amount of time
the âclosestâ you get is having the function return whether or not the pointer will be pointing to null or pointing to the Heap, or wrapping the pointers declaration and assignment into a single if scope.
Java is sometimes called âsyntactic sugar on C++â (I personally donât have experience with Kotlin, so I donât know its absolute relation and build up from languages like C++), and even then these ânullable callsâ are functionally just syntax of putting a comparison to null with the assignment, it does it with extra symbols instead of potentially additional scope and nesting.
bool FuncDoStuff(MyPointer* inPointer){
if (inPointer == nullptr) { return false; }
// do some other stuff
return true;
}
with C++98(auto pointers, smart pointers), C++ 17 (optional) and C++ 20 (Spaceship operator <=>
) have introduced mechanisms for things similar to nullable calls, but these are are reliant on smart pointers, which unreal does wrap on for the specialized pointers we are given. but we are advised for return types and parameters to use raw pointers and references especially for blueprint facing code.
for the question that you asked âis it possible to do a nullable call in cpp?â
technically yes, but it just takes wrapping it in an if block, or relying on the return value of the function.
things like
if( UMyWorldSubsystem* system = GetWorld()->GetSubsystem<UMyWorldSubsystem>() ){
// if the subsystem exists then it will be assigned to the pointer 'system' and safe to access
// it will also only be accessible within this if block for additional safty
} else {
// the subsystem does not exist currently
}
or
bool LineTraceWrap(FVector Start, FVector End) {
FHitRestult HitResult;
if( GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility) ) {
// something was hit on that channel either blocking or overlapping, and it is "safe" to try and work with the pointers in the 'FHitResult'
} else {
// nothing was hit do something if you need.
}
}
the big reason why nullable calls have not been implemented is because in normal C++ we donât have garbage collection, so the only way the pointer will be freed or deleted is if that operation is called explicitly.
for example there are situations in Java where if memory is being consumed too rapidly and malloc is unable to return additional addresses then a GC will be activated, and that pointer you are actively working on might be just gone, while you are working on it; leading to a rare case of Null_Reference_Exception in Java (unlikely but is not impossible, although it is equally possible that the runtime will return the value at the offset but this has additional problems)