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
1
Question by ThiagoTejo · Dec 12, 2013 at 05:18 AM · variablebooleanifoptimizing

How does Unity reads the "If" conditions?

Hello guys, I'm trying to optimize my game, and I do a lot of conditions like this in the update function:

 if(boolean == true && variable > 5)

Let's say that the boolean is false. I assume that unity will not even check the variable, because the boolean is false, am I right?

Or I have to do in separate If's?

 if ( boolean == true )
 {
 if (variable > 5)
 {}
 }

this second case, i'm sure that unity won't check the variable, but most of my code I wrote usign the first exemple to prevent poluting the lines... I'm asking this, because i saw lots of scripts that people wrote just like the second exemple, and I want to know wich is better, because i want unity to avoid checking unnecesary variables every time.

Comment
Add comment
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

3 Replies

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

Answer by robertbu · Dec 12, 2013 at 05:32 AM

Both C# and Javascript use short-circuit boolean. That means that if the first condition is false, the second will not be executed. Short-circuit boolean is done in a majority of languages, but it's not true of all programming languages.

Note you don't have to do 'boolean == true' you can just 'boolean' So you code would be:

 if (boolean && variable > 5)

Note you can test a language by using a function that returns a boolean in an 'if' statement:

 function BoolTest() : boolean {
     Debug.Log("Testing");
     return false;
 }

Then to test:

 if (BoolTest() && BoolTest()) {
    //
 }

If you only get one 'Testing', then it is Short circuit boolean.

Comment
Add comment · Show 2 · 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 ThiagoTejo · Dec 12, 2013 at 02:18 PM 0
Share

Thank You very much! :)

avatar image Hoeloe · Dec 12, 2013 at 09:53 PM 1
Share

It's useful to know this, because it can help in optimising programs later. For example, if you have a condition like this:

 if(someList.Contains(someElement) && someBool)

it may run slower than this:

 if(someBool && someList.Contains(someElement))

in the average case. This is because someList.Contains is an expensive operation, while boolean equivalence testing is not. By putting the cheaper operations first, you reduce the number of times the more expensive ones are evaluated, which can save some time. Having said that, it is not always possible to do this if the more expensive operation has side effects.

avatar image
2

Answer by KellyThomas · Dec 12, 2013 at 05:35 AM

"&&" and "`||`" are shortcut boolean operators, working from left to right the program will only evaluate as much as is strictly necessary.

The Wikipedia page on "short circuit evaluation" will go into further depth.

If you wanted to evaluate the expression completely (e.g. to trigger side effects) then you could use "`|`" and "`&`".

update:

Generally speaking chaining functions with side effects into complex boolean statements is discouraged. Separating functions that query state from methods that change state will usually result in code that is easier to understand and maintain.

However as an example I offer:

 bool strengthRoll() {
     if (Random.Range(0,11) < 8 ) {
         strengthXP++;
         return true;
     }
     return false;
 }

 bool dexterityRoll() {
     if (Random.Range(0,11) < 8 ) {
         dexterityXP++;
         return true;
     }
     return false;
 }

 //then in the update:
 if (buttonPressed()) {
     if (strengthRoll() & dexterityRoll()) {
         //special attack
     }
 }

Here each of the stat rolls has the sideeffect of incrementing a separate XP counter.

strengthRoll and dexterityRoll are joined by the "`&`" so they will both be called even if the first returns true, as a result their XP will increase evenly.

If they were joined by the "`&&`" then dexterityRoll would only be called when strengthRoll returns true, in that scenario dexterityXP would increase at a lower rate to strengthXP.

As you can see the impacts of operator selection can have subtle effects on program behavior. While it is important to be able to recognize it when reading other peoples code , I would try to favor a more overt style when writing. One of best measures of code quality is that it does what you expect of it.

Comment
Add comment · Show 1 · 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 ThiagoTejo · Dec 12, 2013 at 02:26 PM 0
Share

Thanks for the awser! But in that case, can you give me a exemple in wich its better to use "&" ins$$anonymous$$d of "&&"? I can't think of any!

avatar image
0

Answer by YoungDeveloper · Dec 12, 2013 at 06:25 PM

This also might bu useful: http://answers.unity3d.com/questions/537514/if-else-how-the-program-reads-it.html

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

19 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 avatar image

Related Questions

ANOTHER Boolean Problem 1 Answer

Store Multiple values in one variable 2 Answers

Set a global bool to true? 1 Answer

Flag structure variable in unity editor 1 Answer

Unity says there is no such thing as a boolean. 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