Engine Class
MemoryStoreSortedMap
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
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
ส่งค่ากลับ
ตัวอย่างโค้ด
การอัปเดตแผนที่ที่จัดเรียงใน MemoryStore
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
-- เพิ่มการฆ่า 5 ครั้งและการตาย 1 ครั้งให้กับผู้เล่นที่มี userId 12345
updateLeaderboard(12345, 5, 1)