How to save and restore any value with RAII approach in Unreal Engine

Imagine you’re in a situation where you need to save and restore a value. For instance, let’s say you have a UMG widget that you want to hide temporarily, do some stuff, and then bring back its previous visibility state, all within a single function. The most straightforward approach might be to store the value in a temporary variable, modify it, and then revert it back, like this: ESlateVisibility VisibilityState; const ESlateVisibility PreviousVisibility = VisibilityState; VisibilityState = ESlateVisibility::Hidden; // Do smth....

February 25, 2024 · 2 min · Georgy Treshchev

How to cast Slate widgets in Unreal Engine

This brief article addresses the question of how to cast one Slate widget to another in Unreal Engine. Imagine you have an SWidget widget wrapped in a shared pointer like this: TSharedPtr<SWidget> MyWidgetPtr; If you want to cast it, for example, to an SSpacer, your code might look like this: TSharedPtr<SSpacer> MySpacerPtr = StaticCastSharedPtr<SSpacer>(MyWidgetPtr); It’s important to keep in mind that you should verify whether the shared pointer is valid before using it....

October 21, 2023 · 1 min · Georgy Treshchev

How to set a timer for the next tick in Unreal Engine

In Unreal Engine, it is possible to set a delay for the function to execute after a single tick, which is sometimes useful when working with UMG/Slate side of things where the widget properties are only available after a complete rebuild, such as obtaining the desired size of the widget’s geometry, which is often not accessible right after initializing or constructing the widget. In such cases, you may want to implement a delay, as described in this article , but sometimes even a single tick delay is sufficient and can enhance the overall user experience, preventing visual hitches and abrupt interactions....

September 15, 2023 · 2 min · Georgy Treshchev

Efficient HTTP file download by chunks in Unreal Engine C++

Chunk downloading is a technique used to retrieve large binary data from the server in separate parts, ensuring reliability and compatibility across different platforms. Unreal Engine’s HTTP module has a limitation of 2GB for binary HTTP response content due to internal restrictions (specifically, TArray<uint8> uses the int32 size type, which has a maximum value of 2,147,483,647, approximately 2 GB in our case). To overcome this limitation, we can use the Range HTTP header supported by most servers, without requiring any file preparation or segmentation....

May 21, 2023 · 4 min · Georgy Treshchev

How to save UObject that is represented as an asset in Unreal Engine

You can save any UObject that is represented as an asset within a UPackage using this method: bool SaveToAsset(UObject* ObjectToSave) { UPackage* Package = ObjectToSave->GetPackage(); const FString PackageName = Package->GetName(); const FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()); FSavePackageArgs SaveArgs; // This is specified just for example { SaveArgs.TopLevelFlags = RF_Public | RF_Standalone; SaveArgs.SaveFlags = SAVE_NoError; } const bool bSucceeded = UPackage::SavePackage(Package, nullptr, *PackageFileName, SaveArgs); if (!bSucceeded) { UE_LOG(LogTemp, Error, TEXT("Package '%s' wasn't saved!...

February 5, 2023 · 1 min · Georgy Treshchev