I have a 2D infinite tile map game in which the player can place floors and walls to construct "buildings".

I'm trying to think of an efficient way to determine whether the player is inside a "complete" building, meaning it's fully enclosed by walls and a door.

Normally, the logic isn't difficult since it's easy to tell if the walls fully encircle an area - but in the case of an infinite map, the "building" may extend into unloaded areas of the game.

For example, if a player makes a complete rectangular building, 10x100 tiles long, only some of the chunks it covers will be loaded at once.

I have a few ideas:

  • If a player is near candidate walls, follow them and load all necessary chunks to check completeness. This would work, but could load a moderate number of unused chunks.
  • Cache a list of "complete" buildings as they're completed. So option #1 is applied only as the player builds the building. This would have to saved/loaded, and updated when integral walls/doors are removed, so it trades efficiency for a little overhead, which might be OK.
  • Using a different metric to determine "indoors". I can't think of one though.

Anyone have any other ideas? Has this ever been implemented somewhere else, something I can at least use as an example?


1 Answer 1


I'll try to extend your second option. Add a flag called "indoor" on your tiles. When you finish a building set the "indoor" true on all tiles inside the building. When the player is on a "indoor" tile he is inside a building. If you need to know in which building you are inside, make "indoor" a pointer to a building in your:

list of "complete" buildings


  • \$\begingroup\$ That's an interesting solution. It requires the same determination logic as option 2, moves the caching to the tiles themselves, has an initial higher cost to "write" the info to all tiles "inside" (since it would cache the bool in all tiles, not just coordinates of the walls), yet is also cheaper to lookup every frame which is more important. Unless I find something better, I think I'll start with this. If something doesn't work it can easily be modified into the original option #2. \$\endgroup\$
    – BotskoNet
    Jul 7, 2017 at 14:27
  • \$\begingroup\$ @BotskoNet Please accept this answer (checkmark) if it helped you. \$\endgroup\$
    – Engineer
    Feb 13, 2018 at 8:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .