エンジンクラス
MemoryStoreHashMap
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
方法
RemoveAsync(key: string):() |
UpdateAsync(key: string,transformFunction: function,expiration: number):Variant |
APIリファレンス
方法
GetAsync
パラメータ
戻り値
Variant
コードサンプル
メモリストアのハッシュマップからのデータ取得
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, _ = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("セットに成功しました!")
end
local item
local getSuccess, getError = pcall(function()
item = hashMap:GetAsync(key)
end)
if getSuccess then
print(item)
else
warn(getError)
endListItemsAsync
パラメータ
コードサンプル
メモリストアハッシュマップにアイテムをリスト表示する
local MemoryStoreService = game:GetService("MemoryStoreService")
local testHashMap = MemoryStoreService:GetHashMap("HashMap1")
local EXPIRATION = 600
local NUM_TEST_ITEMS = 32
local function populateHashMap(hashMap: MemoryStoreHashMap, numItems: number): { [string]: any }
print("HashMapデータを設定しています...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- HashMapのキーは文字列でなければなりません
local value = `{key}_テスト値`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`キー {key} の設定中にエラーが発生しました: {result}`)
end
end
print("HashMapデータの設定が完了しました。")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- ログ記録の目的で、現在のページ番号を追跡します
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`ページ {currentPageNumber} のアイテムを取得しています...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- 読み取るページがもっとある場合はページを進めます
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("すべてのページの読み取りが完了しました")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("取得したアイテムを期待されるアイテムと比較しています...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`キー {key} に対する取得した値が一致しません: 期待値 {expectedValue}, 取得値 {retrievedItems[key]}`)
end
end
print("比較が完了しました!")
return numMatchingItems
end
-- ハッシュマップに追加されたキーは、このexpectedItemsテーブルにも追加されます。
-- 後で、取得したハッシュマップアイテムはこの期待されるアイテムのテーブルと比較されます。
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- ページを取得する際にエラーが発生することがあります。この場合、エラーのままプログラムの実行を停止しますが、
-- pcallを使用して異なる方法で処理することもできます。
print(`ListItemsAsyncを使用してHashMapページを取得しています...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- アイテムの設定または取得中にエラーが発生しなかった場合、すべてのアイテムは一致するはずです。
print(`プログラム完了。 {numMatchingItems}/{NUM_TEST_ITEMS} の取得アイテムが期待される値と一致しました。`)RemoveAsync
パラメータ
戻り値
()
コードサンプル
メモリーストアハッシュマップからデータを削除する
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if not setSuccess then
warn(setError)
end
local removeSuccess, removeError = pcall(function()
hashMap:RemoveAsync(key)
end)
if removeSuccess then
print("削除に成功しました!")
else
warn(removeError)
endSetAsync
戻り値
コードサンプル
メモリストアハッシュマップにデータを追加する
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("設定に成功しました!")
else
warn(setError)
endUpdateAsync
戻り値
Variant
コードサンプル
メモリーストアハッシュマップの更新
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("ResourceInventory")
local function contributeResources(itemResource, addedCount)
local success, newResourceCount = pcall(function()
return hashMap:UpdateAsync(itemResource, function(resource)
resource = resource or { count = 0 }
resource.count = resource.count + addedCount
-- 最大リソースカウントを超えないことを確認する
if resource.count > 500 then
resource.count = 500
end
return resource
end, 1200)
end)
if success then
print(newResourceCount)
end
end
contributeResources("myResource", 50)