How do I delete tiles that are not in the viewport?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BlackBladedAxe

I have created a procedurally generated map that essentially resembles infinite space. There is a boundary around the player and any unfilled cells get filled with tiles, such that when the player moves, this boundary space is checked and filled with tiles.

Naturally, this isn’t optimal. I would like tiles to be deleted when they are not in view, so there isn’t a constant memory overload.

I thought I could do this check at each process call, or I could create a custom Tile scene that is scripted to constantly check if its in the set boundary and, if it is not, delete itself.

It would be great to learn the best practice for this! I have also thought of using a Parallax background - will this give me the illusion of infinite space too?

:bust_in_silhouette: Reply From: Juxxec

The TileMap node should already be optimized for the behaviour you are trying to achieve.

Anyway, the same way you add tiles to the tilemap, you can remove tiles:

# Clears a cell
$TileMap.set_cellv(position, TileMap.INVALID_CELL)
     
     

Awesome, thank you. So, as long as the tiles are not in the tilemap, the Godot engine will simply retain skeletal data for recreating that any non-visible tiles once they come into view again? That’s how I would understand it as an optimisation.

BlackBladedAxe | 2022-12-02 23:15