FiveM एंटिटी मैनेजमेंट: पेड्स, वाहन, ऑब्जेक्ट संभालें
Master FiveM एंटिटी मैनेजमेंट। पेड्स, वाहनों और ऑब्जेक्ट्स के लिए स्पॉनिंग, ओनरशिप, कलिंग और क्लीनअप ताकि आपका सर्वर स्थिर और तेज़ रहे।
Agency Scripts
Agency Scripts के संस्थापक और प्रमुख डेवलपर
FiveM में एंटिटीज़ को समझना
FiveM में, एक इकाई कोई भी वस्तु होती है जो गेम दुनिया में मौजूद होती है: वाहन, पेड, प्रॉप्स, पिकअप्स, और यहां तक कि खिलाड़ी पात्र। हर इकाई का एक हैंडल (एक पूर्णांक ID) होता है जिसका उपयोग आप नेटिव फंक्शंस के माध्यम से इंटरैक्ट करने के लिए करते हैं। इकाई प्रबंधन FiveM विकास में सबसे मौलिक कौशलों में से एक है क्योंकि लगभग हर स्क्रिप्ट किसी न किसी रूप में इकाइयों के साथ इंटरैक्ट करती है। खराब इकाई प्रबंधन मेमोरी लीक, भूत वस्तुओं जो संसाधन पुनःप्रारंभ के बाद बनी रहती हैं, खिलाड़ियों के बीच असमंजस, और अंततः सर्वर अस्थिरता का कारण बनता है। यह गाइड इकाइयों के निर्माण, ट्रैकिंग, और सफाई के सही पैटर्न को कवर करता है।
ऑब्जेक्ट्स और प्रॉप्स को स्पॉन करना
प्रॉप्स को स्पॉन करने के लिए मुख्य नेटिव है CreateObject (या CreateObjectNoOffset ). किसी भी ऑब्जेक्ट को स्पॉन करने से पहले, आपको मॉडल का अनुरोध करना होगा और इसे लोड होने तक इंतजार करना होगा। मॉडल का अनुरोध न करने पर अदृश्य या गायब इकाई होगी। ऑब्जेक्ट बनाने के बाद हमेशा मॉडल को रिलीज़ करें ताकि मेमोरी मुक्त हो सके।
-- client/entity_spawner.lua
local function SpawnProp(modelName, coords, rotation, isNetwork)
local model = joaat(modelName)
-- Request the model and wait for it to load
RequestModel(model)
local timeout = 0
while not HasModelLoaded(model) do
Wait(10)
timeout = timeout + 10
if timeout > 5000 then
print(('[ERROR] Model %s failed to load after 5s'):format(modelName))
return nil
end
end
local obj = CreateObject(model, coords.x, coords.y, coords.z, isNetwork, true, false)
if rotation then
SetEntityRotation(obj, rotation.x, rotation.y, rotation.z, 2, true)
end
-- Freeze the object so it does not fall through the ground
FreezeEntityPosition(obj, true)
-- Release the model from memory
SetModelAsNoLongerNeeded(model)
return obj
end
-- Usage
local chair = SpawnProp('prop_chair_01a', vector3(200.0, -800.0, 31.0), nil, false)
वाहनों को स्पॉन करना
वाहन स्पॉनिंग एक समान पैटर्न का पालन करता है लेकिन उपयोग करता है CreateVehicle और नेटवर्क स्वामित्व, ईंधन स्तर, और प्लेट टेक्स्ट के लिए अतिरिक्त विचार हैं। हमेशा संभव हो तो सर्वर साइड पर वाहन स्पॉन करें, या क्लाइंट से वाहन निर्माण अनुरोधों को मान्य करने के लिए सर्वर-साइड कॉलबैक का उपयोग करें।
-- client/vehicle_spawner.lua
local function SpawnVehicle(modelName, coords, heading)
local model = joaat(modelName)
RequestModel(model)
while not HasModelLoaded(model) do
Wait(10)
end
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
-- Basic vehicle setup
SetVehicleOnGroundProperly(vehicle)
SetEntityAsMissionEntity(vehicle, true, true)
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetVehicleNeedsToBeHotwired(vehicle, false)
SetVehRadioStation(vehicle, 'OFF')
-- Set fuel if using a fuel system
Entity(vehicle).state:set('fuel', 100.0, true)
SetModelAsNoLongerNeeded(model)
return vehicle
end
Entity Tracking and Cleanup
इकाई प्रबंधन का सबसे महत्वपूर्ण पहलू है कि आपकी स्क्रिप्ट द्वारा बनाई गई हर इकाई को ट्रैक करना और जब संसाधन बंद हो जाए तो उन्हें साफ़ करना। उचित सफाई के बिना, इकाइयाँ गेम दुनिया में अनाथ के रूप में बनी रहती हैं जो मेमोरी बर्बाद करती हैं और संसाधनों को रेंडर करती हैं। सभी इकाई हैंडल को स्टोर करने के लिए ट्रैकिंग टेबल का उपयोग करें और एक onResourceStop handler जो उन्हें हटाता है।
-- client/entity_manager.lua
local ManagedEntities = {}
function RegisterEntity(entity, category)
if not DoesEntityExist(entity) then return end
ManagedEntities[entity] = {
category = category or 'default',
created = GetGameTimer(),
model = GetEntityModel(entity),
}
end
function UnregisterEntity(entity)
if ManagedEntities[entity] then
if DoesEntityExist(entity) then
SetEntityAsMissionEntity(entity, false, true)
DeleteEntity(entity)
end
ManagedEntities[entity] = nil
end
end
function CleanupCategory(category)
for entity, data in pairs(ManagedEntities) do
if data.category == category then
UnregisterEntity(entity)
end
end
end
function CleanupAllEntities()
for entity, _ in pairs(ManagedEntities) do
UnregisterEntity(entity)
end
ManagedEntities = {}
end
-- Critical: Clean up when resource stops
AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end
CleanupAllEntities()
end)
नेटवर्क एंटिटी स्वामित्व
FiveM के नेटवर्केड वातावरण में, हर इकाई का एक मालिक होता है, वह क्लाइंट जो उसकी भौतिकी और स्थिति का अनुकरण करता है। नेटवर्क स्वामित्व निर्धारित करता है कि कौन सा खिलाड़ी क्लाइंट इकाई की गति और टक्कर को नियंत्रित करता है। नेटवर्क स्वामित्व को समझना और प्रबंधित करना समन्वित इकाई व्यवहार के लिए आवश्यक है, विशेष रूप से उन स्क्रिप्ट्स के लिए जिन्हें NPCs या वाहनों को दूर से नियंत्रित करना होता है।
-- server/ownership.lua
-- Request control of a networked entity
local function RequestEntityOwnership(src, netId)
local entity = NetworkGetEntityFromNetworkId(netId)
if not DoesEntityExist(entity) then return false end
-- Set the requesting player as the owner
SetEntityRoutingBucket(entity, GetPlayerRoutingBucket(src))
return true
end
-- client/ownership.lua
-- Request network control of an entity
local function TakeEntityControl(entity, timeout)
timeout = timeout or 2000
local start = GetGameTimer()
NetworkRequestControlOfEntity(entity)
while not NetworkHasControlOfEntity(entity) do
Wait(100)
NetworkRequestControlOfEntity(entity)
if GetGameTimer() - start > timeout then
return false
end
end
return true
end
-- Usage: Move an entity you need control of
local function MoveEntity(entity, targetCoords)
if TakeEntityControl(entity) then
SetEntityCoords(entity, targetCoords.x, targetCoords.y, targetCoords.z, false, false, false, false)
return true
end
print('[WARN] Could not get control of entity')
return false
end
पेड्स को स्पॉन करना और प्रबंधित करना
Ped (पैदल यात्री) इकाइयाँ NPCs, दुकान विक्रेता, क्वेस्ट देने वाले, और परिवेश पात्रों के लिए उपयोग की जाती हैं। इन्हें विशेष संभाल की आवश्यकता होती है क्योंकि इनमें AI होता है जो उन्हें भटकने, पर्यावरण पर प्रतिक्रिया देने, या खिलाड़ियों पर हमला करने का कारण बन सकता है यदि सही तरीके से कॉन्फ़िगर न किया गया हो। स्थिर NPCs के लिए हमेशा AI को फ्रीज और डिसेबल करें।
-- client/ped_spawner.lua
local function SpawnStaticPed(modelName, coords, heading, scenario)
local model = joaat(modelName)
RequestModel(model)
while not HasModelLoaded(model) do
Wait(10)
end
local ped = CreatePed(0, model, coords.x, coords.y, coords.z - 1.0, heading, false, true)
-- Make the ped static and non-interactive with ambient AI
SetEntityAsMissionEntity(ped, true, true)
SetBlockingOfNonTemporaryEvents(ped, true)
SetPedFleeAttributes(ped, 0, false)
SetPedCombatAttributes(ped, 17, true)
SetPedDiesWhenInjured(ped, false)
SetEntityInvincible(ped, true)
FreezeEntityPosition(ped, true)
-- Play a scenario animation if specified
if scenario then
TaskStartScenarioInPlace(ped, scenario, 0, true)
end
SetModelAsNoLongerNeeded(model)
RegisterEntity(ped, 'npc')
return ped
end
-- Spawn a shop vendor
local vendor = SpawnStaticPed(
's_m_y_ammucity_01',
vector3(22.0, -1105.0, 29.8),
160.0,
'WORLD_HUMAN_STAND_IMPATIENT'
)
एंटिटी प्रबंधन के लिए प्रदर्शन सुझाव
- सक्रिय इकाइयों को सीमित करें। प्रत्येक एंटिटी मेमोरी और रेंडर चक्रों का उपभोग करती है। स्ट्रीमिंग सिस्टम का उपयोग करें ताकि खिलाड़ी के पास होने पर एंटिटीज़ लोड हों और क्षेत्र छोड़ने पर अनलोड हो जाएं।
- ऑब्जेक्ट पूल का उपयोग करें। बार-बार उत्पन्न और नष्ट होने वाली इकाइयों जैसे बुलेट केसिंग या कण प्रभावों के लिए, नए हैंडल बनाने और हटाने के बजाय इकाई हैंडल पुन: उपयोग करें।
- क्लाइंट-साइड संस्थाओं को प्राथमिकता दें। यदि किसी इकाई को केवल एक खिलाड़ी को दिखाना आवश्यक है (जैसे UI प्रॉप या पूर्वावलोकन वस्तु), तो इसे गैर-नेटवर्केड के रूप में बनाएं ताकि सर्वर लोड कम हो।
- हमेशा SetModelAsNoLongerNeeded कॉल करें। लोड किए गए मॉडल तब तक मेमोरी में रहते हैं जब तक उन्हें स्पष्ट रूप से रिलीज़ न किया जाए। इस कॉल को भूल जाना FiveM स्क्रिप्ट्स में मेमोरी लीक के सबसे सामान्य कारणों में से एक है।
- ऑपरेशंस से पहले DoesEntityExist जांचें। Entities can be deleted by the game engine, other scripts, or desync. Always verify an entity still exists before trying to modify it.
- मेटाडेटा के लिए एंटिटी स्टेट बैग्स का उपयोग करें। एंटिटी डेटा के लिए अलग लुकअप टेबल बनाए रखने के बजाय, उपयोग करें
Entity(entity).stateइकाई पर सीधे कस्टम गुण संग्रहीत करने के लिए।