Search Unity

Newbie guide to Boo - "What do you mean there's less typing?!"

Discussion in 'Scripting' started by heromal, Jun 23, 2011.

  1. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
    Rest will be finished tomorrow hopefully, but since I don’t have my own computer and my parents use it often(im 15), I apologize if I can’t have it finished then. I will also fix the indentation(I did this in MS Word, 'nuff said).

    Note before reading: this is a modified version of TonyD’s javascript tutorial. If you can’t decide between UnityScript and Boo, you can see exactly how they compare by looking at the examples. The only thing I take credit for is editing the scripts and making them Boo code. If you see any problems feel free to let me know :)
    Furthermore, I explain this from my own experience. If you know the basics-such as what variables, functions, and the like- are, you will have no problem. There are many tutorials that explain the basics, and I do not plan to reinvent the wheel as far as that goes. However, I will explain pitfalls you may encounter while using Boo, as well as some differences that may not be obvious. Finally, this is my first tutorial ever, so if and when I make technical mistakes, understand that this is a learning process for me as well.

    If you want to run the sample code, you do need to be familiar with the Unity interface and know how to create a script and attach it to a game object. See links below.

    Intro to the Unity Interface:
    http://download.unity3d.com/support/Tutorials/1 - GUI Essentials.pdf

    Intro to Scripting with Unity - read sections labeled "Our first script", and "Attaching the script".
    http://download.unity3d.com/support/Tutorials/2 - Scripting Tutorial.pdf

    Text in RED applies to Unity iPhone users only.


    VARIABLES


    Declaring and using variables in Boo is different from both JavaScript and C#. If you already know what value you want your variable to hold, you can simply give it that value. Example:
    Code (csharp):
    1. box = “apple”
    You should have noticed a few things about this example:
    1. It is not necessary to use semicolons. Boo is based on whitespace, meaning tabs, spaces, and newlines. It will know when you finished a statement after you start a new line. If you want to use semicolons or you can't get out of the habit, Boo won't have a problem with you ending a statement with a semicolon as you would in many other languages.

    2. It is not always necessary to declare the type of variable, as you normally would in C#. The type will be determined upon compilation. This is a life-and time-saver for me.

    3. There is no keyword for “declaring” a variable. Simply assign it a value.
    The shortest possible JavaScript equivalent to this in one line would be:
    Code (csharp):
    1.  
    2. sayHello = “hello!;
    3.  
    Even that small example has more characters than its Boo equivalent, even though it's only one more.*
    *



    Now say you know you know you need the variable box, and you know what type it is, but you don’t yet know what value will be stored in box:
    Code (csharp):
    1.   box as string
    That’ll do the trick 
    In case you’re following along with the JavaScript tutorial at hand, that’s the same as:
    Code (csharp):
    1.  var box : string;
    Again, less characters.
    You can also use the same convention for floats(which are known as “singles” in boo), integers, and Booleans:
    Code (csharp):
    1.  
    2. number as single = 1.14 // you could also say number = 1.14, but I’m just demonstrating the types
    3. intNumber as int = 1 # again, you could also say number = 1
    4. gameOver as bool = false //see above. gameOver = false is the same.
    5.  
    You can use a hashtag(#) or double forward slashes(//) to denote comments



    Notice that a variable can normally only contain one thing at a time. But we can make a variable that contains multiple things, by creating an array:

    Code (csharp):
    1. applePie = ("apple", "brown sugar", "butter", "pie crust")
    This array contains everything you need to make an apple pie! Again, you don’t need to do this, but if you wanted your Boo to look more verbose and/or like C#/ UScript, you could say:
    Code (csharp):
    1.  applePie as Array = ("apple", "brown sugar", "butter", "pie crust")
    Unfortunately, that’s the longest I could make it 

    I will also take this time to explain lists. Lists in Boo can contain all types of values, and are mutable, meaning you can change them.
    Code (csharp):
    1.  applePie = ["apple", "brown sugar", "butter", "pie crust",6,false]
    Of course I don’t know why you would want to put 6 or false in your apple pie, but it’s worth a shot :p

    If we wanted to view the contents of applePie, we could output it to the Unity console using the Debug command. Add this line to the code above:

    Code (csharp):
    1. Debug.Log(applePie)
    On play, the console should display:

    apple,brown sugar,butter,pie crust

    To access a single element (item) from an array, we can access it thru its index (position in array). This may seem confusing, but indexes start at 0. So index 0 is actually the first element in the array.

    Code (csharp):
    1. applePie =("apple", "brown sugar", "butter", "pie crust")
    2. item1 = applePie[0]
    3. Debug.Log(item1)
    You can actually use indexing with Strings as well. The following code displays the first character of "hello", the letter 'h':

    Code (csharp):
    1.  myString = "hello"
    2. Debug.Log(myString[0]);
    Now lets alter our variables in other ways.

    We can add variables:
    Code (csharp):
    1. number1 = 10
    2. number2 = 11
    3. total = number1 + number2;
    4. Debug.Log(total)
    If you add an int (integer) to a single (floating point number) the result becomes a single(if you’re going to be using boo get out of the habit of using float now, it’ll save you precious debugging time later on):
    Code (csharp):
    1. number1 = 100
    2. total = number1 + 1.5
    3. Debug.Log(total)
    Obviously, we can also subtract/divide/multiply ints and floats.

    Less obvious, we can also 'add' strings. This merges them together:
    Code (csharp):
    1.  string1 = "apple"
    2. string2 = "cider"
    3. combo = string1 + string2
    4. Debug.Log(combo)
    If we add a number to a string the result becomes a string.

    We can also multiply strings.
    Code (csharp):
    1.  greeting = "howdy"
    2. Debug.Log(greeting * 3)
    Unfortunately, we can't divide or subtract strings. There are ways to split strings and remove parts of strings, but that is a more advanced topic.

    Let's wrap up our discussion on variables with incrementing numbers (changing their value by a set amount).

    First, declare a variable and set it to 1:

    Code (csharp):
    1. number = 1;
    We can increment numbers various ways.

    Code (csharp):
    1. number = number + 1
    The above adds 1 to our number, and it becomes 2.

    But programmers are lazy, and decided this was too long. So they abbreviated it to:

    Code (csharp):
    1. number += 1
    This is simply shorthand for number = number + 1

    But guess what? Lazy programmers decided even THIS was to long, and shortened it to:

    Code (csharp):
    1. number ++
    Use whichever makes most sense to you, they all do the same thing! But note that if you choose to increment by a value other than 1, ++ won't work.

    ++ is shorthand for += 1 only.

    You can also do the same with subtraction:

    Code (csharp):
    1. number --;
    But for division and multiplication, you must use one of the longer forms:

    Code (csharp):
    1. number = number/2;
    2. number *= 2;
    Note that an asterisk means multiply, a slash means divide.


    IF STATEMENTS

    If statements are conditional statements. If a condition evaluates as true, do something.
    [color = blue] Pitfall [/color]
    You ALWAYS indent after an if block. Remember that pressing the tab button once makes a code block. Whereas space doesn’t matter in other languages, it does in Boo. This will become clearer as we move on.
    [color = blue]End Pitfall[/color]

    We can do a comparison of two values by using two equal signs, ==

    "number == 10" evaluates as true if our number variable equals 10, otherwise it evaluates as false.

    Note: it's important to remember to use two equal signs when comparing variables/values, but one equal sign when setting a variable to a value!

    The following creates a variable and sets it to true, checks to see if the variable equals true, and if so prints a text string to the console:

    Code (csharp):
    1.  
    2.  gameStarted = true
    3. if (gameStarted == true):
    4.     Debug.Log("Game has started")
    The above is actually redundant, since our variable 'gameStarted' is a boolean. There is no reason to check "if true equals true", just check "if true":

    Code (csharp):
    1.  
    2. gameStarted = true
    3. if (gameStarted):
    4.     Debug.Log("Game has started")
    It is not necessary to put the condition(the stuff after the keyword if) inside of parenthesis, but it is a habit I picked up, and won’t bother many people if you do it.



    When if contains only one statement to execute, the brackets are optional. But if it contains more than one statement, you MUST use the brackets! Note that semicolons are not needed after brackets.

    Code (csharp):
    1.  
    2. gameStarted = false
    3. If (gameStarted == false):
    4.     gameStarted = true
    5.     Debug.Log("I just started the game")
    6.  
    Read the second line of code above. Remember those lazy programmers? They don't want to write

    Code (csharp):
    1. if (gameStarted == false)
    When they can just write:

    Code (csharp):
    1. If (not gameStarted)
    But you know what? Why write 'not' when I can shorten that too?

    Code (csharp):
    1. if (! gameStarted)
    Yes, an exclamation point means 'not' to lazy programmers!

    You can also combine this with equals, where it means "not equals":

    Code (csharp):
    1. answer = 1;
    2. if (answer != 42) :
    3. Debug.Log("Wrong question!")
    An elif block is executed if the first if block condition is not satisfied. This way you can check for multiple situations, a very handy tool indeed.
    Note: Just to throw this out there, in JS you say “else if” whereas in Boo you say “elif” :D
    If no if or elif block is satisfied and is not executed, you will execute the else block.

    You can also check for greater than or less than:

    Code (csharp):
    1.  age = 18
    2. if (age > 18):
    3.     Debug.Log("old enough")
    4. elif(age < 18):
    5.     Debug.Log("jailbait")
    6. else:
    7.     Debug.Log("exactly 18")
    You can also check for multiple conditions in a single statement:

    Code (csharp):
    1.  
    2. if (age >= 21 and sex == "female"):
    3. buyDrink = true
    Above, we introduced greater than or equal to >= and the and operator, which is two ampersand characters in other languages: . If both conditions are true, the statement is executed. If even one is false, the statement is not.

    Note: if you want to run the above code, remember to create variables for age (int), sex (String), and buyDrink (boolean) first!

    Code (csharp):
    1.  
    2. if (engine == "Unity" or developer == "friend")
    3.     buyGame = true
    Above, we used the or keyword, which is two pipe characters in other languages: ||. If either condition is true, the statement is executed. If both are false, the statement is not.

    Note: if you want to run the above code, remember to create variables for engine (String), developer (String), and buyGame (boolean) first!

    If can also be used with the keyword 'in'. This is generally used with Arrays:

    Code (csharp):
    1.  
    2. names = ("max", "rick", "joe");
    3. if ("joe" in names):
    4. Debug.Log("Found Joe!")

    LOOPING

    Looping allows you to repeat commands a certain amount of times, usually until some condition is met.

    What if you wanted to increment a number and display the results to the console?

    You could do it this way:
    Code (csharp):
    1. number = 0;
    2. number += 1
    3. Debug.Log(number)
    4. number += 1
    5. Debug.Log(number)
    6. number += 1
    7. Debug.Log(number)
    And so on... but this is redundant, and there is nothing lazy programmers hate more than rewriting the same code over and over!

    So lets use a For Loop:

    Code (csharp):
    1.  
    2. number = 0
    3. for(num in range(number,10)):
    4.     number++
    5.  
    Okay, that for statement on the second line may look a little confusing. But it's pretty simple actually- even more so than its C#/JavaScript equivalent.
    num in range(number,10)
    this is just using num as a throwaway variable, meaning you can name it anything. It will start at number(the first variable we passed in to range) and go to 10(the second variable passed in to range), incrementing num by 1 each time. This is the same as doing number++ ten times, except it’s a lot shorter. I realize I didn’t explain this very well, so if you have a specific question please feel free to ask 
    http://docs.python.org/library/functions.html#range
    should be of help as well.


    Or print all even numbers between 0 and 10:
    Code (csharp):
    1.  
    2. number = 0
    3. for num in range(0,10,2)
    4.     Debug.Log(num)
    5.  
    The third variable passed in to range(2) tells us what to count by, in this case, we’re starting at 0,ending at 10, and counting by 2, effectively giving us only even numbers between 0 and 10.
    The rest will be finished tomorrow 
     
    Last edited: Jun 24, 2011
  2. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
    Please also let me know what else you would like to see in this guide( the second half is the same as the second half of Tony's JS tutorial).
     
  3. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Pretty good, esp. considering your age. Few nitpicks:

    Neither JS nor C# require you to declare the type of the variable (though I almost always do it because I find my code easier to understand that way).

    -----

    Code (csharp):
    1. var sayHello = “hello!;
    Is completely unfair, you make the variable name, and the string it contains longer!

    Try:

    Code (csharp):
    1. var box = “apple”;
    (Should be valid JS and C#).

    -----

    I do agree with your point about white-space (when I started VB was heavenly and C# a hideous mess), I think your point about the amount of typing is not so easily defined.

    Take:

    Code (csharp):
    1.  box as string
    Is actually longer than the C#:

    Code (csharp):
    1.  string box;
    -----

    Question, how does that boo list work?

    Is it just a Object array or an Arraylist?
     
    Last edited: Jun 23, 2011
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I see some innaccuracies:

    There are; you can use them if you want, but they're optional.

    It's not always necessary in C#:

    Code (csharp):
    1. var box = "apple"; // valid C#, defines box as a string
    The shortest possible in Javascript is:

    Code (csharp):
    1. sayHello = "hello!";
    As long as you're not using #pragma strict (or you are, but you're also using #pragma implicit), that implicitly declares sayHello.

    In Javascript you can do

    Code (csharp):
    1. applePie = ["apple", "brown sugar", "butter", "pie crust"];
    which defines applePie as a String[] array.

    Actually they don't; "number++" is quite different in usage from "number += 1". Also, it's different from ++number.

    Code (csharp):
    1. number = 1
    2. if (number++ == 1):
    3.     Debug.Log("1")
    4. Debug.Log(number)
    5.  
    6. // compare to
    7.  
    8. number = 1
    9. if (++number == 2):
    10.     Debug.Log("2")
    11. Debug.Log(number)
    --Eric
     
  5. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Whoa, what?

    Edit:

    Wow, you learn something new every day...
     
    Last edited: Jun 23, 2011
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ++number increments then returns the number, whereas number++ returns the number then increments. Sometimes ++number is more convenient, in other cases number++ is better; depends on what you're doing.

    --Eric
     
  7. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
    It was kind of late at night when I did some of this, so that explains the part about having braces and brackets and semicolons. Also I meant to replace the sayHello = "hello" thing to the box example you used. I thought I changed that, but I guess I overlooked it. I have never used JS before, so I kind of had to learn it as I go. Thanks for all of the corrections and help.

    @NPSF I know that lists in Python are most similar to arraylists in Java, so I would assume that a list in boo is comparable to an arraylist in a different language as well. In other words, it's an arraylist.

    @Eric Well that's nice to know. I just copied and pasted that section without checking the facts first. Silly me :p. I also know that you can use semicolons in Boo/Python, but I just don't see the point. I'm not implying that you said there is a point, just letting you know why I said that. I will word it differently though.
    I didn't know that there was implicit typing in C# though. I realize how many mistakes there are, I'm glad there are you guys here.
     
    Last edited: Jun 24, 2011
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not really mistakes, just a few inaccuracies; generally that was well done.

    --Eric