0

I am making an unturned plugin and I need it to show player names based on CSteamID in a list I am unsure where to start I have the CSteamID in a list as strings if that helps in anyway! please let me know below what bits of my script you need to see, I am trying to get the steam username from the steam64 ID which I have obtained in a list in strings

1 Answers1

0

First, we need to retrieve the information from the Steam servers with: SteamFriends.RequestUserInformation(steamIDUserOfYourInterest);. After the user is retrieved, your event handler is called: protected Callback<PersonaStateChange_t> OnPersonaStateChangeHandler;. The handler has this signature:

private void OnPersonaStateChangeHandler(PersonaStateChange_t PersonaStateChange)

From inside of it you can check if the steam id of the changed persona matches the id of the player of your interest. After you determined that they match, you can finally call SteamFriends.GetFriendPersonaName(steamIDUserOfYourInterest) to actually retrieve the nickname from the steam id. We call GetFriendPersonaName, even considering they're not necessarily your friend. It works for non-friend users. There's a very similar SteamFriends.GetPlayerNickname() function but it doesn't work for me: it returns null.

Here's how to retrieve Gabe Newell's Steam account nickname and print it to the console:

public class SteamNicknameGetter
{
    private CSteamID GabeNewellsSteamID = new CSteamID(76561198085278322);
    protected Callback<PersonaStateChange_t> PersonaStateChangeCallback;

    public SteamNicknameGetter()
    {
        PersonaStateChangeCallback = Callback<PersonaStateChange_t>.Create(OnPersonaStateChangeHandler);
        bool needsToRetreiveInformationFromInternet = SteamFriends.RequestUserInformation(GabeNewellsSteamID, true);
        if (!needsToRetreiveInformationFromInternet)
        {
            string GabeNewellsSteamNickname = SteamFriends.GetFriendPersonaName(GabeNewellsSteamID);
            Console.WriteLine($"Gabe Newell's Steam nickname: {GabeNewellsSteamNickname}");
        }
    }

    private void OnPersonaStateChangeHandler(PersonaStateChange_t PersonaStateChange)
    {
        if (PersonaStateChange.m_ulSteamID == GabeNewellsSteamID.m_SteamID)
        {
            string GabeNewellsSteamNickname = SteamFriends.GetFriendPersonaName(GabeNewellsSteamID);
            Console.WriteLine($"Gabe Newell's Steam nickname: {GabeNewellsSteamNickname}");
        }
    }

    public static void Main(string[] Args)
    {
        SteamAPI.Init();
        SteamNicknameGetter steamNicknameGetter = new SteamNicknameGetter();
        while (true)
        {
            SteamAPI.RunCallbacks();
            Thread.Sleep(1000);
        }
    }
}

As the documentation says, we need to run SteamAPI.RunCallbacks() in order to dispatch the callbacks to the listeners. Otherwise, our OnPersonaStateChangeHandler won't be called.

I haven't tested this in C#, because I'm working in C++. I adapted the code from Callbacks section in the Steamworks.NET documenation, and it looks like it should work exactly the same(as the C++ version), according to the documentation. The C++ code looks pretty much identical but done in the C++ way:

class SteamNicknameGetter
{
private:
    unsigned long long GabeNewellsSteamID{ 76561198085278322 };
    STEAM_CALLBACK(SteamNicknameGetter, OnPersonaStateChangeHandler, PersonaStateChange_t);

public:
    SteamNicknameGetter() {
        bool needsToRetreiveInformationFromInternet = SteamFriends()->RequestUserInformation(GabeNewellsSteamID, true);
        if (!needsToRetreiveInformationFromInternet)
        {
            std::string GabeNewellsSteamNickname = SteamFriends()->GetFriendPersonaName(GabeNewellsSteamID);
            std::cout << "Gabe Newell's Steam nickname:" << GabeNewellsSteamNickname;
        }
    }
    static void main()
    {
        SteamAPI_Init();
        SteamNicknameGetter steamNicknameGetter{};
        while (true)
        {
            SteamAPI_RunCallbacks();
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }
    }
};

void SteamNicknameGetter::OnPersonaStateChangeHandler(PersonaStateChange_t* PersonaStateChange)
{
    if (PersonaStateChange->m_ulSteamID == GabeNewellsSteamID)
    {
        std::string GabeNewellsSteamNickname = SteamFriends()->GetFriendPersonaName(GabeNewellsSteamID);
        std::cout << "Gabe Newell's Steam nickname:" << GabeNewellsSteamNickname;
    }
}
KulaGGin
  • 943
  • 2
  • 12
  • 27