如何在 Steamworks.NET 中制作排行榜?

Posted

技术标签:

【中文标题】如何在 Steamworks.NET 中制作排行榜?【英文标题】:How to do leaderboards in Steamworks.NET? 【发布时间】:2016-10-16 23:41:20 【问题描述】:

我正在尝试在我的应用程序(用 C# 编写,使用 SteamWorks.NET 并基于 XNA 的游戏)中包含排行榜。

Steam 已正确初始化。

在游戏开始时我会调用:

SteamAPICall_t hSteamAPICall = SteamUserStats.FindLeaderboard("Most active beta testers");
leaderboard_BetaTesters_ResultFindLeaderboard = Callback<LeaderboardFindResult_t>.Create(OnLeaderboardFindResult_BetaTesters);

使用

static private void OnLeaderboardFindResult_BetaTesters(LeaderboardFindResult_t pCallback)

// See if we encountered an error during the call
if (pCallback.m_bLeaderboardFound == 0)
  
  Warning.Happened("Leaderboard could not be found / accessed");
  return;
  
else
  
  Write.Text("Steam leaderboad connected");
  
  leaderboard_BetaTesters = pCallback.m_hSteamLeaderboard;

在我调用的每个 Update() 中:

if (SystemLogic.SteamInitSuccessul()) SteamAPI.RunCallbacks();

但是,永远不会调用 OnLeaderboardFindResult_BetaTesters。我做错了什么?

【问题讨论】:

【参考方案1】:

我知道这是一个老问题,但我现在也有同样的问题。

当我尝试使用 Steamworks.NET 在 Unity 上实现 Steam 排行榜时,我遇到了同样的问题。 最后,我从原始的 Steamworks C++ 示例项目中得到了它。 (Steamworks.NET 的文档真的很差,就像原来的 Steamworks c++ 一样,所以你必须查看 Steamworks c++ 示例项目以了解如何使用它)

你有一个错误,你必须使用“CallResult”类而不是“Callback”类。

以下代码允许我将 Unity 上的分数上传到 SteamLeaderboard:

using UnityEngine;
using Steamworks;
using System.Collections;
using System.Threading;

public class SteamLeaderboards : MonoBehaviour

    private const string s_leaderboardName = "StoryMode";
    private const ELeaderboardUploadScoreMethod s_leaderboardMethod = ELeaderboardUploadScoreMethod.k_ELeaderboardUploadScoreMethodKeepBest;

    private static SteamLeaderboard_t s_currentLeaderboard;
    private static bool s_initialized = false;
    private static CallResult<LeaderboardFindResult_t> m_findResult = new CallResult<LeaderboardFindResult_t>();
    private static CallResult<LeaderboardScoreUploaded_t> m_uploadResult = new CallResult<LeaderboardScoreUploaded_t>();


    public static void UpdateScore(int score)
    
        if (!s_initialized)
        
            UnityEngine.Debug.Log("Can't upload to the leaderboard because isn't loadded yet");
        
        else
        
            UnityEngine.Debug.Log("uploading score(" + score + ") to steam leaderboard(" + s_leaderboardName + ")");
            SteamAPICall_t hSteamAPICall = SteamUserStats.UploadLeaderboardScore(s_currentLeaderboard, s_leaderboardMethod, score, null, 0);
            m_uploadResult.Set(hSteamAPICall, OnLeaderboardUploadResult);
        
    

    public static void Init()
    
        SteamAPICall_t hSteamAPICall = SteamUserStats.FindLeaderboard(s_leaderboardName);
        m_findResult.Set(hSteamAPICall, OnLeaderboardFindResult);
        InitTimer();
    

    static private void OnLeaderboardFindResult(LeaderboardFindResult_t pCallback, bool failure)
    
        UnityEngine.Debug.Log("STEAM LEADERBOARDS: Found - " + pCallback.m_bLeaderboardFound + " leaderboardID - " + pCallback.m_hSteamLeaderboard.m_SteamLeaderboard);
        s_currentLeaderboard = pCallback.m_hSteamLeaderboard;
        s_initialized = true;
    

    static private void OnLeaderboardUploadResult(LeaderboardScoreUploaded_t pCallback, bool failure)
    
        UnityEngine.Debug.Log("STEAM LEADERBOARDS: failure - " + failure + " Completed - " + pCallback.m_bSuccess + " NewScore: " + pCallback.m_nGlobalRankNew + " Score " + pCallback.m_nScore + " HasChanged - " + pCallback.m_bScoreChanged);
    



    private static Timer timer1; 
    public static void InitTimer()
    
        timer1 = new Timer(timer1_Tick, null,0,1000);
    

    private static void timer1_Tick(object state)
    
        SteamAPI.RunCallbacks(); 
    

*这段代码我已经编辑了好几次,没有编译,可能包含任何语法错误,但实现应该没问题

** 另请记住,此代码已在 Unity 4 上使用,但 Unity 类仅用于“控制台日志”消息和最后两个方法“每 x 毫秒调用 SteamAPI.RunCallbacks() 方法”

【讨论】:

要补充一点,如果你想创建排行榜,你可以使用类似FindOrCreateLeaderboard(s_leaderboardName, ELeaderboardSortMethod.k_ELeaderboardSortMethodDescending, ELeaderboardDisplayType.k_ELeaderboardDisplayTypeNumeric); 而不是FindLeaderboard() 的枚举列表你需要提供的参数 2 和 3 我在这里找到: github.com/rlabrecque/Steamworks.NET/blob/… 非常感谢 ICoso。请原谅我的菜鸟,但是在调用 UpdateScore() 之前应该如何等待 Init() 完成? @ChrisEmerson:我不再使用 Steam,但正如我在代码中看到的那样,UpdateScore 方法在“s_initialized”为真之前不会做任何事情,当 FindLeaderboard 回调时变为真被触发(OnLeaderboardFindResult)。因此,当 Init 方法完成时,UpdateScore 方法尚不可用,因为正在等待异步“OnLeaderboardFindResult”回调。如果需要,可以添加一个 bool 方法来检查 UpdateScore 是否可用(如果 s_initialized 值为 true)

以上是关于如何在 Steamworks.NET 中制作排行榜?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 discord.js 中使用 JSON 数据库制作排行榜

Game Center 如何让两个游戏使用同一个排行榜

如何更改 Game Center (iOS) 中的排行榜图标?

如何使用 Swift 将游戏的高分保存在排行榜上?

Discord.js 排行榜命令

尝试制作排行榜命令并收到此错误 TypeError: Cannot read property 'split' of undefined