UDataTable

UDataTable

继承关系

UObject

元方法

__len

  • 用法: #UDataTable
  • 返回类型: integer
  • 返回数据表中的行数。

方法

IsValid()

  • 返回类型: bool
  • 返回值: 数据表是否有效(非 null)。

GetRowStruct()

  • 返回类型: UScriptStruct | nil
  • 返回值: 定义此数据表行结构的 UScriptStruct;未设置时返回 nil。

GetRowMap()

  • 返回类型: table
  • 返回值: 包含所有行的 Lua 表,行名为键,行数据为值。
  • 每个值根据该行类型是否存在自定义推送器,可以是 UScriptStruct 或 Lua 表。

FindRow(string RowName)

  • 返回类型: UScriptStruct | nil
  • 返回值: 找到时返回行数据 UScriptStruct;行不存在时返回 nil。
  • 返回的结构体通过引用访问,修改它会直接影响 DataTable。
  • 示例:local row = dt:FindRow("Player"); row.Health = 200 会修改实际的 DataTable。

AddRow(string RowName, table|UScriptStruct RowData)

  • 使用指定名称和数据向数据表添加新行。
  • 若同名行已存在,则替换该行。
  • 接受 Lua 表或 UScriptStruct:
    • 表: 字段应与行结构体定义相匹配
    • UScriptStruct: 从结构体复制数据(适用于克隆行)
  • 示例:dt:AddRow("NewPlayer", {Health = 100, MaxHealth = 100})
  • 示例:dt:AddRow("Clone", dt:FindRow("Original")) -- 复制一行

RemoveRow(string RowName)

  • 从数据表中移除指定名称的行。
  • 若该行不存在,则不执行任何操作。

EmptyTable()

  • 移除数据表中的所有行。
  • 注意:不会清除 RowStruct 定义。

GetRowNames()

  • 返回类型: table
  • 返回值: 数据表中所有行名组成的数组(索引从 1 开始的表)。

GetAllRows()

  • 返回类型: table
  • 返回值: 数组(索引从 1 开始的表),每个元素是包含两个字段的表:
    • Name:行名称(字符串)
    • Data:行数据(table 或 UScriptStruct)

ForEachRow(function Callback)

  • 遍历数据表中的所有行,并对每行调用回调函数。
  • 回调参数:string rowName、UScriptStruct rowData。
  • rowData 参数通过引用访问,修改它会直接影响 DataTable。
  • 回调可选择返回 true,以提前结束循环。
  • 示例:
dataTable:ForEachRow(function(rowName, rowData)
    print(string.format("Row: %s, Health: %d", rowName, rowData.Health))
    -- Modify the row directly
    rowData.Health = rowData.Health + 10
    -- Return true to stop iterating
    if rowName == "TargetRow" then
        return true
    end
end)

原文:docs/lua-api/classes/udatatable.md · 固定提交 560411f18512 · 来源与 MIT 许可