Help with a Custom Grid System

I’ve been working on a grid building system, and I’ve come across an issue. I can’t seem to figure out how to make the grid system work with multiple sized objects. For example, in Blockate. That game has varieties of block sizes. Full (4, 4, 4), Slab (4, 2, 4), Small (2, 2, 2), and Pillar (2, 4, 2). How would I modify my current grid system to achieve this goal?

local function ReturnCalculated(Offset)
	
	local Target = Mouse.Target
	if Target then
		
		local convertedCF = Target.CFrame:ToWorldSpace(Offset)

		return convertedCF * Rotation
		
	end
	
end
local function CalculatePosition(targetSurface)
	
	local GridX = CurrentPlaceholder.Size.X
	local GridY = CurrentPlaceholder.Size.Y
	local GridZ = CurrentPlaceholder.Size.Z
	
	if targetSurface == "Front" then
		
		local convertedCF = ReturnCalculated(CFrame.new(0, 0, -GridZ))
		return convertedCF
		
	elseif targetSurface == "Back" then
		
		local convertedCF = ReturnCalculated(CFrame.new(0, 0, GridZ))
		return convertedCF
		
	elseif targetSurface == "Left" then

		local convertedCF = ReturnCalculated(CFrame.new(-GridX, 0, 0))
		return convertedCF
		
	elseif targetSurface == "Right" then

		local convertedCF = ReturnCalculated(CFrame.new(GridX, 0, 0))
		return convertedCF
		
	elseif targetSurface == "Top" then

		local convertedCF = ReturnCalculated(CFrame.new(0, GridY, 0))
		return convertedCF
		
	elseif targetSurface == "Bottom" then

		local convertedCF = ReturnCalculated(CFrame.new(0, -GridY, 0))
		return convertedCF
		
	end
	
end

Those scripts above are my main functions used to calculate positioning. It also includes my attempts to counter this issue.

Edit 1: Objects that are normal sized also have issue being placed onto smaller objects.

1 Like
  1. Check all the tiles if the grid allows you to build there.
    A) If it is a valid space.
    B) If it is not filled.
  2. It works now.

Also have a priority tile/offset which is part of the model which is a part that moves around with your mouse (I recommend having this as the middle, but you can figure that out for yourself).

ah i hate grids. genuinely. ive made 4-5 grid inventories, only 1 is cohesive, rest were thrown away or buggy.

i dont want to give you a too direct answer but i want to give you a really good pointer

say you have an item I sized (A, B) and at position (X, Y) (top left corner of item). then the full extents of the item is (X + A - 1, Y + B - 1). why is it not just (X + A, Y + B)? because say the item is at position (1, 1) and is sized (1, 1). then the top left is (1, 1) as desired, but the bottom right would be (2, 2)- even though the item is sized (1, 1) it spans 4 grids. subtracting 1 from X and Y in the extent solves this.

so we to know if another item is within I’s boundaries. we know it’s boundaries, (X, Y) to (X + A - 1, Y + B - 1), so now we check each grid in every column of every row (or vice versa doesn’t matter). if an item is in that grid space, we cannot put I there, else we can. we are looking for overlapping items.

local i = {size = Vector2.New(1, 1), pos = Vector2.New(3, 3)}
local grid = {}
for _ = 1, 8 do -- 8 x 8 grid. this line is for columns
       table.insert(grid, table.create(0, 8)) -- create a unique table of 8 zeros and insert it to grid
end

grid[4][6] = 1 -- grid at column 4 row 6 is '1'

local isOverlapping = false
for col = i.pos.X, i.pos.X + i.size.X - 1 do
       for row = i.pos.Y, i.pos.Y + i.size.Y - 1 do
              if (grid[col][row] == 1) then
                     print('overlapped item!')
                     break
              end
       end
end

move around the item and see what happens
add more dimensions as necessary
do checks to make sure the item isn’t OOB
and dont think this is just for items

please don’t loop through all the tiles. if your grid is small, sure it works, but you can also be selective and only look at the space required by the object with just a little maths.

I think you read it wrong, what I meant, is like a building grid game. Not an inventory system. And, incase you were wondering, the grid is the ENTIRE workspace.

You can alter it for the building system you have in mind very easily. I just showed u an example of an inventory system grid to help you. I read it right

1 Like

But, if I were to include the Z, would i add a (Z + C - 1)?

Yeah thats the beauty of mulfiple dinensions. You can add as many as you want.

