# Calculating Rows in an inventory. Help simplifying code.

Hey

Can anyone help me simplify this this code. Im using it to calculate the ammount of rows in my inventory but there must be a cleaner way of doing it. At the moment I am setting each rowPosition based on the number of items in the inventory.

``````            if (i < inventoryColumns) {
rowPosition = 0;
}
else if (i < inventoryColumns * 2) {
rowPosition = (slotHeight + slotSpacing);
}
else if (i < inventoryColumns * 3) {
rowPosition = (slotHeight + slotSpacing) * 2;
}
else if (i < inventoryColumns * 4) {
rowPosition = (slotHeight + slotSpacing) * 3;
}
else if (i < inventoryColumns * 5) {
rowPosition = (slotHeight + slotSpacing) * 4;
}
``````

Any help simplifying this would be appreciated.

Thanks - Matt

Just off the top of my head

You need to express i against the inventoryColumns as a integer couting to the size.

for

f(i)=rounddown(i/inventoryColumns) // This will be acheved by the caste to (int) in the below code.

``````rowPosition = ((slotWidth * 0.6f) + slotSpacing) * ((int)(i/inventoryColumns));
``````

**** Warning this was untested since I don’t have all the values of your solution ****

I think you want:

``````rowPosition = (i / inventoryColumns) * (slotHeight + slotSpacing);
``````

Lots of else ifs… In cases like this you usually use a switch. But the way your conditions are structured, I can’t think of a way to put them into switch form. Either case, if you plan of having more rows in the future and each requiring a separate if statement, a switch is definitely what you’re looking for. You’ll just have to re-structure your code a bit to make it work.