Hi,
So I’m creating an inventory system and I want the player to have an m x n grid of slots. How would I create a widget that can create exactly m x n slots uniformly?
Hello,
Here are 2 straightforward ways of doing it:
This one is extremely simple and does the job pretty well.
This one on the other hand is more complex but is way nicer and has replication cappabilities. Note that the second approach takes way longer to do.
You simpl add a grid Widget to your Inventory.
there, you can set Column/row of each children, when adding a new child to grid via “AddChildToGrid”.
The column is simply
col = MaxSlotWidth - (CurrentIndex % MaxSlotWidth
row = (CurrentIndex % MaxSlotWidth) + Math::Floor(CurrentIndex / MaxSlotWidth)
This is a pseudo code, you can simply convert to BP by having two Integer Variables:
- MaxSlotWidth = Maximum count of Columns in the Inventory
- CurrentIndex = The current Index of an Item of your Item Array f.e. the index of the foreach loop
Math::Floor is just a BP Math Node called “Floor”, used to round float values DOWN to a flat Integer.
col and row are the Inputs “Column” and “Row” of the AddChildToGrid.