Files
Bubberstation/code/datums/elements/chewable.dm
SkyratBot 80d124a3c6 [MIRROR] Remove ELEMENT_DETACH on everything that doesn't need it, rename to ELEMENT_DETACH_ON_HOST_DESTROY + a PSA (about 0.2s init time savings) [MDB IGNORE] (#17384)
* Remove ELEMENT_DETACH on everything that doesn't need it, rename to ELEMENT_DETACH_ON_HOST_DESTROY + a PSA (about 0.2s init time savings) (#70972)

ELEMENT_DETACH is **not** a requirement to having `Detach` called.
Detach is always called when the element itself is destroyed.

ELEMENT_DETACH is a flag that when set, makes sure Detach is called when
the atom destroys.

Sometimes you want this, for instance:

```dm
/datum/element/point_of_interest/Detach(datum/target)
	SSpoints_of_interest.on_poi_element_removed(target)
	return ..()
```

This Detach cleans up a reference that would have hung if target was
destroyed without this being called.

However, most uses of Detach are cleaning up signals. Signals are
automatically cleaned up when something is destroyed. You do not need
ELEMENT_DETACH in this case, and it slows down init. This also includes
somewhat more complex stuff, like removing overlays on the source
object. It's getting deleted anyway, you don't care!

I have removed all uses of ELEMENT_DETACH that seemed superfluous. I
have also renamed it to `ELEMENT_DETACH_ON_HOST_DESTROY` to make its
purpose more clear, as me and a lot of other maintainers misunderstood
what it did,

---

An update to this, ELEMENT_DETACH *is* needed for anything that can
register to a turf, as turfs do not clear their signals on destroy.

* Remove ELEMENT_DETACH on everything that doesn't need it, rename to ELEMENT_DETACH_ON_HOST_DESTROY + a PSA (about 0.2s init time savings)

* skyrat elements

Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: tastyfish <crazychris32@gmail.com>
2022-11-05 17:48:01 -04:00

69 lines
2.0 KiB
Plaintext

/// Anything with this element will provide the reagents inside the
/// item to the user when it is equipped.
/datum/element/chewable
element_flags = ELEMENT_DETACH_ON_HOST_DESTROY | ELEMENT_BESPOKE
id_arg_index = 2
/// The amount to metabolize per second
var/metabolization_amount = REAGENTS_METABOLISM
/// A bitfield of valid slots. If this is not provided, then it will
/// use the `slot_flags` of the item.
var/slots_to_check
/// The objects with this element that are currently being processed
var/list/processing = list()
/datum/element/chewable/Attach(datum/target, metabolization_amount, slots_to_check)
. = ..()
if (!isitem(target))
return ELEMENT_INCOMPATIBLE
var/obj/item/target_item = target
if (metabolization_amount)
src.metabolization_amount = metabolization_amount
src.slots_to_check = slots_to_check || target_item.slot_flags
RegisterSignal(target, COMSIG_ITEM_DROPPED, .proc/on_dropped)
RegisterSignal(target, COMSIG_ITEM_EQUIPPED, .proc/on_equipped)
/datum/element/chewable/Detach(datum/source, force)
. = ..()
processing -= source
UnregisterSignal(source, list(COMSIG_ITEM_DROPPED, COMSIG_ITEM_EQUIPPED))
/datum/element/chewable/process(delta_time)
if (processing.len == 0)
return PROCESS_KILL
for (var/obj/item/item as anything in processing)
var/mob/chewer = item.loc
if (!istype(chewer) || !item.reagents?.total_volume)
processing -= item
continue
handle_reagents(item, delta_time)
/datum/element/chewable/proc/handle_reagents(obj/item/item, delta_time)
var/datum/reagents/reagents = item.reagents
var/metabolism_amount = metabolization_amount * delta_time
if (!reagents.trans_to(item.loc, metabolism_amount, methods = INGEST))
reagents.remove_any(metabolism_amount)
/datum/element/chewable/proc/on_dropped(datum/source)
SIGNAL_HANDLER
processing -= source
/datum/element/chewable/proc/on_equipped(datum/source, mob/equipper, slot)
SIGNAL_HANDLER
if (slot & slots_to_check)
processing += source
START_PROCESSING(SSdcs, src)
else
processing -= source