# Need help creating some sort of chunk/tile get feature.

Hi there,

I have been working on a save plugin for my game and I come to the part that i can load and save, now my levels will be very large open world maps, so i need to efficiently handle that.
Now my idea was to save objects in chunks, so every object in a 1km x 1km tile will get saved under the same chunk, this so performance stays good and to prevent loading times.
Now my idea was when saving to get the position of a object(i know how to) and then get the tile it should reside in.(not sure how to do this)
So when i have a house at location x1105 and y3800 then i want it to give me the values x2 and y4.(Means the house is in the second tile and then the 4th tile)
and if y would be a negative value like -30 i want it to output -1.
I did made an attempt in blueprints but it is unreliable as it sometimes gives wrong values for some reason:
(cant upload picture right now, keeps failing.)

So can anyone help me implementing this in c++(or blueprints and i will convert to c++)?
I just need how to calculate it, the rest i will do.
I hope you can understand what i mean

thanks

If you are using 1000m x 1000m chunks, then you can just divide the local objects position by those values to get the Chunk:

``````

const FVector ObjectPosition = Object->GetActorLocation();

int ChunkX = ObjectPosition.X * 0.001f; // Same as a / 1000.0f;
int ChunkY = ObjectPosition.Y * 0.001f;

// Add +1 ChunkX/Y if you want to use 1 based indices, versus 0 based.
// Check either chunk if they are less than 0 and set them to -1 or what not.

``````

That code works as you spawn all your objects in the +X/+Y direction. If you want to spawn in either -X/+X/-Y/+Y you need to do a bit more math and offset things around by your origin point - but again, it’s not crazy.

Thanks for your reply, but i need it to work in the - too, and it may never return a 0 as a chunk.
the problem with your code is is that it contains decimals wich it may not have.

I first thought the way you did it, and then do floor/ceiling for the values.
But that proved unreliablel for some reason.

They key is casting to an INT (which cannot have decimal values). If you really copied my code, there’s no way you’d have decimal values as ChunkX/Y are INTs.

If you want it to work in all directions, you just need to offset by value you want to be at index (0, 0). So, if the top left of my world was -8096.0f, 8096.0f, then I’d subtract that offset from all locations before diving by 1000 and finally casting to an INT (which will floor the value, you can use FMath::FloorToInt if you want to be extra sure).

Draw it out if you need to, tends to help with these things.

Thanks, but @ExtraLifeMatt if i would divide e.g. a position of 500 by 1000 it will result in 0, but there are no chunks on 0, it is the center.
so if a position would be 500, the result should be 1, if it is 1500 it should be 2, if it is -1500 it should be -2.
How would i achieve that?
Maybe by using ceiling()?(not sure if that is what it is called)
But then if the value would under 0? then i need to use floor?

``````

using ChunkXY = std::pair< int, int >;

ChunkXY GetChunkXYFromLocation(int pos_x, int pos_y)
{
ChunkXY result;
result.first = ( pos_x < 0 ? ( pos_x >= -ChunkSize ? -1 : pos_x / ChunkSize - 1 ) : ( pos_x < ChunkSize ? 1 : pos_x / ChunkSize + 1 ) );
result.second = ( pos_y < 0 ? ( pos_y >= -ChunkSize ? -1 : pos_y / ChunkSize - 1 ) : ( pos_y < ChunkSize ? 1 : pos_y / ChunkSize + 1 ) );
return result;
}

``````

BTW Using signed ints and omitting 0 is not the best idea.

Thanks!
Will try it out when i get home.