You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -104,6 +102,63 @@ registry.remember = function (id, target)
104
102
end
105
103
end
106
104
105
+
-- Handle unexpected loss of an entity
106
+
-- This will be triggered by on_object_destroyed, notifying any selectors that may depend on an entity. If a selector no longer depends on the entity, it simply ignores it.
107
+
-- This is also called when "victim" is modified in some way that may render a selector invalid for some reason.
108
+
-- Handling one of these events will fall into one of these four categories, in order of increasing severity:
109
+
-- - No action required (e.g. victim was a stale reference to an old target)
110
+
-- - The targeted inventory should be re-evaluated (e.g. the target is a proxy container whose own target may have changed)
111
+
-- - The target itself may have changed (e.g. either the mourner or its target have moved)
112
+
-- - The selector should be destroyed (e.g. the mourner itself is no longer valid)
113
+
-- Any of these should behave in an idempotent manner, allowing for straightforward handling of events immediately as they come in,
114
+
-- though possibly with some redundant work.
115
+
--
116
+
-- A smarter implementation might batch these actions together, applying only the most severe resolution necessary (as each also
117
+
-- ensures that any "lesser" issues would be resolved if present). The tricky part of this approach would be ensuring that batched
118
+
-- actions were still performed on same tick, as there is no guarantee of event order relative to `on_tick`.
119
+
---@parammournerid # Selector's id
120
+
---@paramvictimid # Generally this should be a valid unit_number
121
+
localfunctionmourn(mourner, victim)
122
+
localdata=storage.selectors[mourner]
123
+
124
+
-- If the mourner is already gone, then there's nothing to do.
125
+
ifnotdatathenreturnend
126
+
127
+
-- Regardless of the victim's relationship to the mourner, validate its entity references.
128
+
129
+
-- If the mourner's entity is no longer valid, it should be cleaned up.
130
+
ifnotdata.entityornotdata.entity.validthenreturndata:destroy() end
131
+
-- If the mourner's target is no longer valid, it should attempt to find a new one.
132
+
ifdata.targetandnotdata.target.validthenreturndata:update_proxy_target() end
@@ -284,7 +339,7 @@ function selector:update_proxy_inventory()
284
339
index=chained_index
285
340
286
341
-- Track the secondary target as well
287
-
registry.remember(self.id, chained)
342
+
remember(self.id, chained)
288
343
self.chained_id=chained.unit_number--[[@as id]]
289
344
end
290
345
@@ -327,7 +382,7 @@ function selector:update_proxy_target(target)
327
382
localentity=self.entity
328
383
localmode=self.mode
329
384
330
-
registry.remember(id, entity)
385
+
remember(id, entity)
331
386
332
387
target=targetorself.target
333
388
@@ -384,7 +439,7 @@ function selector:update_proxy_target(target)
384
439
385
440
self.target=target
386
441
self.target_id=targetandtarget.unit_number
387
-
iftargetthenregistry.remember(id, target) end
442
+
iftargetthenremember(id, target) end
388
443
389
444
returnself:update_proxy_inventory()
390
445
end
@@ -729,28 +784,8 @@ local function from_tag(entity, tag)
729
784
end
730
785
end
731
786
732
-
-- Handle unexpected loss of an entity
733
-
-- This will be triggered by on_object_destroyed, notifying any selectors that may depend on an entity. If a selector no longer depends on the entity, it simply ignores it.
0 commit comments