Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Number Wizard Console Boo Translation Notes

What is Boo?
Boo is a programming language that runs on the same platform that C# does. It was created around 2004 by
Rodrigo Barreto de Oliveira to fix some of the perceived issues with existing programming languages on the Common
Language Runtime (thats the underlying infrastructure that runs C#). You can find out much more about the
language by reading the Boo manifesto (PDF).
The Syntax of Boo is directly inspired from the Python programming language, from which it takes the syntactically
significant whitespace. What this means is that indenting has meaning in Boo and is how you define blocks of
code. This has the benefit of making all boo code look similar and forcing programmers to use good indenting habits.
In practice it has the look and feel of python with the majority of the concepts from C#.
You can learn much more about Boo on the project webpage and by reading The Language Guide.

Why Use Boo?


Boo is a very programmer-friendly language. Like Python, it gives you the power to express your programs simply
and in a readable syntax, and mostly gets out of the way. For example, compare the Hello World programs in C#
below:
public class HelloWorld {
public static void Main() {
System.Console.WriteLine("Hello from C#!");
}
}

To a Boo equivalent:
print("Hello from Boo!")

Combine this with complete access to all the same constructs as C# programs, and many useful syntactic features
that make a programmers life easier and its easy to see why Boo might be the language of choice for your next
game.

Translating our first script


In these translation notes and subsequent translation notes, we will concentrate on feature that are unique to boo
and highlight the differences between the scripts that we have not encountered before. Since this is the first time
we have translated to Boo, we will go through the NumberWizard.cs file in some details. Future translation notes
will simply highlight new or interesting features that were introduced by the project instead of going over familiar
ground. You should also take a look at the differences between Boo and C# on The Boo Website. There is also An
Online Translator available that will translate from C# to Boo.
Dont forget that the Entire project file is translated into boo and is attached to this lecture!

General differences
There are some clear changes and equivalences that will be most obvious from first reading Boo code.
The first of these is how type declaration works in Boo. Instead of declaring types by using the Type VariableName
syntax, Boo uses the VariableName as Type keyword. For example:
This does not affect the use of the private, protected, and public keywords, which can still be used when
declaring variables in the same way:

Learn more at www.CompleteUnityDeveloper.com

Number Wizard Console Boo Translation Notes

In addition it can be seen from the above snippets that Boo does not separate statement using semicolons, instead,
new statements are newline delimited. This, besides saving us some typing and reducing visual noise in our code,
also eliminates a very common source of errors.

Importing other modules


Just like in C#, modules we use have to be imported. By default, new Boo scripts will include import UnityEngine
at the top. This will import all Unity specific features that are commonly used, for example, the MonoBehaviour
class. The namespaces and module names are the same in both languages, and are in fact the same underlying code
and libraries.
C#

Boo

using UnityEngine;
using UnityEngine.UI;

import UnityEngine
import UnityEngine.UI

Declaring classes
The syntax in Boo for class declaration directly corresponds to C#, however, the parent class from which our class
is derived is passed in as an argument within parentheses. For example:
C#

Boo

// NumberWizard inherits from Monobehaviour


public class NumberWizard : MonoBehaviour {
// ...
}

# NumberWizard inherits from Monobehaviour


public class NumberWizard(MonoBehaviour):
# ...

In addition, instead of using { and } to define the limits of the classs code, Boo uses a colon after the class declaration
followed by indenting. Anything with the same indent level as the first line after a colon will be considered in the
same block by the Boo parser.
We can also see another difference between the two languages in the snippets above. Boo single line comments will
start with # rather than //.

Declaring Methods
The method syntax for Boo is similar to the class definition syntax. You can find out all the gory details on the Boo
wiki on Github.
In general, Boo methods and functions are defined like this:
access def MethodName(arg1 as Type, arg2 as Type) as ReturnType:
return Something

Key differences to note with C#:


Methods/functions are defined using the def keyword
The return type is optional, declared using the as keyword and comes after the function arguments but before
the colon.
Method/function names use the same convention as in C#, that is capitalised camel-case
The access keywords public, private and protected have the same meaning as in C# and are placed before
def.
Argument types are declared using the as keyword as well.
Learn more at www.CompleteUnityDeveloper.com

Number Wizard Console Boo Translation Notes

Since whitespace and newlines are syntactically significant, statements do not need to be terminated with
semicolons, and the language does away with semicolons altogether.
From the NumberWizard game, we can see all the differences when looking at the NextGuess() function:
C#

Boo

void NextGuess () {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}

def NextGuess():
guess = ((max + min) / 2)
print(('Higher or lower than ' +
guess))
print('Up = higher, down = lower,
return = equal')

And because Boo declares blocks of code using indentation, we do not need a closing brace before the next function,
we can simply de-indent. Putting this together gives us the following NumberWizard.boo file for our game:
import UnityEngine
public class NumberWizards(MonoBehaviour):
max as int
min as int
guess as int
def Start():
StartGame()
def StartGame():
max = 1000
min = 1
guess = 500
max = (max + 1)
print('========================')
print('Welcome to Number Wizard')
print('Pick a number in your head, but don\'t tell me!')
print(('The highest number you can pick is ' + max))
print(('The lowest number you can pick it ' + min))
print(('Is the number higher or lower than ' + guess))
print('Up = higher, down = lower, return = equal')
def Update():
if Input.GetKeyDown(KeyCode.UpArrow):
min = guess
NextGuess()
elif Input.GetKeyDown(KeyCode.DownArrow):
max = guess
NextGuess()
elif Input.GetKeyDown(KeyCode.Return):
print('I won!')
StartGame()
def NextGuess():
guess = ((max + min) / 2)
print(('Higher or lower than ' + guess))
print('Up = higher, down = lower, return = equal')

Learn more at www.CompleteUnityDeveloper.com

Number Wizard Console Boo Translation Notes

Notice also here how space is used to delimit functions from each other. The convention is to leave a blank line
between the end of a function and the next one, while leaving two between classes.
See you in the next translation notes for more Boo features and code :)

Learn more at www.CompleteUnityDeveloper.com

You might also like