概要
方法
GetRangeAsync(direction: Enum.SortDirection,count: number,exclusiveLowerBound: Variant,exclusiveUpperBound: Variant):{any} |
RemoveAsync(key: string):() |
UpdateAsync(key: string,transformFunction: function,expiration: number):Tuple |
API 参考
方法
GetRangeAsync
MemoryStoreSortedMap:GetRangeAsync(
direction:Enum.SortDirection, count:number, exclusiveLowerBound:Variant, exclusiveUpperBound:Variant
参数
exclusiveLowerBound:Variant |
exclusiveUpperBound:Variant |
返回
代码示例
检索 MemoryStore 键
local MemoryStoreService = game:GetService("MemoryStoreService")
local myMap = MemoryStoreService:GetSortedMap("MySortedMap")
function printAllKeys(map)
-- 初始下限为 nil,这意味着从第一个项目开始
local exclusiveLowerBound = nil
-- 这个循环一直持续到地图的末尾
while true do
-- 从当前下限开始获取最多一百个项目
local items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
for _, item in ipairs(items) do
print(item.key)
print(item.sortKey)
end
-- 如果返回的项目少于一百个,则表示我们已经到达地图的末尾
if #items < 100 then
break
end
-- 最后检索到的键是下一次迭代的排他性下限
exclusiveLowerBound = {}
exclusiveLowerBound["key"] = items[#items].key
exclusiveLowerBound["sortKey"] = items[#items].sortKey
end
end
printAllKeys(myMap)SetAsync
UpdateAsync
返回
代码示例
更新内存存储排序地图
local MemoryStoreService = game:GetService("MemoryStoreService")
local map = MemoryStoreService:GetSortedMap("Leaderboard")
local Players = game:GetService("Players")
function updateLeaderboard(itemKey, killsToAdd, deathsToAdd)
local success, newStats, newScore = pcall(function()
return map:UpdateAsync(itemKey, function(playerStats, playerScore)
playerStats = playerStats or { kills = 0, deaths = 0 }
playerStats.kills += killsToAdd
playerStats.deaths += deathsToAdd
if playerStats then
-- `playerScore` 是用于对地图中的项进行排序的 sortKey
playerScore = playerStats.kills / math.max(playerStats.deaths, 1)
return playerStats, playerScore
end
return nil
end, 30)
end)
end
-- 为所有玩家增加一次击杀
for i, player in pairs(Players:GetPlayers()) do
updateLeaderboard(player.UserId, 1, 0)
end
-- 为 userId 为 12345 的玩家增加 5 次击杀和 1 次死亡
updateLeaderboard(12345, 5, 1)