[C++]how to get a umg component reference from an umg i created in c++ and extended in bp

Hi, im trying to make a 3d scrollbox which i will fill on c++ during runtime.

i created a umg in c++ named “TabletScreen” with

UPROPERTY(meta = (BindWidget))
	class UScrollBox* mScrollBox;

UPROPERTY(meta = (BindWidget))
	class UWrapBox* mWrapBox;

and give this class also a getWrapBox()

UWrapBox* UPokerTabletScreen::getWrapBox()
{
	return mWrapBox;
}

then extended this class with a blueprint, and attached a wrapbox and scrollbox with the same name as my variabile in the c++ class.

346752-screen.jpg

after this i created an actor named “tablet” in c++ with a widgetcomponent, i placed this actor in the world and put my blueprint_tabletscreen class as a variable.

346753-screen.jpg

after this i proceed to get first a reference to my tabletscreen class, then use the method getWrapBox to attach some textboxes but i crashed when i use getWrapBox

	UPokerTabletScreen* pk = (UPokerTabletScreen*)mTabletWidgetComponent->GetUserWidgetObject();
	UWrapBox* wrapBox = pk->getWrapBox();

with this error

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000400

UE4Editor_Poker3d_1042!UPokerTabletScreen::getWrapBox() [C:\Users\nerfl\Documents\Unreal Projects\Poker3d\Source\Poker3d\PokerTabletScreen.cpp:13]

surely im doing something wrong but i cant really figure it, my objective is to create a tablet or something like that with a scrollbox that im gonna fill dynamically

Hello! You can just use code like this to stay safe from nullptr exception

     if (UPokerTabletScreen* pk = Cast<UPokerTabletScreen>(mTabletWidgetComponent->GetUserWidgetObject())){
     UWrapBox* wrapBox = pk->getWrapBox();
}

One more thing - do you check that WidgetComponent already has created its Widget at the time you try to call this?

Hi! First of all thanks for answering. I will try when I’m at home.

I placed that actor with the widgetcomponent in the world with the editor so if im not mistaken it’s should already be fine right?

In most cases yes, but there can be issues and config when it is not fine. So you better check that anyway…