I have a few questions. Will I have to replace my ENTIRE grid system? How would I replace an item inside the GRID table when I’m placing a block? And, How does the creating grid work? I printed the grid inside studio to see how it works, and I only see 8 tables of items. Is that how it should work?

No you dont need to replace your entire grid system i dont tihkn

So basically when you add smth to the grid you expand the grids minimum and maximum size. And you fill in thr gaps in the table.

Nah i have an even better solution
Stay tuned because im on phone and am too tired

Alright, thank you for giving a code example though.

Incase you want to see how the game looks so far, here’s the link. Blockate Test - Roblox

so basically, you do as i said for the inventory example. but if the grid at some point between X and X + A - 1 is null, you count it as an empty slot. instead of creating a grid per grid placement, and expanding the grid based on the minimum and maximum positions possible, you just ignore any null columns, and if the columns do exist, rows. it’s extremely simple and it’s as simple as you’re thinking.

for col = i.pos.X, i.pos.X + i.size.X - 1 do
       if (not grid[col]) then continue end
       for row = i.pos.Y, i.pos.Y + i.size.Y - 1 do
              if (not grid[row]) then continue end
              if (grid[col][row] == 1) then
                     print('overlapped item!')
                     break
              end
       end
end

and then when we’re placing an item, we create new columns/rows in such a way that only the necessary columns and rows (the spaces the item will occupy) exist

-- checking overlap
for col = i.pos.X, i.pos.X + i.size.X - 1 do
       if (not grid[col]) then continue end
       for row = i.pos.Y, i.pos.Y + i.size.Y - 1 do
              if (not grid[row]) then continue end
              if (grid[col][row] == 1) then
                     print('overlapped item!')
                     break
              end
       end
end

-- placing
for col = i.pos.X, i.pos.X + i.size.X - 1 do
       if (not grid[col]) then grid[col] = table.create(0, 8) end
       for row = i.pos.Y, i.pos.Y + i.size.Y - 1 do
              grid[row] = i
       end
end

this is so simple and elegant (and is a break from my own lobotomy of an inventory)

if its not a 3 dimensional array then yeah unfortunately. but i pretty much gave you an entire grid system

im gonna take this literally and assume you mean reposition as in replace (and that position = place). i would have a table in i called ‘points’, and whenever i place an item at some grid, i clear the points and allocate new points to the item. the poitns would look like {{x, y, z}, {x, y, z}, …} etc. i would create the points array inside the item at the start of the positioning item function, and for each grid the item replaces i table.insert(points, {x, y, z})

if you mean replace as in swap out- get all overlapping items and clear them, as well as the ‘points’ array in the overlapped items. then place the item i at that position in the grid.

multi-dimensional arrays. so for this system its in the format of columns and rows. for every column there are 8 rows. this can be written as

for column = 1, 8 do -- say 8 columns
       -- for every column there is allocated 8 rows
       grid[column] = table.create(0, 8) -- create an array of 8 zeroes
end

yes that’s correct. when you print the grid (using the 8x8 grid i just wrote in code form ^) it should be 8 arrays with 8 zeroes, representing rows, inside 1 large array, representing columns. each index of the columns array is the column number, and each index inside every column is the row number.

lowkey i can make a grid placing system for you w/ multiple sized objects, and modularized, for a price of 20$. my fiverr got terminated (didnt update it for EU tax regulations [tax evasion]) but we can link up in discrod

If you were to do that, I wouldn’t be able to pay, because I’m too young to even USE fiverr and stuff. Thanks for the offer though. I don’t have discord either.

1 Like

Ok thats fair. Hows the grid system going for you

Terribly, I still can’t figure it out, so I’m just working on other things.

What are you habing trouble with? Was i too indirect wifg my solution

My issue is that, the positioning isn’t as good with objects that are smaller (3, 1.5, 3) than the normal object sizing (3, 3, 3). The positioning is wacky, on the Z and X positioning, it starts at the center of the object, then move in the direction making it look floaty and obscure. The Y positioning is slightly inside the object. I can show you images of it in a bit.


As you can see, the block ABOVE the main block is Z Fighting with it. And the X and Z are centered on the sides.

THIS is basically what I’m aiming for. I got it to work by making a block out of slabs. I’m just really confused on how I can get this to happen.

When Objects are rotated, their positioning is also wacky.

You kind of were, my brain was fumbled for a bit. Very confusing if I’m being honest, but it DID help a little bit.

1 Like