Skip to content

CustomDataService

CustomData 地图自定义数据服务,提供键值对形式的数据存取与删除。 通过 GetService 获取。

Overview

CustomDataService 是地图自定义数据的键值存取入口,允许以字符串键关联任意值进行写入、读取和移除。它适合存放地图运行中需要共享的结构化配置,通过简洁的三段式接口完成数据维护。

Get by:

lua
-- @runtime client
local service = editor:GetService("CustomData")

Public

GetCustomMapData

按指定键读取自定义地图数据中保存的值,返回该键对应的数据;如果当前没有该键对应的数据,则返回 nil。

参数

参数类型说明
keyString数据键名

返回值

类型说明
Any键对应的数据值;不存在时返回 nil

调用 GetCustomMapData

lua
-- @runtime client
local customDataService = editor:GetService("CustomData")
local key = "attack"  -- String
local result = customDataService:GetCustomMapData(key)  -- 返回 Any
if result ~= nil then
    print("调用成功,结果: " .. tostring(result))
end

SetCustomMapData

按指定键写入自定义地图数据,将 data 保存到 key 对应的位置,供后续通过相同 key 读取使用。

参数

参数类型说明
keyString数据键名
dataAny要写入的数据(任意类型)

调用 SetCustomMapData

lua
-- @runtime client
local customDataService = editor:GetService("CustomData")
local key = "attack"  -- String
local data = nil  -- Any
customDataService:SetCustomMapData(key, data)
local currentValue = customDataService:GetCustomMapData(key)
if currentValue ~= nil then
    print("当前值: " .. tostring(currentValue))
end

RemoveCustomMapData

按指定键删除自定义地图数据中保存的数据,删除后该键对应的数据将不再存在。

参数

参数类型说明
keyString数据键名

调用 RemoveCustomMapData

lua
-- @runtime client
local customDataService = editor:GetService("CustomData")
local key = "attack"  -- String
customDataService:RemoveCustomMapData(key)
print("调用完成", customDataService)