Game

ฟังก์ชันอรรถประโยชน์ฝั่ง Client สำหรับค้นหา/จัดการเอนทิตี ยานพาหนะ และการโต้ตอบกับโลกเกม ผ่าน `ESX.Game.*`

Game (Client)

หน้านี้รวบรวมฟังก์ชัน ESX.Game.* ที่ใช้บ่อยสำหรับงานฝั่ง Client-side

ครอบคลุมการค้นหา/จัดการเอนทิตี (player / ped / vehicle / object) การตรวจสอบพื้นที่สปอว์น การ raycast และการอ่าน/ตั้งค่า vehicle properties

ก่อนเริ่ม (แนะนำ)

  • ตั้งค่าการเรียกใช้ Framework ให้ถูกต้องตาม Framework Usage

  • งานที่ต้อง “อัปเดตทุกเฟรม” (เช่น draw text) ให้รันใน CreateThread และใช้ Wait(0)

circle-info

ฟังก์ชันในหน้านี้ใช้ได้เฉพาะสคริปต์ฝั่ง Client เท่านั้น

circle-exclamation

รูปแบบพารามิเตอร์ที่พบบ่อย

  • coords: vector3 หรือ table ที่มี x, y, z

  • model: string|number (ชื่อโมเดล หรือ hash)

  • modelFilter: table แบบ {[hash] = true} เพื่อกรองโมเดล

  • maxDistance: number ระยะค้นหาสูงสุด (หน่วยเป็นเมตรในโลกเกม)

  • cb: function callback ที่ถูกเรียกเมื่อทำงานเสร็จ (เช่น spawn สำเร็จ)

  • networked: boolean ระบุว่าจะสร้างเป็น network entity หรือไม่ (ค่าเริ่มต้นมักเป็น true)


ESX.Game.GetPlayers

ดึงรายการผู้เล่นทั้งหมดใน session

สามารถเลือกให้ไม่รวมตัวเอง หรือให้คืนค่าเป็น ped handle ได้

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

onlyOtherPlayers

boolean

(ไม่บังคับ) ไม่รวมผู้เล่นตัวเอง

returnKeyValue

boolean

(ไม่บังคับ) คืนค่าแบบ {[playerId] = ped}

returnPeds

boolean

(ไม่บังคับ) คืนค่าเป็น ped handle แทน player ID

ค่าที่ส่งคืน (Returns)

Type
Description

table

รายการ player ID/ped หรือ table แบบ key-value

ตัวอย่างการใช้งาน (Example)


ESX.Game.GetPeds

ดึง ped ทั้งหมดจาก game pool

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

onlyOtherPeds

boolean

(ไม่บังคับ) ไม่รวม ped ของผู้เล่นตัวเอง

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array of ped handles


ESX.Game.GetVehicles

ดึงยานพาหนะทั้งหมดจาก game pool

รูปแบบการใช้งาน (Syntax)

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array of vehicle handles


ESX.Game.GetObjects

ดึง object ทั้งหมดจาก game pool

รูปแบบการใช้งาน (Syntax)

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array of object handles


ESX.Game.GetClosestEntity

หา entity ที่ใกล้ที่สุดจากรายการที่ส่งเข้ามา โดยอ้างอิงจากพิกัดที่ระบุ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

entities

table

รายการ entity (จาก GetPlayers, GetPeds ฯลฯ)

isPlayerEntities

boolean

ใส่ true ถ้ารายการเป็นแบบ key = player ID

coords

vector3

(ไม่บังคับ) พิกัดอ้างอิง (ค่าเริ่มต้น: พิกัดผู้เล่น)

modelFilter

table

(ไม่บังคับ) {[modelHash] = true} เพื่อกรองตามโมเดล

ค่าที่ส่งคืน (Returns)

Type
Description

number

Closest entity handle (หรือ player ID)

number

ระยะห่างไปยัง entity ที่ใกล้ที่สุด


ESX.Game.GetClosestPlayer

หาผู้เล่นที่อยู่ใกล้ที่สุดจากพิกัดที่ระบุ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดอ้างอิง (ค่าเริ่มต้น: พิกัดผู้เล่น)

ค่าที่ส่งคืน (Returns)

Type
Description

number

Closest player ID

number

Distance

ตัวอย่างการใช้งาน (Example)


ESX.Game.GetClosestPed

หา ped ที่ไม่ใช่ผู้เล่น และอยู่ใกล้ที่สุดจากพิกัดที่ระบุ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดอ้างอิง

