API Documentation

Native Hook API

เอกสาร API สำหรับ Hook Native Functions ใน FiveM - ดักจับ แก้ไข และควบคุม Native Calls แบบ Real-time

Version 2.0 • Last Updated: December 2025
Introduction

Overview

Lua Native Hook API ให้คุณ Attach Callback ไปยัง Native Function ใดๆ ผ่าน Hash - ตรวจสอบและแก้ไข Arguments ก่อนการทำงาน และควบคุม Return Values ได้อย่างเต็มที่

🎯 Hook Functions

ดักจับ Native Function และรัน Callback ของคุณเองทุกครั้งที่ถูกเรียก

✏️ Modify Arguments

อ่านและแก้ไข Arguments ก่อนที่ Native จะทำงาน

🔄 Control Returns

Override ผลลัพธ์หรือยกเลิก Call ได้อย่างสมบูรณ์

⚡ Zero-restarts Live Patching 🛡️ Friendly API 🔥 Built on Runtime

Note: CitizenFX (CFX) Natives ไม่สามารถ Hook ได้ แต่ Game Natives ทั้งหมดสามารถใช้งานได้

Core API

quality.hooknative

Hook Native ด้วย 64-bit Hash และกำหนด Callback เพื่อจัดการการดักจับ - Callback จะได้รับ ctx object พร้อม Helpers

quality.hooknative([integer] hash, [function] callback) → void
Parameter Type Description
hash number Native Hash (เช่น 0x7F8F1234)
callback function(ctx) ถูกเรียกทุกครั้งที่ Native ทำงาน

Return Value: Return true เพื่อเรียก Native ต้นฉบับ หรือ false เพื่อข้ามและใช้ Return Value ของคุณแทน

Lua
-- Hook a native by hash
quality.hooknative(0xHASHVALUE, function(ctx)
    print("Native intercepted!")

    -- Read first argument as integer
    local arg0 = ctx:GetInt(0)
    print("original arg0:", arg0)

    -- Modify argument
    ctx:SetInt(0, 999)

    -- Override return value
    ctx:SetReturnInt(777)

    -- Return false = do NOT call original native
    return false
end)
Arguments

Get Arguments

Methods สำหรับดึงค่า Arguments จาก Native Context

📊 Integer Types

อ่าน Integer Arguments ตาม Index

  • GetInt(index)
  • GetUInt(index)
  • GetInt64(index)
  • GetUInt64(index)

🔢 Other Types

Float, Boolean, Pointer และ Vector3

  • GetFloat(index)
  • GetBool(index)
  • GetPtr(index)
  • GetVector3(index)
Arguments

Set Arguments

Methods สำหรับแก้ไข Arguments ใน Native Context ก่อน Original Call

🔧 Basic Types

  • SetInt(index, value)
  • SetUInt(index, value)
  • SetFloat(index, value)
  • SetBool(index, value)

⚙️ Advanced Types

  • SetPtr(index, pointer)
  • SetVector3(index, vector)
Math Helpers

Vector3 Operations

Helper Operations สำหรับจัดการ Vector3 Values ใน Hook Callbacks

📍 Read Vector

  • ctx:GetVector3(index)

📝 Write Vector

  • ctx:SetVector3(index, vec)
Lua
-- Offset player position before a teleport native runs
quality.hooknative(0x12345678, function(ctx)
    local dest = ctx:GetVector3(0)

    dest.x = dest.x + 1.0
    dest.y = dest.y + 1.0

    ctx:SetVector3(0, dest)

    return true
end)
Return Pipeline

Return Values

อ่าน แก้ไข หรือ Override Return Values ของ Native ใดก็ได้

📖 Get Results

อ่าน Return Values ต้นฉบับ

  • GetResultInt()
  • GetResultUInt()
  • GetResultInt64()
  • GetResultUInt64()
  • GetResultFloat()
  • GetResultBool()
  • GetResultVector3()
  • GetArgCount()

✍️ Set Returns

Override Return Values

  • SetResultInt(index, value)
  • SetResultUInt(index, value)
  • SetResultFloat(index, value)
  • SetResultBool(index, value)
  • SetReturnInt(value)
  • SetReturnUInt(value)
  • SetReturnFloat(value)
  • SetReturnBool(value)
  • SetReturnchar(string)
  • SetReturnVector3(vector)
Playground

Examples

ตัวอย่างการใช้งานจริงที่แสดงวิธีรวม Argument Hooks และ Return Control

Basic Hook & Modify Argument

Lua
-- Intercept GIVE_WEAPON_TO_PED and modify its arguments
quality.hooknative(0xBF0FD6E56C964FCB, function(ctx)

    local ped        = ctx:GetInt(0)
    local weaponHash = ctx:GetUInt(1)
    local ammo       = ctx:GetInt(2)
    local hidden     = ctx:GetBool(3)
    local forceHand  = ctx:GetBool(4)

    print("[LuaHook] GIVE_WEAPON_TO_PED")
    print("  ped:        ", ped)
    print("  weaponHash: ", string.format("0x%X", weaponHash))
    print("  ammo:       ", ammo)

    -- ⭐ Change weapon to Stun Gun
    ctx:SetUInt(1, 0x3656C8C1)

    return true
end)

Override Return Value & Cancel Original

Lua
-- Override a Vector3 return value and block the original native
quality.hooknative(0x3FEF770D40960D5A, function(ctx)

    local fake = Vector3.new(1000.0, 1000.0, 100.0)

    ctx:SetReturnVector3(fake)

    -- Returning false = Block original native entirely
    return false
end)

Block Native Call Completely

Lua
-- Block a native entirely (no return value, no original execution)
quality.hooknative(0x00123454, function(ctx)
    -- Returning false blocks the original call
    return false
end)

Unhook Native (Remove Hook)

Lua
-- Remove the hook completely
quality.unhook(0xBF0FD6E56C964FCB)

print("[Unhook] GIVE_WEAPON_TO_PED restored to original behavior")