Table Of Contents

Previous topic

Connecting to a Sirikata World

Next topic

Resetting Scripts

This Page

Hello World – Your First Script

Hello World!

Let’s put together a quick hello world script. One can do this in one of two ways: by printing from a running entity’s presence or by creating a new entity, connecting that entity to the world, and then telling that entity to print your message.

We’ll talk about each separately below.

Bring up entity and print when first presence is connected

There are a variety of ways to create new entities in the world. The simplest is to approach an object in the world, and ask it to create a new entity for you. The tutorial on Creating entities explains how a scripter can create a new entity. This subsection focuses on writing a script that a newly created entity would use to print “Hello, World!” after it has connected to a new space.

Conceptually, there are several ways to approach such a task. For instance, one could send a request to connect to the space and frequently check an indicator to see when that request has been accepted, performing some action on acceptance. Such an approach is called “polling”. As an alternate strategy, one could instead request a connection to the space and register a callback with the underlying run-time environment so that the script is notified when the connection request is successful. This is called an “event-based approach”. Emerson, for the most part, encourages event-based scripting, and therefore uses the second strategy.

Consider the simple following script:

function printHelloWorld(newPres)
{
    system.print("\n\nHello, World!\n\n");
}

system.onPresenceConnected(printHelloWorld);

Let’s break it into pieces:

function printHelloWorld(newPres)
{
    system.print("\n\nHello, World!\n\n");
}

As one might guess from the print from running object section of this tutorial, when the above function is called, it simply prints “Hello, World!”. No surprises here. Let’s look at the next bit:

system.onPresenceConnected(printHelloWorld);

The above system.onPresenceConnected function takes in a single argument: a function that specifies what to do when a presence is newly connected. When a new presence is connected, the Emerson run-time automatically executes this function for the scripter.

In general, a scripter might write presence initialization code in such a function, but, for our purposes, printing “Hello, World!” suffices.