NativeBuffer

256 字节缓冲区,用于传递 Any* 参数给 native 函数。GTA V native struct 每字段占 8 字节 (4B 数据 + 4B padding)。

缓冲区本身可在任意回调里创建或按偏移读写;一旦与游戏本机函数(传入缓冲区或在其返回后读结果)配合使用,须在脚本线程中执行。从 gui_tickon_present 触发请用 fiber.run(function() ... end),或写在 on_tickscript.create 回调里。

NativeBuffer.new()NativeBuffer

创建一个零初始化的缓冲区。

按字节偏移读写

精确控制内存偏移量 (手动计算字节位置)。

:get_int(offset)int

从字节偏移 offset 读取 4 字节 int。

参数类型说明
offsetint缓冲区内的字节偏移
:get_float(offset)float

从字节偏移读取 float。

参数类型说明
offsetint字节偏移
:get_bool(offset)bool

从字节偏移读取 int 并转为 bool。

参数类型说明
offsetint字节偏移
:get_vector3(offset)float, float, float

读取 Vector3 (三个 float, 每隔 8 字节)。

参数类型说明
offsetint第一个分量所在字节偏移
:set_int(offset, value)void

写入 int 到字节偏移。

参数类型说明
offsetint字节偏移
valueint写入值
:set_float(offset, value)void

写入 float。

参数类型说明
offsetint字节偏移
valuefloat写入值
:set_bool(offset, value)void

写入 bool (int 0/1)。

参数类型说明
offsetint字节偏移
valuebool写入值
:set_vector3(offset, x, y, z)void

写入 Vector3。

参数类型说明
offsetint起始字节偏移
x, y, zfloat三个分量

按字段索引读写 (推荐)

自动按 index * 8 计算偏移,更安全方便。

:get_field_int(index)int

读取第 index 个字段的 int 值。

参数类型说明
indexint0-based 字段索引
:get_field_float(index)float

读取第 index 个字段的 float 值。

参数类型说明
indexint字段索引
:get_field_bool(index)bool

读取第 index 个字段的 bool 值。

参数类型说明
indexint字段索引
:get_field_vector3(index)float, float, float

从第 index 个字段起读取 Vector3 (连续 3 个字段)。

参数类型说明
indexint起始字段索引
:set_field_int(index, value)void

写入 int 到第 index 个字段。

参数类型说明
indexint字段索引
valueint写入值
:set_field_float(index, value)void

写入 float。

参数类型说明
indexint字段索引
valuefloat写入值
:set_field_bool(index, value)void

写入 bool。

参数类型说明
indexint字段索引
valuebool写入值
:set_field_vector3(index, x, y, z)void

写入 Vector3。

参数类型说明
indexint起始字段索引
x, y, zfloat三个分量
:clear()void

将整个缓冲区清零。

-- 示例: 使用 NativeBuffer 读取纹身数据
local buf = NativeBuffer.new()
FILES.INIT_SHOP_PED_COMPONENT(buf)
if FILES.GET_TATTOO_SHOP_DLC_ITEM_DATA(0, 0, buf) then
    local field0 = buf:get_field_int(0)
    local field1 = buf:get_field_int(1)
end

网络句柄 (Any*) 示例

与示例 online.lua 中的写法相同:将 NativeBuffer 传给需要 Any* 的 NETWORK native,句柄长度常用 13(以 native 声明为准)。若仅需「是否好友」,可直接调用 util.is_friend(player_id)

local buf = NativeBuffer.new()
NETWORK.NETWORK_HANDLE_FROM_PLAYER(player_id, buf, 13)
if NETWORK.NETWORK_IS_HANDLE_VALID(buf, 13) then
    local friend = NETWORK.NETWORK_IS_FRIEND(buf)
end