LuaJIT FFI: 上传 Steamworks 排行榜

如何使用 LuaJIT FFI 将 SteamAPICall_t 与 SteamLeaderboard_t 句柄一起使用?

我使用 LÖVE2D 框架和 Steamworks Lua Integration (SLI)

链接: FindLeaderboard / UploadLeaderboardScore / Typedef

function UploadLeaderboards(score)
local char = ffi.new('const char*', '排行榜名称')
local leaderboardFound = steamworks.userstats.FindLeaderboard(char) -- 返回 SteamAPICall_t
local leaderboardCurrent = ?? -- 如何使用 typedef SteamLeaderboard_t 与 SteamAPICall_t 一起使用呢?
local c = ffi.new("enum SteamWorks_ELeaderboardUploadScoreMethod", "k_ELeaderboardUploadScoreMethodKeepBest")
score = ffi.cast('int',math.round(score))
return steamworks.userstats.UploadLeaderboardScore(leaderboardCurrent, c, score, ffi.cast('int *', 0), 0ULL)
end
leaderboardCurrent = ffi.cast("SteamLeaderboard_t", leaderboardFound) -- 无声明错误
点赞
用户6119195
用户6119195

_SteamAPICall_t_ 仅仅是一个对应你请求的编号。这是用于 steam API 的 CCallback 的附属选项。lua 集成没有包含 CCallback 和 _STEAM_CALLBACK_。

通过调用 FindLeaderboard 来生成 SteamLeaderboard_t 响应。在这种情况下,你正在向 steam 发送请求,并且 steam 需要以异步方式响应。

因此,你需要定义一个监听器对象(在 C++ 中),以便监听响应(即 SteamLeaderboard_t)并为其编写 C 类型函数,以便 ffi 能够理解它们。

这意味着你的程序必须能够执行以下操作:

  1. 为排行榜注册一个监听器。
  2. 提交排行榜请求(FindLeaderboard)。
  3. 等待消息(SteamLeaderboard_t)。
  4. 使用 SteamLeaderboard_t。

简而言之,你需要在 C++ 中编写事件代码,并为它们添加类似 C 的接口,并将其编译为 DLL,然后使用 FFI 将该 DLL 链接到 lua。这可能有点棘手,因此请注意。

在 C(ffi.cdef 和 dll)中:

//您必须编写一个定义这些内容的 DLL
typedef struct LeaderboardEvents{
    void(*onLeaderboardFound)(SteamLeaderboard_t id);
} LeaderboardEvents;
void MySteamLib_attachListener(LeaderboardEvents* events);

然后在 lua 中:

local lib = --在这里加载你的 DLL 库
local Handler = ffi.new("LeaderboardEvents")

Handler.onLeaderboardFound = function(id)
   -- 在这里做你的事情。
end

lib.MySteamLib_attachListener(Handler)

在编写 DLL 时,我强烈建议你阅读 steam API 提供的 SpaceWar 示例,以便了解如何注册回调函数。

2018-04-01 16:11:59