Files
Bubberstation/code/datums/elements/diggable.dm
Mothblocks 60ee087b16 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.
2022-11-05 15:00:59 +01:00

52 lines
1.8 KiB
Plaintext

/// Lets you make hitting a turf with a shovel pop something out, and scrape the turf
/datum/element/diggable
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH_ON_HOST_DESTROY // Detach for turfs
id_arg_index = 2
/// Typepath of what we spawn on shovel
var/atom/to_spawn
/// Amount to spawn on shovel
var/amount
/// What should we tell the user they did? (Eg: "You dig up the turf.")
var/action_text
/// What should we tell other people what the user did? (Eg: "Guy digs up the turf.")
var/action_text_third_person
/// Percentage chance of receiving a bonus worm
var/worm_chance
/datum/element/diggable/Attach(datum/target, to_spawn, amount = 1, worm_chance = 30, action_text = "dig up", action_text_third_person = "digs up")
. = ..()
if(!isturf(target))
return ELEMENT_INCOMPATIBLE
if(!to_spawn)
stack_trace("[type] wasn't passed a typepath to spawn attaching to [target].")
return ELEMENT_INCOMPATIBLE
src.to_spawn = to_spawn
src.amount = amount
src.worm_chance = worm_chance
src.action_text = action_text
src.action_text_third_person = action_text_third_person
RegisterSignal(target, COMSIG_ATOM_TOOL_ACT(TOOL_SHOVEL), .proc/on_shovel)
/datum/element/diggable/Detach(datum/source, ...)
. = ..()
UnregisterSignal(source, COMSIG_ATOM_TOOL_ACT(TOOL_SHOVEL))
/// Signal proc for [COMSIG_ATOM_TOOL_ACT] via [TOOL_SHOVEL].
/datum/element/diggable/proc/on_shovel(turf/source, mob/user, obj/item/tool)
SIGNAL_HANDLER
for(var/i in 1 to amount)
new to_spawn(source)
if (prob(worm_chance))
new /obj/item/food/bait/worm(source)
user.visible_message(
span_notice("[user] [action_text_third_person] [source]."),
span_notice("You [action_text] [source]."),
)
playsound(source, 'sound/effects/shovel_dig.ogg', 50, TRUE)
source.ScrapeAway(flags = CHANGETURF_INHERIT_AIR)