Hi
I use chunks for my game to generate the map, I have a function that generates a map to the dictionary.
local function generateMap()
local biomeMap = {}
for x = 1, X do
biomeMap[x] = {} -- new row
for z = 1, Z do
biomeMap[x][z] = math.noise(SEED, x/biomeSmoothness, z/biomeSmoothness) / 1.1 + 0.5
end
end
return biomeMap
end
(Ignore the math.noise(SEED, x/…)
The problem that I now have is how would I locate on which chunk the player currently is.
And then to make a function that would give me positions of the chunks in of the player to then use a function to create that chunk.
local function createChunk(pos)
local chunk = Instance.new("Part")
chunk.Size = chunkSize
chunk.Position = Vector3.new(pos.X * chunkSize.X, pos.Y * chunkSize.Y, pos.Z * chunkSize.Z)
chunk.Anchored = true
chunk.Parent = workspace
chunk.Name = "chunk"
return chunk
end
And these are the variable I use for the map:
local X = 50
local Z = 50
local chunkSize = Vector3.new(5, 1, 5)
You take the player’s position and then divide it by the chunk size. Depending on how the chunk is laid out on the cartesian coordinate, you may need to apply an offset equal to half of the chunk size.
Thanks for replying and giving an example file. Sorry for replying so late but I though I found a solution myself which turned out to be very bad. I think I’ve done the generation process incorrectly since I do:
local biomeMap = {}
for x = 1, X do
biomeMap[x] = {} -- new row
for z = 1, Z do
local biome = getBiome(math.noise(SEED, x/biomeSmoothness, z/biomeSmoothness) / 1.1 + 0.5)
biomeMap[x][z] = {getRandomChunk(biome), getRandomChunkAngle()}
end
end
return biomeMap
to generate the map dictionary, and that doesn’t go from the centre so then when I create a chunk at the position of X/2, 0, Z/2 so that it’s in the middle and then I use :GetTouchingParts to check if player is touching a hitbox of a chunk and then I get a position of the parent of that hitbox to get which chunk I’m currently on. So the chunk world position and chunk “position” in the dictionary is very different.
I looked at your example file which worked perfectly but the way it’s wrote it is really hard to understand for me probably because im a noob.
Especially this part:
self, rawset/get, short variables and putting “:” after variables really confuses me. It would be nice if you could simplify it or add comments, thanks!
The snippet you had trouble understanding is just for creating a table for organizing the chunks.
Those colons you see are for Luau typechecking which makes the code more interpretable because it tells you exactly what kinds of values are expected (you will have to first learn typechecking to actually understand it )
The metatable is used to automate the process of creating new sub-tables when needed, it can substituted with the following code as an example:
local worldT = {}
local function addChunk(x, z, ch)
if worldT[x] then
worldT[x][z] = ch
else
worldT[x] = {[z] = ch}
end
end
--and to add a chunk, you call the function like this:
addChunk(xPosition, zPosition, chunkPart)
They both accomplish the same goal, however, I’m sticking with metatables because it’s neater (no need for function calls) and apparently more performant based on my testing for this specific task
Anyways, here’s the updated demo with comments detailing the process chunk thing.rbxl (38.9 KB) small mistake: world with 8 chunks in each direction will actually give you a 17x17 chunk world because chunks on axis don’t count