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.
How to use Chunks?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 4
- Joined: Mon Jan 02, 2017 8:18 pm
How to use Chunks?
- Attachments
-
dev.love
- (296.8 KiB) Downloaded 99 times
Re: How to use Chunks?
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.
-
- Prole
- Posts: 4
- Joined: Mon Jan 02, 2017 8:18 pm
Re: How to use Chunks?
but how did i do this?, I have no idea.
Re: How to use Chunks?
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
- zorg
- Party member
- Posts: 3473
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: How to use Chunks?
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.
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.

Me and my stuff
True 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.

Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Swordodo and 5 guests