เอกสาร API สำหรับ Hook Native Functions ใน FiveM - ดักจับ แก้ไข และควบคุม Native Calls แบบ Real-time
Lua Native Hook API ให้คุณ Attach Callback ไปยัง Native Function ใดๆ ผ่าน Hash - ตรวจสอบและแก้ไข Arguments ก่อนการทำงาน และควบคุม Return Values ได้อย่างเต็มที่
ดักจับ Native Function และรัน Callback ของคุณเองทุกครั้งที่ถูกเรียก
อ่านและแก้ไข Arguments ก่อนที่ Native จะทำงาน
Override ผลลัพธ์หรือยกเลิก Call ได้อย่างสมบูรณ์
Note: CitizenFX (CFX) Natives ไม่สามารถ Hook ได้ แต่ Game Natives ทั้งหมดสามารถใช้งานได้
Hook Native ด้วย 64-bit Hash และกำหนด Callback เพื่อจัดการการดักจับ - Callback จะได้รับ ctx object พร้อม Helpers
| Parameter | Type | Description |
|---|---|---|
hash |
number | Native Hash (เช่น 0x7F8F1234) |
callback |
function(ctx) | ถูกเรียกทุกครั้งที่ Native ทำงาน |
Return Value: Return true เพื่อเรียก Native ต้นฉบับ
หรือ false เพื่อข้ามและใช้ Return Value ของคุณแทน
-- 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)
Methods สำหรับดึงค่า Arguments จาก Native Context
อ่าน Integer Arguments ตาม Index
Float, Boolean, Pointer และ Vector3
Methods สำหรับแก้ไข Arguments ใน Native Context ก่อน Original Call
Helper Operations สำหรับจัดการ Vector3 Values ใน Hook Callbacks
-- 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)
อ่าน แก้ไข หรือ Override Return Values ของ Native ใดก็ได้
อ่าน Return Values ต้นฉบับ
Override Return Values
ตัวอย่างการใช้งานจริงที่แสดงวิธีรวม Argument Hooks และ Return Control
-- 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 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 a native entirely (no return value, no original execution) quality.hooknative(0x00123454, function(ctx) -- Returning false blocks the original call return false end)
-- Remove the hook completely quality.unhook(0xBF0FD6E56C964FCB) print("[Unhook] GIVE_WEAPON_TO_PED restored to original behavior")