modelFilter

table

(ไม่บังคับ) {[modelHash] = true} filter

ค่าที่ส่งคืน (Returns)

Type
Description

number

Closest ped handle

number

Distance


ESX.Game.GetClosestVehicle

หายานพาหนะที่อยู่ใกล้ที่สุดจากพิกัดที่ระบุ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดอ้างอิง

modelFilter

table

(ไม่บังคับ) {[modelHash] = true} filter

ค่าที่ส่งคืน (Returns)

Type
Description

number

Closest vehicle handle

number

Distance


ESX.Game.GetClosestObject

หา object ที่อยู่ใกล้ที่สุดจากพิกัดที่ระบุ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดอ้างอิง

modelFilter

table

(ไม่บังคับ) {[modelHash] = true} filter

ค่าที่ส่งคืน (Returns)

Type
Description

number

Closest object handle

number

Distance


ESX.Game.GetPlayersInArea

ดึงผู้เล่นทั้งหมดภายในระยะ (radius) จากพิกัดที่กำหนด

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดศูนย์กลาง (ค่าเริ่มต้น: พิกัดผู้เล่น)

maxDistance

number

ระยะค้นหาสูงสุด

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array of player IDs in range


ESX.Game.GetVehiclesInArea

ดึงยานพาหนะทั้งหมดภายในระยะ (radius) จากพิกัดที่กำหนด

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

(ไม่บังคับ) พิกัดศูนย์กลาง (ค่าเริ่มต้น: พิกัดผู้เล่น)

maxDistance

number

ระยะค้นหาสูงสุด

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array of vehicle handles in range


ESX.Game.GetVehicleInDirection

ยิง raycast ไปด้านหน้าผู้เล่น แล้วคืนค่ายานพาหนะที่ตรวจพบ

รูปแบบการใช้งาน (Syntax)

ค่าที่ส่งคืน (Returns)

Type
Description

number

Vehicle entity handle หรือ nil ถ้าไม่พบ

vector3

พิกัดของยานพาหนะ

ตัวอย่างการใช้งาน (Example)


ESX.Game.IsSpawnPointClear

ตรวจสอบว่าพิกัดสำหรับสปอว์น “โล่ง” หรือไม่ (ไม่มีรถอยู่ในระยะ)

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3

พิกัดที่ต้องการตรวจ

maxDistance

number

ระยะตรวจสอบ

ค่าที่ส่งคืน (Returns)

Type
Description

boolean

true ถ้าไม่มียานพาหนะในระยะ


ESX.Game.IsVehicleEmpty

ตรวจสอบว่ายานพาหนะ “ไม่มีคนอยู่” ทุกที่นั่ง

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

vehicle

number

Vehicle entity handle

ค่าที่ส่งคืน (Returns)

Type
Description

boolean

true ถ้ายานพาหนะว่างทุกที่นั่ง


ESX.Game.GetVehicleProperties

อ่านค่า properties ของรถแบบครบชุด (สี, mod, สภาพรถ, damage state ฯลฯ)

เหมาะสำหรับการ “บันทึก/กู้คืน” สถานะยานพาหนะ

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

vehicle

number

Vehicle entity handle

ค่าที่ส่งคืน (Returns)

Type
Description

table

ตาราง properties ของรถ (model, plate, colors, mods, health, damage states ฯลฯ)

chevron-rightรายการ field ที่ได้คืน (Returned properties)hashtag
Property
Type
Description

model

number

Vehicle model hash

plate

string

License plate text

plateIndex

number

Plate style index

bodyHealth

number

Body health

engineHealth

number

Engine health

tankHealth

number

Fuel tank health

fuelLevel

number

Fuel level

dirtLevel

number

Dirt level

color1 / color2

number

Primary / secondary color index

customPrimaryColor

table

Custom RGB primary color

customSecondaryColor

table

Custom RGB secondary color

pearlescentColor

number

Pearlescent color index

wheelColor

number

Wheel color index

wheels

number

Wheel type

windowTint

number

Window tint index

neonEnabled

table

Array of 4 booleans for neon sides

neonColor

table

RGB neon color

extras

table

Vehicle extras on/off states

tyreSmokeColor

table

RGB tyre smoke color

modSpoilers...modLivery

number

Vehicle mod indices

doorsBroken

table

Broken door states

windowsBroken

table

Broken window states

tyreBurst

table

Burst tyre states

livery

number

Vehicle livery index

ตัวอย่างการใช้งาน (Example)


ESX.Game.SetVehicleProperties

