I have hit a bit of a roadblock on my development. I have constructed a modular building system for my top down project where the player can place walls, floors, etc piece by piece. The buildables can be placed freely and are not confined to any grid.
The part I am struggling on is room detection. How can I set up an optimised system that can detect if a room has been created for a system like this. For example, if the player closes off a loop of walls, it should form a room. Rooms should then perform various things such as muffling weather sounds when inside the room, creating a slight fog effect on the outside, and restricting visibility of characters in the outside world by making them semi transparent.
Could anyone share logic/insight into how to achieve room detection for a system like this? I am fairly technical with blueprint; but I would appreciate a helping hand with brainstorming this.
Sorry for so many edits, my 2 previous approaches were both problematic and wouldn’t work. I tried to correct them by adding fixes but there were still scenarios in which they wouldn’t work. Anyways there’s a much more simpler apprach which would work but that unfortunately wasn’t the first thing that came to my mind.
So you can actually detect collisions in between your buildables by reading their classes or tags to make sure we don’t take unrelated things like the ground or the player or other objects into account. Now when a buildable collides with another buildable, call a function for one of them that checks if the ones collide with it also collide with more than one buildable. And if so, call that function for those ones as well. Check if their other collisions (we ignore the already checked one) collide with the ones that touch the previously checked buildable. If still not, do the same thing with their other neighbors but this time check if those neighbors of the 2 already checked buildables collide with each other. Run this loop until there’s no more buildable with more than 2 collisions with other buildables are left that are in that connection chain. If they don’t succeed at this point, that means there’s no confinement.
I can’t provide any more guide on how to implement this approach exactly, but I think this can give you some inspiration
As for your concerns on performance side, I don’t think an optimized version of this logic would lag your game or anything unless there’s a ton of objects to check. Though if it does, you can simply prevent all of it to be run in the same moment. You could easily lower the workload just by dividing the actions to be run into really short time periods if they exceed a certain limit and it would still seem almost instant