The Problem
So I’ve been doing a lot of work with widgets and UMG but I prefer to keep as much logic inside C++ as possible where I can. So the problem comes when I want to get say a “TextBlock” element from the Canvas but into code as a variable such as UTextBlock* MyTextBlock.
I’ve been doing it in a specific way but it’s not full proof, it often produces “data races” more like misaligned c++ vs BP timings I think.
My Current Solution - Half Broken
Capture - Shows some variables from my canvas object, you can see their variables and public.
Capture 1 - Shows a method (Set All Text and Image Elements" which is a custom C++ function called in BP via the construct where I’m assigning all of the canvas variables to C++.
Capture 2 - The actual C++ method you saw being called in BP in Capture 1, I’ve scratched out a few extra elements as they’re not essential for this question. I’m associating my class member variables “AttackTextBlock”, “DefenceTextBlock” etc with the canvas variables I’m passing via blueprint.
Capture 3 - Another method that is called via other objects that will actually update the text on the TextBlocks. When I get to this point it tends to say that there is a nullptr here on the text block and it breaks.
My question
Sorry if this has been answered before but I did look for quite a while and I prefer not to type long questions and I usually try out my own solutions regardless.
Question - Is there a much better way to get access to your canvas variables via C++ code on a custom classed UMG widget? Thanks for your help! If you need any further information please let me know.
UPDATE
So I’ve tried the advice of a comment by using BlueprintImplementable events but that problem is still there. Here is a snippet or what is happening.
From my Player Controller I am calling a “DisplayStats” method and am sending AUnitBase which is the unit that contains my data (this is fine and populated)
WidgetParentClass->DisplayStats(UnitDataToPull);
Display Stats is actually an event in Blueprints for a different Class (UUserWidget) and this event calls this function The cast in this function is not needed but I was trying to see if accepting a generic widget and casting would help and it didn’t:
void UStatBattleWidget::ShowAllStats(AUnitBase* UnitToObserve, UUserWidget* StatPanel)
{
USeperateStatPanel* TempPanel = Cast<USeperateStatPanel>(StatPanel);
UE_LOG(LogTemp, Warning, TEXT("WAiting"));
if (UnitToObserve != nullptr)
{
TempPanel->DisplayDataEvent(UnitToObserve);
}
}
This is the blueprint showing the event calling this function and you can see I’m passing a UUserWidget Object (It’s my W_Stat_VB_Panel which is a custom widget that I added to this one. Now this is where the problem happens. It’s this specific line:
TempPanel->DisplayDataEvent(UnitToObserve);
It throws an error saying that Access Violation but essential the error comes from the StatPanel or TempPanel because they are null. Yet I just passed a reference via the method call. So the method has been called 100% but the variable is null.
Does anyone have any idea why this would be null?