Beginner – 03 Conditions – If Then Else

  • An if statement do one thing if a compare is true another if it is false..
    --remember to add two == if you want to compare
    -- "==" is exactly the same
    -- a > b --is a bigger than b then return true else false
    -- a < b --is a smaller then b then return true else false
    -- a != b --if a is NOT equal to b then return true else false
    
    -- So if the compare returns true run the script
    -- inside the parentheses else not at all...
    
    --
    
    a = 5
    b = 5
    
    if a == b then (
      print "a and b are the same"
    ) 
    
    -- often you want to do one thing if it is true
    -- and another if it is false
    
    a = 5
    b = 6
    
    if a == b then (
      print "a and b are the same"
    ) else (
      print "a and b are not the same"
    )

    Read more about comparison expressions here

  • When you want to choose certain objects. Say I want all boxes that has a height over 100.0 units. Or Want to change all lights that has a intensity of 50 but not anything else. You can use the IF THEN command
    --First create an array of all boxes in the scene
    
    myBoxes = $Box* 
    
    --this is a rather unsafe way since it selects all
    --objects based on their name.. so if you have a
    --sphere that someone named "BoxXyz" it will also
    --be included.. and then it does not have a height
    --property
    
    --A safer way to select is
    --make sure it is a box we are selecting.
    
    --You can get the type of class an object is..
    ClassOf $ --this gets the class of the selected object..
    
    --This collects all objects that is of classOf box
    myBoxes = for item in objects where (classOf item == Box) collect item
    
    --Ok now we got an array with just objects
    --that inherits from the class Box
    
    --Lets create a for each loop that
    --prints out if the value is below 50 or above..
    
    for b in myBoxes do (
      if b.height > 50.0 then (
        print (b.name + " is soo very high")
      ) else (
        print (b.name + " is feeling low")
      )
    )