Hello World Alice Scripting Style

The simplest way to get started with Alice scripting is to type expressions into the interpreter bar. Alice's interpreter is the Jython implementation of the Python programming language, so any Jython/Python expression is valid here. To begin with the quintessential first example, type:

print "Hello, world!"

into the interpreter bar and press {enter} (or click "Go"). You should see the greeting appear immediately in Alice's text console.

World's Script

While immediate execution is amusing, it is more useful to be able to attach scripts to your Alice worlds. You can attach a script to the world object by right clicking on it in the object tree and choosing edit script. Alice opens a simple text editor window into which you can type your code. This script will be executed when the world runs. Again, type in the simple program:

print "Hello, world!"

but this time place it into world's script space. Click play. The world should begin and the greeting should be displayed in Alice's text console.

Functions, Parameters, And Alice Objects

Alice scripts can also define functions which can accept parameters and interact with the objects in the Alice world. The following script defines a new function that accepts someone's name as a string and constructs a greeting to that person appropriate for the current time of day. The resulting greeting is stored as a string in world's string property return:

import time
def greeting (name):
        hour = time.localtime(time.time())[3]
        if hour < 12:
                period = "morning"

        elif hour < 18:
                period = "afternoon"
                period = "night"

        world.return = "Good %s, %s!" % (period , name)
  • Line 1 imports to time module, needed to get the current time from the system
  • Line 2 defines a new function called greeting that accepts a single parameter, name
  • Line 3 retrieves the current hour, a number between 0 and 23, inclusive
  • Lines 4–9 determine the appropriate greeting for the current hour
  • Line 11 assembles the greeting and stores it in world's string property return

Here is an Alice program demonstrating how to use the greeting function:


and the program's output:


Function Return Values

In a normal Python program, the greeting function defined above would return its value to the caller via a return expression. While this is syntactically legal in Alice, I have not been able to get it to work correctly. Every return value I have tried has resulted in a ClassCastException of one type or another. I believe this is a limitation of my understanding rather than the Alice scripting framework and will update this document when I discover how to return values correctly.