- Home /
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
You may need to post more of the code, either the rest of the function or class; whatever will allow everyone to see exactly what your variables are for and exactly what it is doing. Cheers!
Answer by Travis-Bulford · Jul 01, 2013 at 03:29 PM
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
Thanks for the reply Travis. This looks like it works however now I need to reset the x position of the row everytime a new row is created.
This is the current code.
rowPosition = ((slotWidth * 0.6f) + slotSpacing) * ((int)(i/inventoryColumns));
slotPosition = ((slotWidth + slotSpacing) * i) + slotSpacing;
rowPosition = ((slotWidth * 0.6f) + slotSpacing) * ((int)(i/inventoryColumns));
slotPosition = ((slotWidth + slotSpacing) * (i%inventoryColumns)) + slotSpacing;
Should do the trick Assu$$anonymous$$g I am making head or tails of which is your x and y and what you mean by resets :)
That does the trick exactly. Thanks heaps man. $$anonymous$$uch cleaner haha.
Answer by robertbu · Jul 01, 2013 at 03:27 PM
I think you want:
rowPosition = (i / inventoryColumns) * (slotHeight + slotSpacing);
Not sure % will do the trick that's going to return the remainder of i/inventoryColumns
hehe I saw that just now, turns out the final answer needed one anyway :)
Answer by Em3rgency · Jul 01, 2013 at 03:29 PM
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.
Your answer
Follow this Question
Related Questions
NGUI - Vector 3 Position different from position 1 Answer
Array doesn't increase or add element 1 Answer
Make object move in a direction depending on where it spawns? (C#) 1 Answer
How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers
Spawn object at a random predeterminated(transform) position 1 Answer