How to get a TSharedPtr to my Widget?


I just got some advance on the CustomWidget, but, FSlateDrawElement::MakeCustom requires a TSharedPtr from the CustomDrawer as last parameter.

I thought on send the own Widget as CustomDrawer, but the AsShared() doenst’ returns what I need and I’m not getting how to cast it form “this”.

I’ve created a clean TSharedPtr inside my class but couldn’t get yet how to fill it with my instance. :frowning:

Thank You in advance.

PS If this is impossible I’ll get a third class that accepts New() operator just to make the drawing.

A TSharedPtr is used to manage the lifetime of the base object. So you do something like:

TSharedPtr<MyObject> myObjectPtr(new MyObject());

You can’t use it for a stack-based object, it has to be deletable, s0 you have to create a new object for the TSharedPtr to manage. This is because, when all the TSharedPtr that point to an object go out of scope, the object is deleted.

Trying to use new with any child from SWidget produces:
error C2248: ‘FSlateControlledConstruction::operator new’ : cannot access private member declared in class ‘FSlateControlledConstruction’

I got a compile with MakeShareable(this) macro, but I’m getting access violation on exit. lol

If the constructor is private, then Epic doesn’t want you doing that :).

You should make a new class to implements the interface and call your instance from that.

Widgets are built inheriting from compoundwidget or userwidget.
On watch ElementDraw::MakeCustom docs I thought on implement ICustomSlateElement in the own widget class, but the TSharedPtr thing messed my plans.
Unless there is a valid way to the Widget get a TSharedPtr to itself on calling Construct I’ll need to write another class to get the Widget working, wrote 2 already…

UWidgetClass expose to UMG an “inner” SWidgetClass that keeps the Widget funcionality and by what’s going on I’ll also need a separate Drawer*…

*unless I get a pointer as needed. lol