Files
Bubberstation/code/datums/elements/connect_loc.dm
Ghom 6f862d107f Adds a unit test to stop elements from using identical lists for their arguments. (#76322)
## About The Pull Request
Ok, so a few days ago I made an issue report about multiple instances of
identical elements being generated because of uncached lists.
ninjanomnom (the mind being the element datums) cleared it up and said
an implementation of GetIdFromArguments() that also checks the list
contents wouldn't be worth the performance cost, while adding that a
unit test should be written to check that it doesn't happen at least
during init, which should catch a good chunk of cases.

Also, i'm stopping RemoveElement() from initializing new elements
whenever a cached element is not found. Ideally, there should be a focus
only unit test for that too, but that's something we should tackle on a
different PR.

Some of the code comments may be a tad inaccurate, as much as I'd like
to blame drowsiness for it. Regardless, the unit test takes less than
0.2 seconds to complete on my potato so it's fairly lite.

## Why It's Good For The Game
This will close #76279.

## Changelog
No player-facing change to be logged.
2023-07-27 16:04:16 -07:00

44 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|ELEMENT_NO_LIST_UNIT_TEST
argument_hash_start_idx = 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_REF(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
listener.UnregisterSignal(old_loc, connections)
/datum/element/connect_loc/proc/on_moved(atom/movable/listener, atom/old_loc)
SIGNAL_HANDLER
unregister_signals(listener, old_loc)
update_signals(listener)