Cannot use TSharedPtr with UObjects

I am trying to create a list of available games/servers that a player can join. I was looking at the shooter game example and I was trying to duplicate what they did. However I can’t get it to work. First error I got was

Error	9: error C2039: 'AsShared' : is not a member of 'UArenaFriendsList'

Then I added the following to my class in the header:

UCLASS()
class THEARENA_API UArenaFriendsList : public UUserWidget, public TSharedFromThis<UArenaFriendsList>
{
	GENERATED_BODY()

but when I add TSharedFromThis I got the error:

Error	2: error C2338: You cannot use TSharedPtr with UObjects.

Anyone know how I could go about showing a list of friends who are currently hosting games. This is the line of code causing all the fuss:

StartOnlinePrivilegeTask(IOnlineIdentity::FOnGetUserPrivilegeCompleteDelegate::CreateSP(this, &UArenaFriendsList::OnUserCanPlayOnlineJoin));

The CreateSP on the delegate is “create shared-pointer”, and assumes that the object it’s being bound to is somehow derived from TSharedFromThis so it can call AsShared() on it.

As the error pointed out, you can’t use TSharedPtr with a UObject based type, however we do have a different delegate creation function, CreateUObject, that will work with UObject types.

StartOnlinePrivilegeTask(IOnlineIdentity::FOnGetUserPrivilegeCompleteDelegate::CreateUObject(this, &UArenaFriendsList::OnUserCanPlayOnlineJoin));

You’re the man!

Hi @Jamie_Dale,

In a UObject method, I would like to use TDelegate::CreateLambda instead of TDelegate::CreateUObject because I need to capture some variables. How can I be sure that the UObject is still alive when the lambda is called ? With TSharedFromThis it ispossible by using AsShared() or AsWeak().