エンジンクラス
MemoryStoreSortedMap
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
方法
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 |
戻り値
コードサンプル
メモリストアキーの取得
local MemoryStoreService = game:GetService("MemoryStoreService")
local myMap = MemoryStoreService:GetSortedMap("MySortedMap")
function printAllKeys(map)
-- 初期の下限は nil で、最初のアイテムから開始することを意味します
local exclusiveLowerBound = nil
-- このループは、マップの終わりに達するまで続きます
while true do
-- 現在の下限から始めて最大100個のアイテムを取得します
local items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
for _, item in ipairs(items) do
print(item.key)
print(item.sortKey)
end
-- 呼び出しが100個未満のアイテムを返した場合、それはマップの終わりに達したことを意味します
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
-- 全プレイヤーに1キル追加
for i, player in pairs(Players:GetPlayers()) do
updateLeaderboard(player.UserId, 1, 0)
end
-- userId 12345 のプレイヤーに5キルと1死亡を追加
updateLeaderboard(12345, 5, 1)