Refactor client entity management functions

Added freeze position handling in UpdateEntity and ensured OnDestroy behavior is triggered on entity destruction. Removed unused and commented-out Update function, and improved code clarity in Set and Destroy methods.
This commit is contained in:
Mr.Crowley committed 2025-09-03 01:04:04 -05:00
1 parent 8346130b00
commit dd08f058c3
1 file changed
+11 -59
+11 -59
View File
@@ -107,7 +107,10 @@ local function UpdateEntity(_entityData)
end
entityData.oldRotation = entityData.rotation
end
if entityData.freeze ~= nil and entityData.spawned and IsEntityPositionFrozen(entityData.spawned) then
FreezeEntityPosition(entityData.spawned, entityData.freeze)
end
end
--- Registers an entity received from the server and sets up proximity spawning.
@@ -150,9 +153,9 @@ ClientEntity.RegisterBulk = ClientEntity.CreateBulk
function ClientEntity.Destroy(id)
local entityData = Entities[id]
if not entityData then return end
Behaviors.Trigger("OnDestroy", entityData)
Point.Remove(id)
RemoveEntity(entityData)
RemoveEntity(entityData)
Entities[id] = nil
end
ClientEntity.Unregister = ClientEntity.Destroy
@@ -161,68 +164,17 @@ function ClientEntity.RegisterBehavior(id, behavior)
Behaviors.Create(id, behavior)
end
--- Sets a key-value pair for a client entity.
---@param id any
---@param key any
---@param value any
---@return nil
function ClientEntity.Set(id, key, value)
local entityData = ClientEntity.Get(id)
if not entityData then return print(string.format("[ClientEntity] SetKey: Entity %s does not exist", id)) end
entityData[key] = value
-- TriggerServerEvent("community_bridge:server:UpdateEntity", id, {[key] = value})
end
--- Updates the data for a registered entity.
-- @param id string|number The ID of the entity to update.
-- @param data table The data fields to update.
-- function ClientEntity.Update(id, data)
-- local entityData = ClientEntity.Get(id)
-- if not entityData then return end
-- local needsPointUpdate = false
-- for key, value in pairs(data) do
-- if key == 'coords' and #(entityData.coords - value) > 0.1 then
-- needsPointUpdate = true
-- end
-- if key == 'spawnDistance' and entityData.spawnDistance ~= value then
-- needsPointUpdate = true
-- end
-- entityData[key] = value
-- end
-- -- If entity is currently spawned, apply updates
-- if entityData.spawned and DoesEntityExist(entityData.spawned) then
-- if data.coords then
-- SetEntityCoords(entityData.spawned, entityData.coords.x, entityData.coords.y, entityData.coords.z, false, false, false, true)
-- end
-- if data.rotation then
-- if entityData.entityType == 'object' then
-- SetEntityRotation(entityData.spawned, entityData.rotation.x, entityData.rotation.y, entityData.rotation.z, 2, true)
-- else -- Ped/Vehicle heading
-- SetEntityHeading(entityData.spawned, type(entityData.rotation) == 'number' and entityData.rotation or entityData.rotation.z)
-- end
-- end
-- if data.freeze ~= nil then
-- FreezeEntityPosition(entityData.spawned, data.freeze)
-- end
-- -- Add other updatable properties as needed
-- end
-- -- Update Point registration if coords or distance changed
-- if needsPointUpdate then
-- Point.Remove(id)
-- Point.Register(
-- entityData.id,
-- entityData.coords,
-- entityData.spawnDistance or 50.0,
-- entityData,
-- SpawnEntity,
-- RemoveEntity,
-- OnUpdateEntity
-- )
-- end
-- if entityData.OnUpdate and type(entityData.OnUpdate) == 'function' then
-- entityData.OnUpdate(entityData, data)
-- end
-- end
function ClientEntity.Get(id)
return Entities[id]
end