GridPack - Create Grid/Tetris Style Inventories

No sadly there is not full console support yet, though it is going to come in a future update. If you enable the virtual cursor it might work.

I use canvasgroups for each item but even when I dont it doesnt seem to make a difference on highlight

Sorry, I forgot about your reply. Have you changed the slot assets in any way? You don’t need any configuration to have highlights working. You just need to create a grid and an item.

Hey, I’m not sure if this has been asked before, but does anyone know how to loop through the available grid spots to try and find a spot for the item if the one it’s trying to place itself to isn’t available when you use the createItem function? Because right now it’s really annoying if I forgot to move an item out of the starting position and it just eats the item.

local function CreateItem(ReplicatedItem)
	local ItemFrame
	
	local Size = ReplicatedItem:GetAttribute("Size")

	local Item = GridPack.createItem({
		Position = Vector2.new(0, 0),
		Size = Size,

		Assets = {
			Item = game:GetService("ReplicatedStorage"):WaitForChild("ItemElement"),

		},

		Metadata = {
			-- Here you are free to store any values you want.

		},
	})
	
	Items[ReplicatedItem] = Item

	local ItemFrame

	ScreenGui:WaitForChild("Background").ChildAdded:Connect(function(Child)
		if Child and Child.Name == "ItemElement" then
			ItemFrame = Child

		end
	end)

	Grid:AddItem(Item)
	
	repeat task.wait() until ItemFrame
	
	ItemFrame:WaitForChild("Item").Value = ReplicatedItem

	local Item = ReplicatedItem:Clone()
	Item.PrimaryPart.Anchored = true
	Item:SetPrimaryPartCFrame(Item:GetAttribute("ViewportCFrame"))
	Item.Parent = ItemFrame:WaitForChild("ViewportFrame")

end

My function if anyone can help or if this even helps anyone else.

Use pcalls to see if the item adding failed. And to get an empty position for the item use Grid:GetNextFreePositionForItem().

1 Like

Sorry, silly question, but I’ve never really used pcalls before. I’ve attempted to use this, but it doesn’t seem to work, so how do they work in this case?

	local Success, Item = pcall(function()
		return GridPack.createItem({
			Position = Vector2.new(0, 0),
			Size = Size,

			Assets = {
				Item = game:GetService("ReplicatedStorage"):WaitForChild("ItemElement"),
				
			},

			Metadata = {}
			
		})
	end)

	if not Success or not Item then
		print("Error")
		return

	end

It still prints “Success” as true even when it errors.
(Also how do you even use GridGetNextFreePositionForItem? I’ve tried doing Position = Grid:GetNextFreePositionForItem(), however that obviously didn’t work, but funnily enough that did trigger the pcall)

Mythaman found a fix

Attempting to move a grid Item into a grid that won’t fit it causes multiple errors. · Issue #21 · Frexsim/grid-pack - github link

I’m not really sure what this is supposed to fix, as my problem is regarding the Grid:AddItem() function and not the Item.new function. I replaced it anyway, however it still doesn’t catch the error.

Yeahhh, sorry, y’all, I was up for ~24 hours at that point and was not thinking clearly, lol. I fixed it the main issues by just setting the item position to the next free position via Item.Position = Grid:GetNextFreePositionForItem(Item)

1 Like

Btw pcalls are used for error handling so if any of the lines fail then the pcall returns success = false and an error message so for example:

-- this pcall will always return an error message
local success, errorMessageOrReturn = pcall(function()
    error("example error message")
end)
-- this pcall will always return "example value"
local success, errorMessageOrReturn = pcall(function()
    return "example value"
end)
-- put this under the pcalls to handle the errors
if success == false then
    print(errorMessageOrReturn) -- always prints error message because it failed (in this case "example error message")
else
    print(errorMessageOrReturn) -- always prints the thing you returned in the pcall (in this case "example value")
end

Pcalls are often used in things like data stores since Roblox’s data store api doesn’t always work since the servers might be down or slow.

As in the slots in the grid, I dont believe I have. Only thing I have done is in the script the transferlink gets enabled and disabled and the grid frame gets visible turned on and off

The only thing that I think you could be doing wrong is turning the visibility on and off. You should be using ItemManager:SetVisibility() instead of ItemManager.GuiElement.Visible. So switch to that if you’re not using it.

Highlights should be working out of the box, you just need to create a grid and an item, no configuration needed.