How to use Chunks?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kinanprincipe
Prole
Posts: 4
Joined: Mon Jan 02, 2017 8:18 pm

How to use Chunks?

Post 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.
Attachments
dev.love
(296.8 KiB) Downloaded 99 times
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to use Chunks?

Post 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.
kinanprincipe
Prole
Posts: 4
Joined: Mon Jan 02, 2017 8:18 pm

Re: How to use Chunks?

Post by kinanprincipe »

but how did i do this?, I have no idea.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to use Chunks?

Post 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
User avatar
zorg
Party member
Posts: 3473
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to use Chunks?

Post 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:
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 7 guests