All courses / Data Structures and Algorithms for Game Devs / Bits and bytes
How much memory a map needs
Challenge lesson, about 11 min, +20 XP
The dungeon under the tower is a grid of tiles. Each tile is floor, wall, door, water or chest, and the game stores it as a number, its tile id. Making the grid is one line: new int[width * height]. The question to ask before that line is how much memory the grid takes. For a small room, the answer is almost nothing. For a big world, it can be more memory than the player's computer has to spare.
Every tile takes the same number of bytes, so the whole grid is:
Read the rest of this lesson and run the code in your browser.
Previous: Spot the bug: the gold total Next: Quiz: bits and bytes