Page 1 of 1

How to use Chunks?

Posted: Tue Jan 03, 2017 7:35 pm
by kinanprincipe
Hello everyone!
please i need help again.

In my game, when i create big map (like 50x50 of 16x16 pixels) it makes the game slowwwwwwwww...

my brother said that i can use Chunks, cut the map in various squares and save it to files, then just the player location is a open chunk, and this will make the game faster because use less memory.

but i did'nt now how to code it, i tried in google but i don't get it.

Thanks.

Re: How to use Chunks?

Posted: Tue Jan 03, 2017 7:55 pm
by raidho36
You simply create a very large super-grid (but with few cells in it), and create a smaller sub-grids for each "chunk" of game area you're going to use, and put these sub-grids into super-grid. When your camera moves, it calculates which chunks are in focus and which are not. Then if the chunk is in focus but not loaded, you load it, and if it's out of focus but is loaded, you unload it.

Re: How to use Chunks?

Posted: Wed Jan 04, 2017 7:13 pm
by kinanprincipe
but how did i do this?, I have no idea.

Re: How to use Chunks?

Posted: Wed Jan 04, 2017 8:14 pm
by raidho36
Assume you have the world grid with chunks in it. Suppose you created in in map editor. Then you fetch chunks using something like this:

Code: Select all

local x1, x2 = math.floor ( ( x - width ) / chunk_size ), math.floor ( ( x + width ) / chunk_size
local y1, y2 = math.floor ( ( y - height ) / chunk_size ), math.floor ( ( y + height ) / chunk_size
local chunks = { }
for xx = x1, x2 do
  for yy = y1, y2 do
    chunks[ #chunks + 1 ] = worldmap[ xx ][ yy ]
  end
end
for i = 1, #chunks do
  if not loaded_chunks[ chunks[ i ] ] then
    loaded_chunks[ chunks[ i ] ] = load_chunk ( chunks[ i ] )
  end
end

Re: How to use Chunks?

Posted: Wed Jan 04, 2017 8:52 pm
by zorg
worldmap[xx][yy] should be a loadfile or similar... otherwise you don't get memory usage benefits.

On the other hand, speed benefits can happen if you only do logic and/or only render the visible chunks.

Do note that going through all loaded chunks and CHECKING them takes time too; it'd be faster to index the loaded chunks with their own indices, and iterate through them, and only them.

No code, just ideas, cause i'm a meanie. :emo: