Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RetnuhStudio · Jul 01, 2013 at 03:01 PM · arrayposition

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MissingSemicolon · Jul 01, 2013 at 03:26 PM 0
Share

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!

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RetnuhStudio · Jul 01, 2013 at 03:49 PM 0
Share

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;
avatar image Travis-Bulford · Jul 01, 2013 at 03:55 PM 1
Share
 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 :)

avatar image RetnuhStudio · Jul 01, 2013 at 03:58 PM 0
Share

That does the trick exactly. Thanks heaps man. $$anonymous$$uch cleaner haha.

avatar image
0

Answer by robertbu · Jul 01, 2013 at 03:27 PM

I think you want:

 rowPosition = (i / inventoryColumns) * (slotHeight + slotSpacing); 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Travis-Bulford · Jul 01, 2013 at 03:30 PM 0
Share

Not sure % will do the trick that's going to return the remainder of i/inventoryColumns

avatar image robertbu · Jul 01, 2013 at 04:02 PM 0
Share

You are right. I fixed it just after I posted it.

avatar image Travis-Bulford · Jul 01, 2013 at 04:04 PM 0
Share

hehe I saw that just now, turns out the final answer needed one anyway :)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges