Refactor behavior and entity invocation handling

Standardizes the use of the 'invoked' property for entities and behaviors, ensuring behaviors are scoped per invoking resource. Updates behavior registration, retrieval, removal, and cleanup logic to support resource-specific behavior tables, improving modularity and preventing cross-resource conflicts.
This commit is contained in:
Mr.Crowley committed 2025-08-31 20:50:46 -05:00
1 parent b532e33fc7
commit c399fbaca4
3 files changed
+23 -18

No files matched your search

+2 -2
View File
@@ -112,10 +112,10 @@ function ClientEntity.Create(entityData)
entityData.rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) entityData.rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0)
entityData.oldCoords = entityData.oldCoords or vector3(0.0, 0.0, 0.0) entityData.oldCoords = entityData.oldCoords or vector3(0.0, 0.0, 0.0)
entityData.oldRotation = entityData.rotation entityData.oldRotation = entityData.rotation
entityData.invoked = entityData.invoked or GetInvokingResource() or "community_bridge"
local entityPoint = Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, UpdateEntity) local entityPoint = Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, UpdateEntity)
Entities[entityData.id] = entityPoint Entities[entityData.id] = entityPoint
entityData.invoked = GetInvokingResource() or "community_bridge"
ClientEntity.Invoked[entityData.invoked] = ClientEntity.Invoked[entityData.invoked] or {} ClientEntity.Invoked[entityData.invoked] = ClientEntity.Invoked[entityData.invoked] or {}
ClientEntity.Invoked[entityData.invoked][entityData.id] = entityData ClientEntity.Invoked[entityData.invoked][entityData.id] = entityData
Behaviors.Trigger("OnCreate", entityPoint) Behaviors.Trigger("OnCreate", entityPoint)
+1 -1
View File
@@ -24,7 +24,7 @@ function ServerEntity.New(id, entityType, model, coords, rotation, args)
self.coords = coords self.coords = coords
self.rotation = rotation or vector3(0.0, 0.0, 0.0) self.rotation = rotation or vector3(0.0, 0.0, 0.0)
local invoking = GetInvokingResource() or "community_bridge" local invoking = GetInvokingResource() or "community_bridge"
self.invoking = invoking self.invoked = invoking
Invoked[invoking] = Invoked[invoking] or {} Invoked[invoking] = Invoked[invoking] or {}
table.insert(Invoked[invoking], self) table.insert(Invoked[invoking], self)
+20 -15
View File
@@ -4,29 +4,31 @@ Behaviors = {
invoked = {} invoked = {}
} }
function Behaviors.Create(behaviorId, behavior) function Behaviors.Create(behaviorId, behavior)
if not behaviorId or not behavior then return end if not behaviorId or not behavior then return end
if Behaviors.All[behaviorId] then return print(string.format("[ClientEntity] Behavior %s already exists", behaviorId)) end Behaviors.All = Behaviors.All or {}
local invoking = GetInvokingResource() or "community_bridge" local invoking = GetInvokingResource() or "community_bridge"
Behaviors.All[behaviorId] = behavior Behaviors.All[invoking] = Behaviors.All[invoking] or {}
Behaviors.invoked[invoking] = Behaviors.invoked[invoking] or {} Behaviors.All[invoking][behaviorId] = behavior
table.insert(Behaviors.invoked[invoking], behaviorId)
end end
function Behaviors.Get(behaviorId) function Behaviors.Get(behaviorId)
return Behaviors.All[behaviorId] local invoking = GetInvokingResource() or "community_bridge"
return Behaviors.All[invoking] and Behaviors.All[invoking][behaviorId]
end end
function Behaviors.Remove(behaviorId) function Behaviors.Remove(behaviorId)
if not Behaviors.All[behaviorId] then return end local invoking = GetInvokingResource() or "community_bridge"
Behaviors.All[behaviorId] = nil if not Behaviors.All[invoking] or not Behaviors.All[invoking][behaviorId] then return end
Behaviors.All[invoking][behaviorId] = nil
return true return true
end end
function Behaviors.Trigger(actionName, clientEntityData, ...) function Behaviors.Trigger(actionName, clientEntityData, ...)
if not clientEntityData or not actionName then return end if not clientEntityData or not actionName then return end
for property, behavior in pairs(Behaviors.All) do
local invoking = clientEntityData.invoked or GetInvokingResource() or "community_bridge"
for property, behavior in pairs(Behaviors.All[invoking] or {}) do
local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property
if hasBehaviorArgs and behavior[actionName] then if hasBehaviorArgs and behavior[actionName] then
local success, result = pcall(behavior[actionName], clientEntityData, hasBehaviorArgs, ...) local success, result = pcall(behavior[actionName], clientEntityData, hasBehaviorArgs, ...)
@@ -38,7 +40,8 @@ function Behaviors.Trigger(actionName, clientEntityData, ...)
end end
function Behaviors.Inherit(behaviorId, clientEntityData, defaultData) function Behaviors.Inherit(behaviorId, clientEntityData, defaultData)
if not Behaviors.All[behaviorId] then local invoking = GetInvokingResource() or "community_bridge"
if not Behaviors.All[invoking] or not Behaviors.All[invoking][behaviorId] then
print(string.format("[ClientEntity] Behavior %s does not exist", behaviorId)) print(string.format("[ClientEntity] Behavior %s does not exist", behaviorId))
return false return false
end end
@@ -55,18 +58,20 @@ function Behaviors.Has(behaviorId, clientEntityData)
end end
function Behaviors.Cleanup(resourceName) function Behaviors.Cleanup(resourceName)
if not resourceName or not Behaviors.invoked[resourceName] then return end if not resourceName or not Behaviors.All[resourceName] then return end
for _, behaviorId in ipairs(Behaviors.invoked[resourceName]) do for _, behaviorId in ipairs(Behaviors.All[resourceName]) do
Behaviors.Remove(behaviorId) Behaviors.Remove(behaviorId)
end end
Behaviors.invoked[resourceName] = nil Behaviors.All[resourceName] = nil
end end
AddEventHandler("onResourceStop", function(resourceName) AddEventHandler("onResourceStop", function(resourceName)
Behaviors.Cleanup(resourceName) Behaviors.Cleanup(resourceName)
if resourceName ~= GetCurrentResourceName() then return end if resourceName ~= GetCurrentResourceName() then return end
for behaviorId, _ in pairs(Behaviors.All) do for invoking, behaviors in pairs(Behaviors.All) do
Behaviors.Remove(behaviorId) for behaviorId, _ in pairs(behaviors) do
Behaviors.Remove(behaviorId)
end
end end
end) end)