Hi there. I have a weird problem while using UMG from C++ code. I have a UMG class(with UserWidget as parent) which is used in my own HUD class : class UInGameMenuWidgetClass* InGameMenu;
I initilalize it at HUD’s BeginPlay method, but do not add to Viewport at that point(i do that after the specific button being pressed).
After using that widget i remove it from viewport with RemoveFromParent method. If i try to open that widget again after a short amount of time - everything is okay. But if i try to do it after 3-5 minutes after(only if it already was used) my game crashes af this part of code:
if (InGameMenu != nullptr)
{
if (!InGameMenu->IsInViewport())
{
InGameMenu->AddToViewport();
}
}
Looks like something wrong with Viewport not with my widget, and i don’t know to fix it. Have that problem in both 4.8/4.9 versions.
Could you elaborate on your setup and where the two if statements are being called? From my understanding you have a UMG class that inherits from UserWidget and then another class that inherits from the first? Or is the first UMG class the UInGameMenuWidgetClass you referred to? Where in your class do you have the code above being called? Additionally, could you post the callstack and log files from the crash? The callstack can be copied from the black Crash Report window after the crash. The log files are located inside the project directory’s Saved\Logs folder. After the crash occurs, you can zip the most recent logs and add the file as an attachment to the comment with the callstack.
Thank you for answer, but i’ve just found a solution ten minutes ago. Maybe it will help someone - the problem was that i didn’t use uproperty macro for InGameMenu widget definition. Looks like GC collects all properties which do not have that macro. I do not know C++ well enough(i’m a C# programmer) but i still do not understand why check for null pointer doesn’t work on collected properties like this one.
You can also use TWeakObjectPtrinstead of the usual pointer to the widget without writing UPROPERTY.
For BP you can use TSoftObjectPtr.
Perhaps this architectural approach will be more concise and correct.