Code to Create Item in Shape Grid position

Zaddo

Active member
Hi, I have a custom process for saving and restoring the game state. I can't work out how to create an inventory item and place it in the shape grid at a specific position. Could you please provide a code example?

This is the code I use to get the item's position:
C#:
var shapeGridController = itemCollection.Inventory.gameObject.GetComponent<ItemShapeGridController>();
var gridData = shapeGridController.GetGridDataForItem(itemInfo);
var pos = gridData.GetItemIndex(itemInfo);
 
We have a component that saves the grid data built in. It is called "ItemShapeGridDataSaver". You could use that if you are using our save system. Or you could have a look at the code and inspire yourself from it.
 
Thanks. I looked through the save system. But didn't notice that component. Thanks for the pointer, that is perfect. Thanks.
 
In case someone else is struggling with how to do this. This is a code example for spawning an Item and placing it at a specific position in the shape grid. There might be a better a way, but this works.

C#:
// Example data
uint myItemDefinitionId = 1234; // The item I want to create
int qty = 1;  // Quantity of item
int gridPosition = 18; //

ItemInfo newItemInfo;

var itemDefinition = InventorySystemManager.GetItemDefinition(myItemDefinitionId);
var item = InventorySystemManager.CreateItem(itemDefinition);
var inventoryIdentifier = InventorySystemManager.GetInventoryIdentifier(1);
var inventory = inventoryIdentifier.Inventory;

newItemInfo = inventory.MainItemCollection.AddItem(item, qty);

var shapeGridData = inventory.gameObject.GetComponent<ItemShapeGridData>();
if (shapeGridData == null)
{
    Debug.LogWarning($"Could not find ShapeGridData");
    return;
}

var gridData = shapeGridData.Controller.GetGridDataForItem(newItemInfo);
var currentPos = gridData.GetItemPos(newItemInfo);
var newPos = shapeGridData.OneDTo2D(gridPosition);
shapeGridData.TryMoveIndex(currentPos, newPos);
 
Top