Files
Bubberstation/code/datums/elements/connect_loc.dm
SkyratBot 169c42a262 [MIRROR] Refactors connect_loc_behalf into a component (#7613)
* Refactors connect_loc_behalf into a component (#60678)

See title. Also refactors caltrops into a component because they use connect_loc_behalf which requires them to hold the state.

This also fixes COMPONENT_DUPE_SELECTIVE from just outright not working.

connect_loc_behalf doesn't make sense as an element because it tries to hold states. There is also no way to maintain current behaviour and not have the states that it needs.
Due to the fact that it tries to hold states, it means the code itself is a lot more buggy because it's a lot harder to successfully manage these states without runtimes or bugs. 

On metastation, there is only 2519 connect_loc_behalf components at roundstart. MrStonedOne has told me that datums take up this much space:
image

If we do the (oversimplified) math, there are only ever 5 variables that'll likely be changed on most connect_loc_behalf components at runtime:
connections,
tracked,
signal_atom,
parent,
signal_procs

This means that on metastation at roundstart, we take up this amount: (24 + 16 * 5) * 2519 = 261.97600 kilobytes
This is not really significant and the benefits of moving this to a component greatly outweighs the memory cost.

(Basically the memory cost is outweighed by the maint cost of tracking down issues with the thing. It's too buggy to be viable longterm basically)

* Update glass.dm

Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com>
Co-authored-by: Gandalf <jzo123@hotmail.com>
2021-08-17 20:29:11 +01:00

45 lines
1.5 KiB
Plaintext

/// This element hooks a signal onto the loc the current object is on.
/// When the object moves, it will unhook the signal and rehook it to the new object.
/datum/element/connect_loc
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
/// An assoc list of signal -> procpath to register to the loc this object is on.
var/list/connections
/datum/element/connect_loc/Attach(atom/movable/listener, list/connections)
. = ..()
if (!istype(listener))
return ELEMENT_INCOMPATIBLE
src.connections = connections
RegisterSignal(listener, COMSIG_MOVABLE_MOVED, .proc/on_moved, override = TRUE)
update_signals(listener)
/datum/element/connect_loc/Detach(atom/movable/listener)
. = ..()
unregister_signals(listener, listener.loc)
UnregisterSignal(listener, COMSIG_MOVABLE_MOVED)
/datum/element/connect_loc/proc/update_signals(atom/movable/listener)
var/atom/listener_loc = listener.loc
if(isnull(listener_loc))
return
for (var/signal in connections)
//override=TRUE because more than one connect_loc element instance tracked object can be on the same loc
listener.RegisterSignal(listener_loc, signal, connections[signal], override=TRUE)
/datum/element/connect_loc/proc/unregister_signals(datum/listener, atom/old_loc)
if(isnull(old_loc))
return
for (var/signal in connections)
listener.UnregisterSignal(old_loc, signal)
/datum/element/connect_loc/proc/on_moved(atom/movable/listener, atom/old_loc)
SIGNAL_HANDLER
unregister_signals(listener, old_loc)
update_signals(listener)