Home > Blockchain >  Can't Get Or Set Achievements With Steamworks.NET in Unity [closed]
Can't Get Or Set Achievements With Steamworks.NET in Unity [closed]

Time:10-09

I'm using Steamworks.NET to set up Steam achievements in the game I'm working on. The SteamManager is initialising properly, the App ID is properly set and the achievements have been published. Calling GetAchievement however, returns false. SetAchievement has the same problem and the UserAchievementStored_t callback is never called. All of the other calls to the Steamworks API return true and their callbacks are triggered with no errors.

Any ideas as to why these calls are returning false or anything else to check would be appreciated.


Here's my code:

public class SteamAchievementManager : AchievementManager
{
    List<AchievementID> achievementQueue = new List<AchievementID>();
    List<AchievementID> backupAchievementQueue = new List<AchievementID>();

    private Callback<UserStatsReceived_t> statsRequestedResult;
    private Callback<UserStatsStored_t> statsStoredResult;
    private Callback<UserAchievementStored_t > achievementStoredResult;

    bool awarding = false;

    int retryCount = 0;
    int maxRetryCounts = 5;

    public override void Init()
    {
        if( !SteamManager.Initialized )
        {
            Debug.LogError( "Tried to Init SteamAchievementManager but SteamManager is not initialised" );
            return;
        }

        //Steamworks.SteamUserStats.ResetAllStats( true );

        statsRequestedResult = Callback<UserStatsReceived_t>.Create( OnStatsReceived );
        statsStoredResult = Callback<UserStatsStored_t>.Create( OnStatsStored );
        achievementStoredResult = Callback<UserAchievementStored_t>.Create( OnAchievementStored );

        SteamUserStats.RequestCurrentStats();
    }

    public override void AwardAchievement( AchievementID achievementID )
    {
        if( !SteamManager.Initialized )
        {
            Debug.LogError( "Tried to Award Achievement but SteamManager is not initialised" );
            return;
        }

        achievementQueue.Add( achievementID );
        if( !awarding )
            AttemptToAwardAndStoreAchievements();
    }

    void AttemptToAwardAndStoreAchievements()
    {
        awarding = true;

        if( SteamUserStats.RequestCurrentStats() )
        {
            Debug.Log( "Successfully Requested Stats" );
        }
        else
        {
            Debug.LogError( "Failed to Request Stats" );
            CompleteAwarding( false );
        }
    }

    void OnStatsReceived( UserStatsReceived_t result )
    {
        if( result.m_eResult != EResult.k_EResultFail )
        {
            Debug.Log( "Successfully Received Stats "   result.m_eResult );
            AwardQueuedAchievements();
        }
        else
        {
            Debug.LogError( "Failed To Received Stats" );
            CompleteAwarding( false );
        }
    }

    void AwardQueuedAchievements()
    {
        List<AchievementID> achievementsToAward = new List<AchievementID>( achievementQueue );

        while( achievementQueue.Count > 0 )
        {
            AchievementID achievementID = achievementQueue[ achievementQueue.Count - 1 ];
            string steamID = AchievementData.achievementList.GetAchievement( achievementID ).steamID;
            bool alreadyAwarded;
            Debug.Log( Steamworks.SteamUserStats.GetAchievementDisplayAttribute( steamID, "name" ) );
            if( !Steamworks.SteamUserStats.GetAchievement( steamID, out alreadyAwarded ) )
            {
                Debug.LogError( "Failed To Get Achievement: "   steamID );
            }
            if( !alreadyAwarded )
            {
                if( Steamworks.SteamUserStats.SetAchievement( steamID ) )
                {
                    Debug.Log( "Awarded Achievement: "   steamID );
                }
                else
                {
                    Debug.LogError( "Failed To Awarded Achievement: "   steamID );
                    backupAchievementQueue.Add( achievementID );
                }
            }
            achievementQueue.RemoveAt( achievementQueue.Count - 1 );
        }

        SetStats();
    }

    private void OnAchievementStored( UserAchievementStored_t result )
    {
        Debug.Log( "Achievement Stored Callback" );
    }

    public void SetStats()
    {
        SteamUserStats.StoreStats();
    }

    private void OnStatsStored( UserStatsStored_t result )
    {
        if( result.m_eResult != EResult.k_EResultFail )
            Debug.Log( "Achievement Stats Succesfully Stored" );
        else
            Debug.LogError( "Achievement Stats Failed To Store" );

        CompleteAwarding( result.m_eResult != EResult.k_EResultFail && backupAchievementQueue.Count == 0 );
    }

    void CompleteAwarding( bool wasSuccesful )
    {
        awarding = false;
        retryCount = wasSuccesful ? 0 : retryCount   1;
        achievementQueue.AddRange( backupAchievementQueue );
        backupAchievementQueue.Clear();

        if( retryCount > maxRetryCounts )
        {
            Debug.LogError( "Failed to Award Achievements After Multiple Attempts" );
            retryCount = 0;
        }
        else if( achievementQueue.Count > 0 )
        {
            retryCount  ;
            AttemptToAwardAndStoreAchievements();
        }
    }
}

CodePudding user response:

Turns out the app ID was wrong (figured that out by printing result.m_nGameID in OnStatsStored). I was convinced I had the correct ID because when I imported the package, there was a steam_appid.txt file created inside the Steamworks plugin folder, which I had changed to have the correct id. However there's another steam_appid.txt file in the project root folder, which is where the id is pulled from and this one still had the wrong id.

So essentially I herped before I derped. All working now!

  • Related