Room Awareness

Started by Liegeman, March 24, 2015, 08:54:27 AM

Previous topic - Next topic

Liegeman

Can I make a Thing that is aware of the Things in the same room? Say, a workbench that is more efficient given another workbench. I know that heaters and coolers, for instance, are aware of rooms. (but perhaps not aware of what is in them?)

Simple adjacency is doable of course, but checking within the same room is far more organic and elegant.

Wastelander

I assume you're including an assembly in your mod, but this can be done by calling the GridsUtility.GetRoom method. It takes either an IntVec3 or a Thing (you can pass in your workbench as the thing).

Make sure to guard against a null returned here as I don't think every space is in a room. Once you get the Room object back, you can call AllContainedThings to get a list of every Thing in the room, and iterate over it to check if you've got more of whatever you're looking for. An optimization I would suggest is to not check for things if the Room.IsHuge is true, as for example a workbench placed outside might check against hundreds of objects and that would diminish performance. Also make sure not to check this every tick as that would bog the game down, only check every ten seconds or whatever you determine is appropriate.

Liegeman

This is exactly what I needed, thank you.

And yes, the interval to check this needs to be quite long. I was thinking that the upgrade buildings might use the room as well to ping the original structure and let it know it has an upgrade. Should be some fun coding!