Hi, I’m working on a game which generates random premade “chunks” in a flat square around the player. Right now it does fine performance-wise, though I think it needs to improve for a variety of reasons.
How it works right now
as you can see in the gif, the chunks* are generated column by column, horizontally
heres an example of the code im using
*chunks are 16x16x16 premade segments which get cloned
--radius will be something like 25 idk, note that this is just the bare bones of the generation to demonstrate how it works
for x = -radius, radius do
for y = -radius, radius do
if not table.find(generatedchunks, Vector2.new(x, y)) then
print("FrontChunk")
generatechunk(x,y)
else
for ya = -radius, radius do
if not table.find(generatedchunks, Vector2.new(x + Xoffset, -ya + Yoffset) then
print("backwardschunk")
generatechunk(x, ya)
else
break
end
end
break
end
end
wait()
--waits 1 frame per row, its slowed using another method in the gif,
--note that im not looking to improve this code, im looking for a better alternative
end
Goals
I’m looking for a better system for mass generation. Firstly, I’d like to move to a system which generates from the player, and I would also like a system that is compatible with multi-chunk structures. I haven’t found much stuff like that on here, and dont really know where to look elsewhere.
Any help or advice is appreciated, thanks!
UPDATE:
I’ve been looking through a lot of resources and might swap to a new system based on generating a blank “chunk” and adding the walls in through code. I still don’t know how I could optimize the chunk generation itself though.
I do that wiith a table, its not shown in the code, the issue im having with stuctures like that is how I can check where it is compared to the world, and then adjust its position to generate outside of any pregenerated chunks, like if its on the right side of the current generation thread or above or below.
Thats one of the main reasons I want a system which expands outwards from the player
I already tried doing something like what you said but I cant figure out how to overwrite the chunks it wants to generate in as an alternate either
edit: also only checking if something is on the far side will cause structures to generate horizontally, as stuff in the middle will be disregarded, so I’m thinking something like saviing the chunks instance data with its position and overwriting it might work, but it could also cause performance issues with largely generated worlds
Tables have issues with performance once the generated amount of chunks reaches hundreds to thousands, as checking though allat is probbaly going to be very costly for performance
also note that im not sure if generating out from the player would restrict structure spawning on the diagonals and in between
Idea: generate chunks around the perimeter of a square/ other shape. Increase the squares/circles/shapes size by a chunk on every size and repeat for the radius I want the player to see.
Idk if this is flawed, but it will snake its way around the perimeter so it is always on the outside for structure generation. Maybe a custom function for generating structures so I can offset it to not clip into anything
When it turns a corner or is on a corner it uses that data to know what way it is facing relative to the world, and where to generate a structure if it wants to.
IT WORKS!!! This is going to make generation much easier to modify, structures and other things like that’re larger than a single chunk should now be generateable easily! I’ll put the code up in a bit incase anyone else wants to do something like this or needs ideas/references
this generation method is a lot faster, idk if it performs better tho
this is a basic summary of what this function does in my generation module
for perimeter = 1, radius do
for x = -p + 1, p - 1 do
--generatechunkhere(x, p) or --generatechunkhere(p, y)
--replace x with y for verical segments and the y for this one (x for vertical) should be p or -p, which is the current radius we are generating the perimeter of
-- I do this for every side, along with manually doing each corner, and it looks like spaghetti code, but it works for what I need
end
end