Difference between revisions of "Guides/Scripting Guide/Tutorials/Hello World"

From Sirikata Wiki
Jump to: navigation, search
(Initial import from sirikata-docs)
(No difference)

Latest revision as of 22:19, 25 May 2012

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.

Print from running object

Presumably, you're connected to a world with already-existing entities connected through presences. Move close towards one (you can use the arrow keys to navigate in flat space and 'q' and 'z' to adjust your height). Click on it. A white cube should appear around the presence's mesh as seen in the following figures:

Before clicking on object

Scripting tutorial helloworld 0.png

After clicking on object

Scripting tutorial helloworld 1.png

This white cube means that the presence is "selected". Type alt+s to bring up a scripting window for the entity connected. Note that although the presence is highlighted, you will be scripting the entity associated with the presence, not the presence itself. The scripting window appears as the white rectangle in the following example image:

Scripting tutorial helloworld 2.png

In the window, type system.print("\n\nHello, World!\n\n");. Hit ctrl+enter to execute this script (or, hit the button marked "run"). You should see "Hello, World!" appear in the scripting window. That should be it. You've created your first program! If you want, you can enter other commands into the scripting window to do basic math, :ref:`movepresence`, print more messages, etc.

Typing in command at prompt.

Scripting tutorial helloworld 3.png

Result of running hello, world command.

Scripting tutorial helloworld 4.png

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 :ref:`createent` 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("nnHello, World!nn");

Let's break it into pieces:

   function printHelloWorld(newPres) {
       system.print("nnHello, World!nn");

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:


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.