นำ properties ที่เตรียมไว้ไปใช้กับรถ เพื่อ “กู้คืนสถานะ” ของรถกลับมา

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

vehicle

number

Vehicle entity handle

props

table

Properties table (จาก GetVehicleProperties หรือเตรียมเอง)

ตัวอย่างการใช้งาน (Example)


ESX.Game.GetPedMugshot

สร้างและคืนค่า mugshot (headshot) ของ ped สำหรับใช้ใน UI

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

ped

number

Ped handle

transparent

boolean

(ไม่บังคับ) true เพื่อให้พื้นหลังโปร่งใส

ค่าที่ส่งคืน (Returns)

Type
Description

number

Mugshot handle

string

Texture dictionary string

ตัวอย่างการใช้งาน (Example)

circle-exclamation

ESX.Game.SpawnVehicle

สปอว์นยานพาหนะฝั่ง Client

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

model

string|number

ชื่อโมเดลยานพาหนะ หรือ hash

coords

vector3|table

พิกัดสำหรับสปอว์น

heading

number

มุมหัน (heading)

cb

function

(ไม่บังคับ) Callback function(vehicle)

networked

boolean

(ไม่บังคับ) สร้างเป็น network entity หรือไม่ (ค่าเริ่มต้น: true)

ตัวอย่างการใช้งาน (Example)


ESX.Game.SpawnLocalVehicle

สปอว์นยานพาหนะ “local” (ไม่เป็น network entity)

รูปแบบการใช้งาน (Syntax)

หมายเหตุ

พฤติกรรมเหมือน ESX.Game.SpawnVehicle แต่จะสร้างแบบ local เสมอ


ESX.Game.SpawnObject

สปอว์น object ฝั่ง Client

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

model

string|number

ชื่อโมเดล object หรือ hash

coords

vector3|table

พิกัดสำหรับสปอว์น

cb

function

(ไม่บังคับ) Callback function(object)

networked

boolean

(ไม่บังคับ) สร้างเป็น network entity หรือไม่ (ค่าเริ่มต้น: true)


ESX.Game.SpawnLocalObject

สปอว์น object “local” (ไม่เป็น network entity)

รูปแบบการใช้งาน (Syntax)

หมายเหตุ

พฤติกรรมเหมือน ESX.Game.SpawnObject แต่จะสร้างแบบ local เสมอ


ESX.Game.DeleteVehicle

ลบยานพาหนะอย่างปลอดภัย

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

vehicle

number

Vehicle entity handle


ESX.Game.DeleteObject

ลบ object อย่างปลอดภัย

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

object

number

Object entity handle


ESX.Game.Teleport

วาร์ป entity ไปยังพิกัดที่กำหนด

ฟังก์ชันจะรอ collision/load ให้พร้อมก่อนจบการทำงาน

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

entity

number

Entity handle ที่ต้องการวาร์ป

coords

vector3|vector4|table

พิกัดปลายทาง (และ heading ถ้ามี)

cb

function

(ไม่บังคับ) Callback เมื่อวาร์ปเสร็จ

ตัวอย่างการใช้งาน (Example)


ESX.Game.Utils.DrawText3D

วาดข้อความ 3D ที่พิกัดโลกเกม และปรับสเกลตามระยะกล้อง

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

coords

vector3|table

พิกัดที่ต้องการวาดข้อความ

text

string

ข้อความที่ต้องการแสดงผล

size

number

(ไม่บังคับ) สเกลข้อความ (default: 1)

font

number

(ไม่บังคับ) Font ID (default: 0)

ตัวอย่างการใช้งาน (Example)

circle-info

ต้องเรียกทุกเฟรม (ใช้ลูป Wait(0)) เพื่อให้ข้อความคงอยู่บนจอ


ESX.Game.Utils.EnumerateEntitiesWithinDistance

กรองรายการ entity ให้เหลือเฉพาะตัวที่อยู่ภายในระยะที่กำหนด

รูปแบบการใช้งาน (Syntax)

พารามิเตอร์ (Parameters)

Name
Type
Description

entities

table

รายการ entity ที่ต้องการกรอง

isPlayerEntities

boolean

ใส่ true ถ้ารายการเป็นแบบ key = player ID

coords

vector3

(ไม่บังคับ) พิกัดศูนย์กลาง

maxDistance

number

ระยะสูงสุด

ค่าที่ส่งคืน (Returns)

Type
Description

table

Array ของ entity handle/ID ที่อยู่ในระยะ

Last updated