RimWorld

RimWorld

TD Find Lib
Uuugggg  [developer] Feb 3, 2024 @ 4:03pm
Use TD Find Lib in your mod
I sure HOPE people integrate TD Find Lib into their mods. Let me throw out some quick pointers.


Tell me
that you're gonna use TDFindLib and I'll so very much help you figure it out.
(The one time I know it happened I wasn't even told until it was done)


QuerySearch
A QuerySearch is the class to use if you're searching everything on the map - though still allowing the user to narrow down to "People" or "Items" for faster searches.

Call RemakeList() then access result. It makes sure to re-evaluate only once per frame.


QueryHolder
Use QueryHolder if you just need to manually execute TDFindLib filters on your own object in code. It is the base class for QuerySearch.

Call AppliesTo(Thing) or Filter(List<Thing>), though you still do need a provide a Map so that certain filters work.

QueryHolder is only recently supported with editor windows as I don't actually use it myself. Some of the code awkwardly that assumes everything is a QuerySearch but it should be able to convert to a QueryHolder and back easily enough.


SearchEditorWindow
Open one of these to let the player edit a QuerySearch. Maybe you'd like a confirmation window, so use SearchEditorRevertableWindow. And for QueryFilter, use HolderEditorWindow. And if you're ambitious, subclass your own window, maybe from the base QueryDrawerWindow.


ThingQuery
The ThingQuery is the individual filter for a single property of a Thing. It's what the QueryHolder/QuerySearch holds. Subclass ThingQuery to make a new filter for whatever is in your mod. Follow along the expansion and mod support I already have done. Simple filters are certainly easy to add, but there's a lot of details to consider so talk to me about it.


Saving/Loading
There's extensive code to handle saving globally and loading dynamically, as certain filters may reference in-game objects, so this has to be handled delicately. Comments in code go over this deeply. And again talk to me about this.

TL;DR: Loading a saved QuerySearch requires the game Defs to be loaded, so probably just make sure to load your saved Queries in a StaticConstructorOnStartup, not in the Mod constructor.

References to in-game objects are re-bound dynamically before a search based on the Map the search is executed on. This should be handled magically - but your ThingQuery subclass will need to handle this if it uses in-game references.



Active Search
QuerySearch has a "inactive" state where it doesn't execute searches (which is what the library uses). Searches that are "active" are considered in-game and are re-executed whenever the filter is changed via the editor window. Searches are "cloned" back and forth from active to inactive so your loaded search doesn't edit the library's copy.

Okay, there's not much difference between the two, and I question the wisdom of this boolean, but that's just how it be. It was more complicated before so be happy where it is now.


IDEAS TO USE TD FIND LIB:
1. Basically anywhere there's a ThingFilter
- Storage Zones
- Appropriate Outfit
- etc
2. In the Numbers mod, to list a certain type of thing on the map and show its numbers
3. I don't have that many ideas but someone should, right?
Last edited by Uuugggg; Feb 3, 2024 @ 4:05pm