Online Subsystem Retrieving UniqueNetID

So I’ve spent the last 4 days attempting to find the answer and have read through pretty much every post out there on this.

OnlineSubsystem has already been enabled and all that just not sure how to write this function.

In the documentation for Online IdentityInterface it States

I cannot find anything on HOW to implement this because this is the extent of the documentation on that I can find.

Just want to set it up as a function that returns the UniqueID.

I’m a complete noob at C++ but years of experience in PHP and JS / JS variants… Any help is appreciated been stuck on this almost all week and i feel it should pretty dam simple to retrieve a var that already exists lol

What exactly are you trying to do? If you just want the UniqueID of the local player you can call:



TSharedPtr<const FUniqueNetId> MyLocalPlayer = GetUniquePlayerId(0);


If you want to replicate that Id / Serialize it:



UPROPERTY(Replicated)
FUniqueNetIdRepl MyReplicatedId;

MyReplicatedId.SetUniqueNetId(MyLocalPlayer);


and then to convert back to a FUniqueNetId:



const TSharedPtr<const FUniqueNetId>& RemotePlayer = MyReplicatedId.GetUniqueNetId();


Just search in the code for “FUniqueNetIdRepl” and you’ll find plenty of examples.

Alright I attempted to write the function but I’m getting a return of GetUniquePlayerID undefined, I’m sure I’m missing a header or have the improper one selected. Sorry, I’m such a moron I did JS, PHP and web side technologies for years but for some reason, I’m struggling with the C++ side of this part.



#include "GetUserID.h"
#include "CoreMinimal.h"
#include "Engine.h"
#include "Online.h"
#include "OnlineSubsystem.h"
#include "Interfaces/OnlineIdentityInterface.h"
#include "OnlineSubsystemUtils.h"

void UGetUserID::GetID()
{
    TSharedPtr<const FUniqueNetId> MyLocalPlayer = GetUniquePlayerId(0);

    UPROPERTY(Replicated)
        FUniqueNetIdRepl MyReplicatedId;

    MyReplicatedId.SetUniqueNetId(MyLocalPlayer);

    const TSharedPtr<const FUniqueNetId>& RemotePlayer = MyReplicatedId.GetUniqueNetId();

   Return RemotePlayer;
}


The UPROPERTY needs to be in your header. It’s a variable for your class, not your method. :slight_smile: Also you need to access the Online Subsystem singleton to get the Identity interface (which has the GetUniquePlayerId call).




TSharedPtr<const FUniqueNetId> MyLocalPlayer = IOnlineSubsystem::Get()->GetIdentityInterface()->GetUniquePlayerId(0);



That code assumes you have the OSS stuff all setup correctly (otherwise it’ll crash).

I highly suggest you download one of the content examples, like ShooterGame, which has all this laid out in code for you to read through.

I tried to download the shootergame example but cannot find it on the unreal site, or in the unreal editor. Is it under templates? or in the Learn Unreal section?

EDIT: Nevermind I found it in the Epic Games Launcher under Learn and at the very bottom. Then click the cloud icon.