FiveM Native คืออะไร
FiveM Native คือ Function ที่ FiveM Script ใช้ติดต่อกับระบบของ GTA V และ Cfx.re โดยตรง เพื่ออ่านข้อมูลหรือสั่งให้เกมทำสิ่งต่าง ๆ เช่น หา Player Ped, อ่านตำแหน่ง, สร้างรถ, เล่น Animation, ควบคุม Entity, ตรวจปุ่ม, สร้าง Blip หรือจัดการ Player ฝั่ง Server
ตัวอย่าง Native ที่ FiveM Developer พบได้บ่อยคือ
local ped = PlayerPedId()
จากนั้นสามารถหา Coordinates ได้ด้วย
local coords = GetEntityCoords(ped)
แนวคิดคือ
FiveM Script
↓
Native
↓
GTA V / FiveM Engine
↓
ผลลัพธ์หรือ Action
Native จึงถือเป็นหนึ่งในพื้นฐานสำคัญที่สุดของ FiveM Development เพราะแม้คุณจะใช้ ESX, QBCore, Qbox หรือ Framework อื่น สุดท้าย Gameplay จำนวนมากก็ยังพึ่ง Native อยู่เบื้องหลัง
① FiveM Native คืออะไร
Native คือ Function ที่ระบบเกมหรือ Cfx.re เปิดให้ Script เรียกใช้งาน
ตัวอย่างเช่น
PlayerPedId()
ใช้หา Ped ของ Local Player
GetEntityCoords(entity)
ใช้ดึง Coordinates ของ Entity
หรือ
GetPlayerName(playerId)
ใช้ดึงชื่อ Player ใน Context ที่ Native รองรับ
ดังนั้น Native สามารถมีหน้าที่ได้ทั้ง
อ่านข้อมูล
+
สั่งให้เกมทำงาน
② Native มาจากไหน
Native จำนวนมากมาจาก Function ที่มีอยู่ใน GTA V
Cfx.re นำมา Mapping ให้ Scripting Runtime ของ FiveM สามารถเรียกใช้งานได้
นอกจากนี้ยังมี Native บางส่วนที่ Cfx.re เพิ่มขึ้นเองสำหรับ FiveM
ดังนั้นเมื่อเปิด Native Reference คุณจะพบทั้ง Function ที่เกี่ยวข้องกับ GTA V Gameplay และ Function ที่เกี่ยวข้องกับ FiveM Server/Runtime
③ Native ต่างจาก Lua Function อย่างไร
Lua Function ทั่วไปคือ Function ที่ Developer เขียนเอง
ตัวอย่าง
local function calculatePrice(price, amount)
return price * amount
end
แต่ Native เช่น
GetEntityCoords(entity)
ไม่ได้เป็น Function ที่คุณเขียนเอง
มันเป็น Function ที่ Runtime เปิดให้ Script ติดต่อกับ Game Engine
สรุปคือ
Lua Function
→ Developer เขียน
Native
→ Game/FiveM เปิดให้ Script เรียก
④ FiveM Native ใช้ทำอะไรได้บ้าง
Native ครอบคลุมระบบจำนวนมาก เช่น
Player
Ped
Vehicle
Entity
Object
Camera
Animation
Controls
Weapons
Blips
Text
HUD
Audio
Weather
Time
Network
Server Player
Resource
Routing/Entity Management
ดังนั้น Resource Gameplay แทบทุกประเภทต้องใช้ Native บางส่วน
⑤ ตัวอย่าง Native ง่ายที่สุด
ตัวอย่างหา Player Ped
local ped = PlayerPedId()
print(ped)
PlayerPedId() คืน Entity Handle ของ Local Player Ped
จากนั้นเราสามารถนำ ped ไปใช้กับ Native อื่นได้
เช่น
local coords = GetEntityCoords(ped)
นี่เป็น Pattern ที่พบได้มากใน Client Script
⑥ Ped คืออะไร
Ped มาจากคำว่า Pedestrian
ใน GTA/FiveM ใช้แทน Character Entity เช่น
Player Character
NPC
Police NPC
Civilian NPC
Player เองก็มี Ped
ดังนั้นเมื่อ Script ต้องควบคุมตัวละคร Player มักเริ่มจาก
local ped = PlayerPedId()
⑦ Entity คืออะไร
Entity เป็นแนวคิดที่กว้างกว่า Ped
ตัวอย่าง Entity ได้แก่
Ped
Vehicle
Object
Native จำนวนมากใช้ Entity Handle เป็น Parameter
เช่น
GetEntityCoords(entity)
หรือ
GetEntityHeading(entity)
เพราะ Function เหล่านี้ไม่ได้จำกัดเฉพาะ Player Ped
⑧ GetEntityCoords ใช้ทำอะไร
ใช้หา Coordinates ของ Entity
ตัวอย่าง
local ped = PlayerPedId()
local coords =
GetEntityCoords(ped)
print(coords)
สำหรับ Lua FiveM ค่า Coordinates สามารถถูกใช้งานร่วมกับ Vector Type ของ CfxLua ได้สะดวก
Use Case เช่น
ตรวจระยะ
Marker
Teleport
Shop
Garage
Job
Mission
⑨ Native มี Parameter คืออะไร
Parameter คือค่าที่ต้องส่งเข้า Function
ตัวอย่าง
GetEntityCoords(entity)
Native นี้ต้องได้รับ
entity
ตัวอย่างจริง
local ped = PlayerPedId()
local coords =
GetEntityCoords(ped)
ถ้าส่ง Parameter ผิด Type หรือผิด Entity อาจทำให้ Result ไม่ถูกต้องหรือ Script เกิด Error/Behavior ที่ไม่คาดคิด
⑩ Native มี Return Value คืออะไร
Native บางตัว Return ค่า
เช่น
local ped =
PlayerPedId()
ped คือ Return Value
หรือ
local coords =
GetEntityCoords(ped)
coords คือค่าที่ Native คืนกลับ
แต่ Native บางตัวมีหน้าที่ทำ Action และอาจไม่ได้มี Return Value ที่ Developer ต้องใช้
⑪ Native ที่ทำ Action คืออะไร
ตัวอย่าง Concept เช่น
Set...
Task...
Draw...
Clear...
Delete...
Function เหล่านี้มักสั่งให้เกมเปลี่ยน State หรือทำ Action
เช่น Native ที่ขึ้นต้นด้วย
Set
จำนวนมากมีหน้าที่ตั้งค่าบางอย่าง
แต่ไม่ควรเดา Behavior จากชื่ออย่างเดียว
ควรอ่าน Native Reference ทุกครั้ง
⑫ Native Reference คืออะไร
Native Reference คือเอกสารที่รวบรวม Native ของ FiveM/GTA V
ข้อมูลสำคัญที่ควรดู ได้แก่
Native Name
Namespace
Hash
Parameters
Return Type
API Set
Description
Examples
การอ่าน Native Reference เป็นทักษะที่ FiveM Developer ควรฝึกให้คล่อง
⑬ API Set คืออะไร
นี่เป็นจุดสำคัญมาก
Native Reference ระบุว่า Native ใช้กับ Context ใด
หลัก ๆ เช่น
client
server
shared
ความหมายคือ
client
→ ใช้ฝั่ง Client
server
→ ใช้ฝั่ง Server
shared
→ ใช้ได้ทั้ง Client และ Server
ก่อน Copy Native จาก Internet ควรตรวจ API Set ก่อนเสมอ
⑭ ทำไม Native บางตัวใช้ Client ได้แต่ Server ใช้ไม่ได้
เพราะ Client มี Game Instance ของ GTA V อยู่
Client จึงสามารถเข้าถึงระบบอย่าง
Camera
Controls
HUD
Local Ped
Rendering
Animation
Game UI
โดยตรง
Server ไม่มี Local GTA V Game Instance แบบเดียวกัน
จึงไม่สามารถใช้ Client-only Native ทุกตัวได้
⑮ ตัวอย่าง Native ฝั่ง Client
เช่น
PlayerPedId()
ใช้หา Local Player Ped
อีกตัวอย่าง
IsControlJustReleased(
0,
38
)
ใช้ตรวจ Input ของ Client
สิ่งเหล่านี้เป็นธรรมชาติของ Client Script เพราะเกี่ยวข้องกับ Player Local Game
⑯ ตัวอย่าง Native ฝั่ง Server
ตัวอย่างง่ายคือ
GetPlayerName(playerId)
ใช้หา Player Name ตาม Player ID ใน Server Context ที่รองรับ
เช่น
RegisterCommand(
'playername',
function(source)
local name =
GetPlayerName(source)
print(name)
end,
false
)
Server-side Native มักเกี่ยวข้องกับ Player, Entity และ Server Management ตาม API ที่ FiveM เปิดให้
⑰ Native แบบ Shared คืออะไร
Native ที่ API Set เป็น Shared สามารถใช้ได้ทั้ง
Client
+
Server
แต่ควรอ่าน Parameter และ Behavior ให้ครบ เพราะ Context ของข้อมูลที่ใช้ร่วมด้วยอาจแตกต่างกัน
อย่าคิดว่า Native ที่ใช้ได้สองฝั่งจะให้ผลเหมือนกันทุกสถานการณ์โดยอัตโนมัติ
⑱ Client Native ใส่ใน server.lua ได้ไหม
ถ้า Native เป็น Client-only ก็ไม่ควร
ตัวอย่าง
local ped =
PlayerPedId()
เหมาะกับ client.lua
ถ้า Copy ไป server.lua โดยไม่ดู API Set อาจเจอปัญหา Function ไม่มีใน Context นั้น
นี่เป็น Error ที่มือใหม่พบได้บ่อย
⑲ Server Native ใส่ใน client.lua ได้ไหม
หลักเดียวกัน
ถ้า Native ถูกระบุเป็น Server-only ก็ไม่ควรสมมติว่า Client สามารถเรียกได้
ก่อนใช้งานให้ตรวจ
API Set
ใน Native Reference
นี่เป็นวิธีป้องกัน Error ที่ง่ายที่สุดวิธีหนึ่ง
⑳ Native กับ Client Script เกี่ยวข้องอย่างไร
Client Script ใช้ Native อย่างหนักมาก
ตัวอย่างระบบ Garage
PlayerPedId
↓
GetEntityCoords
↓
ตรวจระยะ Garage
↓
ตรวจ Input
↓
เปิด Menu
ระบบ Gameplay ส่วนใหญ่จึงเป็นการผสม
Lua Logic
+
FiveM Native
+
Framework API
เข้าด้วยกัน
㉑ Native กับ Server Script เกี่ยวข้องอย่างไร
Server-side Native ช่วย Server จัดการข้อมูลหรือ Entity ที่ FiveM รองรับ
แต่ Business Logic อย่าง
Money
Inventory
Database
Permission
มักไม่ได้ใช้ Native เพียงอย่างเดียว
อาจต้องใช้
Framework API
Database Library
Server State
Exports
Events
ร่วมกัน
Native เป็นเครื่องมือหนึ่งใน Architecture ไม่ใช่ทุกอย่าง
㉒ Native กับ Framework ต่างกันอย่างไร
สมมติคุณใช้ QBCore
Framework อาจมี Function
GetPlayer
AddMoney
RemoveItem
สิ่งเหล่านี้เป็น Framework API
ไม่ใช่ GTA Native
แต่ Function อย่าง
PlayerPedId()
GetEntityCoords()
เป็น Native
จำง่าย ๆ ว่า
Native
→ Game/FiveM API
Framework API
→ ESX/QBCore/Qbox API
㉓ Native กับ Export ต่างกันอย่างไร
Native ถูกเปิดโดย Game/FiveM Runtime
Export ถูกเปิดโดย Resource
ตัวอย่าง
PlayerPedId()
คือ Native
แต่
exports['my_inventory']:HasItem(...)
คือ Export ของ Resource
ทั้งสองเป็น Function Call แต่แหล่งที่มาต่างกันมาก
㉔ Native กับ Event ต่างกันอย่างไร
Native ใช้สั่งหรืออ่าน Game/FiveM Engine
Event ใช้สื่อสารระหว่าง Script/Runtime
ตัวอย่าง
local ped =
PlayerPedId()
คือ Native Call
ส่วน
TriggerServerEvent(
'garage:store'
)
คือ Network Event
Resource จริงมักใช้ทั้งสองอย่างร่วมกัน
㉕ ตัวอย่าง Client → Native → Server
ระบบ Garage อาจทำ
Client
↓
PlayerPedId
↓
GetEntityCoords
↓
ตรวจว่าอยู่หน้า Garage
↓
TriggerServerEvent
↓
Server ตรวจ Ownership
↓
Database
นี่แสดงว่า Native และ Event มีหน้าที่คนละอย่าง
Native จัด Game Interaction
Event จัด Client/Server Communication
㉖ Native ใช้กับ Lua อย่างไร
ใน Lua FiveM Native จำนวนมากถูก Mapping เป็น Global Functions
ตัวอย่าง
local ped =
PlayerPedId()
ไม่จำเป็นต้องเขียน Prefix ยาว ๆ ใน Native Call ทั่วไป
อีกตัวอย่าง
local coords =
GetEntityCoords(ped)
ทำให้ Lua FiveM อ่านค่อนข้างตรงกับ Native Name
㉗ Native ใช้กับ JavaScript อย่างไร
JavaScript FiveM ก็ Mapping Native เป็น Global Functions
ตัวอย่าง
const ped = PlayerPedId();
const coords = GetEntityCoords(ped);
แนวคิดเหมือน Lua
เพียง Syntax ของภาษาแตกต่างกัน
Official typings ยังช่วยให้ Editor รู้จัก Native และ API ได้ดีขึ้น
㉘ Native ใช้กับ C# อย่างไร
C# มี Native API ผ่าน CitizenFX Core
ตัวอย่าง Concept มักพบการ Import
using static CitizenFX.Core.Native.API;
จากนั้นเรียก Native ผ่าน C# Mapping
C# ยังมี Wrapper Classes บางประเภท เช่น Player/Ped/Vehicle ที่ช่วยให้ Code เป็น Object-oriented มากขึ้น
แต่พื้นฐานยังเชื่อมกับ Native API ของ FiveM
㉙ Native Name มาจากไหน
Native Reference แสดงชื่อในรูปแบบที่อ่านง่าย เช่น
PLAYER_PED_ID
GET_ENTITY_COORDS
GET_ENTITY_HEADING
เมื่อใช้กับ Lua/JavaScript Mapping จะพบรูปแบบ เช่น
PlayerPedId
GetEntityCoords
GetEntityHeading
Runtime เป็นผู้ Mapping Native เหล่านี้ให้เข้ากับรูปแบบของ Scripting Language
㉚ Native Hash คืออะไร
Native มี Identifier/Hash อยู่เบื้องหลัง
ใน Native Reference จึงมักเห็นข้อมูล Hash ของ Function
สำหรับ Native ที่มี Mapping ชื่อชัดเจน Developer ทั่วไปควรใช้ชื่อ Function ที่อ่านง่าย
เช่น
GetEntityCoords(entity)
แทนการพยายามจำ Hash
Hash มีประโยชน์มากกว่าในกรณีพิเศษหรือการอ้างอิงระดับล่าง
㉛ ต้องจำ Native ทั้งหมดไหม
ไม่ต้อง
FiveM/GTA V มี Native จำนวนมากเกินกว่าจะจำทั้งหมดอย่างมีประโยชน์
Developer ควรจำเฉพาะที่ใช้บ่อย เช่น
PlayerPedId
GetEntityCoords
GetEntityHeading
RegisterCommand
จากนั้นฝึก Search Native Reference ให้เร็ว
ทักษะหา Documentation สำคัญกว่าการจำชื่อ Native หลายพันตัว
㉜ วิธีหา Native ที่ต้องการ
เริ่มจากคิด Intent
ตัวอย่างต้องการหา
Player อยู่ในรถหรือไม่
ค้น Keyword
ped vehicle
หรือถ้าต้องการ
หาตำแหน่ง Entity
ค้น
entity coords
จากนั้นตรวจ
Native Name
API Set
Parameters
Return
Description
อย่า Copy Native จากโพสต์เก่าโดยไม่ตรวจ Reference ปัจจุบัน
㉝ Namespace คืออะไร
Native ถูกจัดกลุ่มตาม Namespace เช่นแนวคิด
PLAYER
PED
ENTITY
VEHICLE
CAM
HUD
NETWORK
Namespace ช่วยให้ค้น Native ที่เกี่ยวข้องได้ง่ายขึ้น
ถ้ากำลังทำรถ สามารถเริ่มดู Native กลุ่ม Vehicle/Entity
ถ้าทำตัวละครก็อาจดู Player/Ped
㉞ Native Parameter Type สำคัญไหม
สำคัญมาก
สมมติ Native ต้องการ
Entity
แต่ส่ง String เข้าไป
Logic จะผิด
หรือ Native ต้องการ
Player ID
แต่ส่ง Ped Handle
ก็เป็นคนละ Identifier
FiveM Developer ต้องแยกให้ได้ระหว่าง
Player ID
Server ID
Ped
Entity Handle
Network ID
Model Hash
เพราะทั้งหมดเป็นคนละ Concept
㉟ Ped Handle กับ Player ID เหมือนกันไหม
ไม่
ตัวอย่าง
local ped =
PlayerPedId()
ค่า ped คือ Ped Entity Handle
ไม่ใช่ Server Player Source
ดังนั้นอย่าส่ง ped ไป Server แล้วคิดว่า Server จะใช้มันเหมือน Player Source ได้เสมอ
ต้องใช้ Identifier ให้ถูก Context
㊱ Entity Handle กับ Network ID เหมือนกันไหม
ไม่
Entity Handle เป็น Reference ของ Entity ใน Runtime Context
Network ID ใช้สำหรับอ้างถึง Networked Entity ผ่าน Networking Layer
เมื่อ Entity ต้องถูกอ้างข้าม Client/Server หรือหลาย Client Developer ต้องเข้าใจ Network ID เพิ่มเติม
หัวข้อนี้จะลงรายละเอียดในบทความต่อ ๆ ไป
㊲ Model Hash คืออะไร
Native หลายตัวที่เกี่ยวกับ Vehicle, Ped หรือ Object ใช้ Model Hash
ตัวอย่าง Concept
Model Name
↓
Hash
↓
Native
ก่อน Spawn Entity ยังมักต้อง
Request Model
รอ Model โหลด
Create Entity
Cleanup Model/Entity ตามความเหมาะสม
จึงไม่ควร Copy CreateVehicle() เพียงบรรทัดเดียวแล้วคิดว่าจบทุกกรณี
㊳ Native ต้องรอ Model โหลดไหม
Native ที่สร้าง Entity จาก Model มักต้องให้ Model พร้อมก่อนตาม Workflow ของ Game
Pattern ทั่วไปคือ
Request Model
↓
ตรวจ HasModelLoaded
↓
Wait
↓
Create Entity
นี่แสดงให้เห็นว่าการใช้ Native ที่ถูกต้องไม่ได้มีแค่รู้ชื่อ Function แต่ต้องเข้าใจ Lifecycle ของระบบด้วย
㊴ Native และ CreateThread เกี่ยวข้องอย่างไร
Client Script มักใช้ Native ใน Thread
ตัวอย่าง
CreateThread(function()
while true do
Wait(1000)
local ped =
PlayerPedId()
local coords =
GetEntityCoords(ped)
print(coords)
end
end)
Native ถูกเรียกทุก 1 วินาทีในตัวอย่างนี้
ถ้าเปลี่ยนเป็น Wait(0) จะถูกเรียกถี่กว่ามาก
㊵ Native ทุกตัวเรียกทุก Frame ได้ไหม
ทางเทคนิค Native หลายตัวสามารถถูกเรียกบ่อยได้
แต่ไม่ได้หมายความว่าควรเรียกทุกตัวทุก Frame
ควรถามว่า
จำเป็นต้องอัปเดตทุก Frameจริงหรือ?
ตัวอย่าง Marker Rendering บางส่วนอาจต้องทำทุก Frame
แต่การตรวจบาง State อาจทำทุก
250 ms
500 ms
1000 ms
ก็เพียงพอ
㊶ Native Call มี Cost หรือไม่
มี
Native Call ต้องข้ามจาก Script Runtime ไปยัง Game/FiveM Native Layer
แต่ Performance Cost แตกต่างกันตาม Native และ Logic รอบข้าง
ปัญหามักเกิดเมื่อ
เรียก Native จำนวนมาก
×
ทุก Frame
×
ผู้เล่น/Entity จำนวนมาก
ดังนั้นควร Optimize จาก Profiler/Resmon และ Use Case จริง
㊷ ตัวอย่าง Native Loop ที่ควรระวัง
CreateThread(function()
while true do
Wait(0)
local ped =
PlayerPedId()
local coords =
GetEntityCoords(ped)
-- heavy processing
end
end)
ถ้าต้องการ Coordinates ทุก Frameจริงก็อาจมีเหตุผล
แต่ถ้าใช้เพียงตรวจว่า Player อยู่ใกล้ Shop หรือไม่ สามารถออกแบบ Adaptive Wait หรือ Interval ที่เหมาะกว่าได้
㊸ Adaptive Wait คืออะไร
ตัวอย่าง Concept
CreateThread(function()
while true do
local sleep = 1000
local ped =
PlayerPedId()
local coords =
GetEntityCoords(ped)
if isNearLocation(coords) then
sleep = 0
-- draw / interaction
end
Wait(sleep)
end
end)
เมื่ออยู่ไกล
Wait 1000
เมื่อเข้าใกล้
Wait 0
ช่วยลดงาน Client เมื่อไม่จำเป็น
㊹ ควร Cache Native Result ไหม
บางกรณีควร
ตัวอย่าง
local ped =
PlayerPedId()
ถ้าใช้ซ้ำหลายครั้งภายใน Iteration เดียว ไม่จำเป็นต้องเรียก Native ซ้ำทุกบรรทัด
เช่น
local ped =
PlayerPedId()
local coords =
GetEntityCoords(ped)
local heading =
GetEntityHeading(ped)
อ่านง่ายกว่าเรียก PlayerPedId() ซ้ำหลายครั้ง
แต่ต้องระวัง Cache ข้อมูลนานเกินจน State เปลี่ยน
㊺ Cache Ped ไว้ถาวรได้ไหม
ไม่ควรสมมติว่า Player Ped Handle จะคงเดิมตลอด Session ทุกกรณี
Player สามารถมีเหตุการณ์ที่ทำให้ Ped เปลี่ยนได้ เช่น Character/Ped Change ตามระบบ
ดังนั้น Cache ระยะสั้นใน Tick/Function ดี
แต่ Cache ระยะยาวต้องรู้ Lifecycle ของข้อมูลนั้น
㊻ Native Return nil หรือค่าไม่คาดคิดได้ไหม
ขึ้นอยู่กับ Native
Developer ควรอ่าน Return Type และเงื่อนไขของ Native
อย่าสมมติว่า Native ทุกตัวจะคืน Entity ที่ Valid เสมอ
ก่อนทำ Action สำคัญกับ Entity สามารถตรวจ Validation ตาม Native/API ที่เกี่ยวข้อง
㊼ Native กับ DoesEntityExist
เมื่อ Script จัดการ Entity อาจต้องตรวจว่า Entity ยังมีอยู่หรือไม่
แนวคิด เช่น
if DoesEntityExist(entity) then
-- entity logic
end
มีประโยชน์เมื่อ Entity อาจถูกลบหรือไม่ได้ถูกสร้างสำเร็จ
โดยเฉพาะ Script ที่เก็บ Handle ไว้ใช้ภายหลัง
㊽ Native กับ Networked Entity
เมื่อ Spawn Vehicle/Ped/Object ใน Multiplayer ต้องคิดเรื่อง
Networked?
Who owns it?
Who can see it?
Network ID?
Scope?
ไม่ใช่แค่เรียก Native สร้าง Entity สำเร็จแล้วจบ
เมื่อ Resource ซับซ้อนขึ้น OneSync และ Entity Ownership จะสำคัญมาก
㊾ Native ฝั่ง Server ทำ Entity ได้ไหม
FiveM/OneSync มี Server-side Entity Natives สำหรับ Use Case ที่รองรับ
Server จึงสามารถสร้างหรือจัดการ Entity บางประเภทจาก Server ได้
แต่ต้องอ่าน API Set และ Documentation ของ Native ที่ใช้
อย่าเอา Client Native CreateVehicle ตัวหนึ่งไปวาง Server แล้วสมมติว่าเป็น API เดียวกันโดยไม่ตรวจ Reference
㊿ Native กับ OneSync เกี่ยวข้องอย่างไร
OneSync ทำให้ FiveM Server จัดการ Networked Player/Entity ได้ลึกขึ้น
Native ที่เกี่ยวข้องกับ
Entity
Network ID
Player
Routing
Ownership
จึงมักถูกใช้ร่วมกับ OneSync
แต่ Native ไม่ได้ทำให้ Entity Sync ถูกต้องอัตโนมัติ
Architecture ยังต้องออกแบบให้เข้าใจ Networking
51 Native กับ State Bags
บาง State ไม่จำเป็นต้องเรียก Native/Event ซ้ำไปซ้ำมา
State Bags สามารถใช้ Replicate State บางประเภทได้
ตัวอย่าง Concept
Native
→ ทำ Action กับ Entity
State Bag
→ เก็บ State ที่เกี่ยวข้องกับ Entity
เช่น
Vehicle locked
Fuel
Custom State
เลือกเครื่องมือให้ตรงกับงาน
52 Native กับ Routing Bucket
Routing Bucket จัดการ Instance/Dimension ผ่าน Server-side API ที่เกี่ยวข้อง
หาก Script ใช้ Native จัด Entity ร่วมกับ Routing Bucket ต้องเข้าใจว่า Player และ Entity อยู่ Bucket ไหน
ไม่เช่นนั้นอาจเกิดปัญหา Entity หรือ Player มองไม่เห็นกันตามที่คาด
53 Native กับ NUI ต่างกันอย่างไร
Native ติดต่อ Game/FiveM Engine
NUI ติดต่อ Browser-based UI
ตัวอย่าง
PlayerPedId()
คือ Native
ส่วน
SendNUIMessage({
action = 'open'
})
เป็น NUI API
Resource เช่น Phone หรือ Inventory มักใช้ทั้ง Native และ NUI ร่วมกัน
54 Native กับ Animation
Animation Script มักใช้ Native หลายตัวต่อกัน
Flow ทั่วไป
Request Anim Dictionary
↓
Wait Until Loaded
↓
หา Player Ped
↓
Task Animation
↓
Cleanup
นี่เป็นตัวอย่างว่าการใช้ Native จริงมักเป็น Workflow ไม่ใช่ Function เดียว
55 Native กับ Vehicle
ระบบ Vehicle อาจใช้ Native เช่น
ตรวจว่า Ped อยู่ใน Vehicle
หา Vehicle
อ่าน Coordinates
อ่าน Heading
ปรับ State
แต่ถ้ามี Ownership/Database ต้องส่ง Server Logic เพิ่มด้วย
Native จัด Gameplay Entity
Database จัด Persistent Ownership
อย่าสับสนสองส่วนนี้
56 Native กับ Controls
Client Native สามารถตรวจ Input เช่น Keyboard/Game Controls
ตัวอย่างแนวคิด
if IsControlJustReleased(
0,
38
) then
-- E released
end
เหมาะกับ Interaction เช่น
กด E เปิด Shop
กด E เข้า Garage
กด E เริ่ม Job
แต่ Action สำคัญหลังจากนั้นควรให้ Server Validate หากมีผลต่อ Economy/Permission
57 Native กับ Security
Native ฝั่ง Client ไม่ได้ทำให้ข้อมูลน่าเชื่อถือ
ตัวอย่าง Client ใช้ Native ตรวจ
Player อยู่จุด Mission
แล้วส่ง
missionComplete
Server ยังควรตรวจ State/Position ตามความจำเป็น
Client Native เหมาะกับ Gameplay และ UX แต่ไม่ควรเป็น Authority ของ Reward
58 Native Error ที่มือใหม่เจอบ่อย
Function ไม่มี
อาจใช้ Native ผิด API Set
ส่ง Parameter ผิด
เช่นส่ง Player ID แทน Entity
Entity ไม่มี
Handle ไม่ Valid หรือ Entity ถูกลบแล้ว
Native ไม่ทำงาน
Context ผิด
Script FPS ตก
เรียก Native หนักหรือถี่เกินไป
Result ผิด
เข้าใจ Return Type หรือ Identifier ผิด
วิธีแก้ที่ดีที่สุดคือกลับไปดู Native Reference
59 วิธี Debug Native FiveM
เริ่มจากแยกทีละขั้น
local ped =
PlayerPedId()
print(
'ped:',
ped
)
จากนั้น
local coords =
GetEntityCoords(ped)
print(
'coords:',
coords
)
ถ้าทำงานทีละขั้น จะรู้ว่า Error เกิดที่ Native ไหน
Client Error ดู F8 Console
Server Error ดู Server Console
60 Checklist ก่อนใช้ FiveM Native
ก่อน Copy Native มาใช้ ให้ตรวจ
① Native ชื่ออะไร
② API Set เป็น Client, Server หรือ Shared
③ Parameter ต้องเป็นอะไร
④ Return Type คืออะไร
⑤ Entity/Player Identifier ใช้ถูกประเภทไหม
⑥ Native ต้องใช้ Model/Dictionary ที่โหลดก่อนหรือไม่
⑦ ต้องเรียกทุก Frameจริงไหม
⑧ Native Result ควร Cache หรือไม่
⑨ Entity ยัง Valid อยู่ไหม
⑩ Native นี้เกี่ยวกับ Networked Entity หรือไม่
⑪ ต้องใช้ Server Validation เพิ่มหรือไม่
⑫ Documentation ปัจจุบันว่าอย่างไร
Checklist นี้ช่วยลด Bug ได้มากกว่าการ Copy Code แล้วแก้ Error ทีหลัง
คำถามที่พบบ่อยเกี่ยวกับ FiveM Native
FiveM Native คืออะไร
คือ Function ที่ Game หรือ Cfx.re เปิดให้ Script เรียกเพื่ออ่านข้อมูลหรือสั่ง Action ใน GTA V/FiveM
Native ใช้กับ Lua ได้ไหม
ได้ FiveM Mapping Native ให้ Lua เรียกเป็น Function ได้โดยตรง
Native ใช้กับ JavaScript ได้ไหม
ได้ และถูก Mapping เป็น Global JavaScript Functions
Native ใช้กับ C# ได้ไหม
ได้ผ่าน CitizenFX Core Native API และ Wrapper ที่เกี่ยวข้อง
Native ใช้ได้ทั้ง Client และ Server ไหม
บางตัวได้ แต่บางตัวเป็น Client-only หรือ Server-only ต้องดู API Set ใน Native Reference
Native Client-only ใช้ server.lua ได้ไหม
ไม่ควร หาก API Set ระบุ Client-only ต้องใช้ใน Client Context
PlayerPedId คืออะไร
เป็น Native สำหรับหา Local Player Ped ใน Client
GetEntityCoords คืออะไร
เป็น Native สำหรับอ่าน Coordinates ของ Entity
ต้องจำ Native ทั้งหมดไหม
ไม่ต้อง ควรจำเฉพาะตัวที่ใช้บ่อยและฝึกค้น Native Reference
Native Hash คืออะไร
เป็น Identifier ระดับล่างของ Native แต่ Developer ทั่วไปควรใช้ชื่อ Function ที่ Mapping ไว้เมื่อมี
Native กับ Export ต่างกันอย่างไร
Native มาจาก Game/FiveM ส่วน Export เป็น Function API ที่ Resource เปิดให้ Resource อื่นเรียก
Native กับ Event ต่างกันอย่างไร
Native ติดต่อ Engine ส่วน Event ใช้สื่อสารระหว่าง Script/Client/Server
Native ทำให้ FPS ตกได้ไหม
ได้หากเรียก Native จำนวนมากหรือ Logic หนักทุก Frameโดยไม่จำเป็น
Server มี Native ไหม
มี FiveM มี Server-side Native/API และ Native บางตัวเป็น Shared
จะรู้ได้อย่างไรว่า Native ใช้ฝั่งไหน
ดูค่า API set ใน Native Reference
สรุป FiveM Native คืออะไร
FiveM Native คือ Function ที่ Script ใช้เป็นสะพานเชื่อมไปยัง GTA V และ FiveM Engine
ตัวอย่างง่าย ๆ คือ
local ped =
PlayerPedId()
local coords =
GetEntityCoords(ped)
Flow คือ
Lua / JavaScript / C#
↓
Native
↓
FiveM / GTA V
↓
Game Data หรือ Action
สิ่งสำคัญที่สุดไม่ใช่การจำ Native จำนวนมาก แต่คือการอ่าน Native Reference ให้เป็น โดยเฉพาะ
Native Name
API Set
Parameters
Return Type
Description
และต้องแยก
Client Native
Server Native
Shared Native
ให้ถูก Context
Native ยังต้องถูกใช้อย่างระมัดระวังด้าน Performance โดยเฉพาะการเรียก Function จำนวนมากทุก Frame และต้องเข้าใจ Identifier ต่าง ๆ เช่น Ped, Entity Handle, Player ID และ Network ID ให้ถูกต้อง
สำหรับผู้ที่เรียน FiveM Developer กับ comsiam เมื่อเข้าใจ Native แล้ว คุณจะเริ่มอ่าน FiveM Script ได้ง่ายขึ้นมาก เพราะ Code อย่าง Ped, Vehicle, Marker, Camera, Animation และ Entity ล้วนเต็มไปด้วย Native Calls
หัวข้อถัดไปของ comsiam จะเข้าสู่ CreateThread ซึ่งเป็นอีกพื้นฐานสำคัญของ Lua FiveM เพราะ Native จำนวนมากถูกเรียกอยู่ภายใน Thread และ Loop โดยตรง และถ้าออกแบบ Thread ไม่ดี Resource อาจกิน CPU หรือทำให้ FPS ลดลงได้
Comments
Post a Comment