Tutorial: Scripting a seat

Time to complete: 10 minutes approximately
Summary: This tutorial will explain how the avatar controller can be used when script seats.
Required application: VastPark Creator

Getting started:

First off we will need to set up a park with an avatar hooked up to an avatar controller, a physics enabled ground plane and a model chair that will acts as a button. Instruction on scripting these elements have been covered in some of the earlier tutorials. I have attached a imml file with the basic setup so we can move on
to scripting our chair.
Basic setup

Setting up the seat:

The seat we will be creating today consists of two elements. There is the seat model,

in this example an office chair which will also act as a button, and the seat node

which is a position marker that we will use in our scripts.

Create a small box with a size of 0.1,0.1,0.1 and position it at in the center at the

base of the chair on the ground.

Rename the box Office-Chair-Node, its important to use the seats name and add '-Node' to it for scripting.

Begin the script:

The 'chair-MouseClick' script is executed when the chair is clicked on. This is where the bulk of the scripting will be.
Copy the following 3 lines in to the script:

-- Get the chair object
seatelement = scene:getelement(obj.name..'-Node')
avatarcontroller:sit(seatelement)

The first line is simply a comment to make the code a little easier to read, any line with two dashes (--) preceeding it will be ignored when the script is executed.
The second line sets the variable 'seatelement' to the name of the chair that was clicked on and adds -Node to the end of it. This is why its important to name your seat node the same as your chair with -Node on then end. scene:getelement grabs all the information about the element such as position, rotation and scale.
The third line calls the sit function of the avatarcontroller and passes it the seatelement, or name of the seat node where you want the avatar to sit.

Lets run the park and see what happens!

Well the avatar is now seated but is facing in the wrong direction. To fix this all we need to do do is rotate the seat node on the Y axis. I also moved it a little higher as the avatar was sitting a little low.
It should now like this:

Scripting Continued:

As you have probably already noticed once you sit down you are unable to get off the chair. First off we will need to know if you are sitting or standing when you click on the chair, to do this we will set a variable when the park loads.

Create a new script called 'onLoad' then create and set the seated variable to false like so:

scene:set('seated', false)

We then need to create a new trigger with 'On' set to 'Loaded' and 'Do' set to 'onLoad'. Switch back to imml view and cut and paste the onload script and the trigger up the top of the script. (Its doesn't really need to be up the top but its makes sense as this is one of the first things you want to be executed when the script is run.)

Back in the 'chair-MouseClick' script we will check the 'seated' variable before deciding whether we need to make the avatar sit or stand. First we will need to retrive the 'seated' variable. Add this line to the top of the 'chair-MouseClick' script.

scene:get('seated')

Next you will need to replace:

avatarcontroller:sit(seatelement)

With the following code:

-- Check if you need to stand up or sit
if(seated == true) then
avatarcontroller:stand()
scene.ui:writeline('STAND UP')
seated = false
else
avatarcontroller:sit(seatelement)
scene.ui:writeline('SIT DOWN')
seated = true
end
scene:set('seated',seated)

This code first checks if the seated variable is equal to true and if it is tell the avatar to stand up. STAND UP is also printed in debug dialog and seated is now set to false.
If seated is equal to false the code after 'else' is exectuted. This tells the avatar to sit down on the selected chair, prints 'SIT DOWN' in the debug dialog and seats seated to true. After the end of the 'if' statement we set seated to equal seated.
This line is a little confusing. What we are doing is setting the original seated variable to our locally changed seated. It basically is just updating the variable with our changes.
We can now select our seat node and set it's visiblity to false in the properties panel.

You now have a functioning chair. Your park should look something like this

The Extra step

We can add a key press to stand up of clicking on the seat again as this can some times be difficult with smaller chair or stools.

First we will need to create a new script called 'standUp-Script' and add the following lines of code:

scene.ui:writeline('key pressed')
avatarcontroller:stand()
seated = false
scene:set('seated',seated)

This script writes 'key pressed' to the debug dialog, tells the avatar to stand up, changes seated to false and then sets the variable seated to seated, in this case false.
In design view create a new trigger. Select On for 'KeyPress' and 'standUp-Script' for the 'Do'.

Lets run the park.

As you can see the key press trigger fires every you time you press a button and your avatar stands up. We will add some code to only fire when SPACEBAR is hit.
We need to add an 'if' statement to 'standUp-Script' to check if SPACEBAR was pressed. Copy the code below. The new lines have been bolded.

scene.ui:writeline('key pressed')
if (args.data.key == key.Space) then
avatarcontroller:stand()
seated = false
scene:set('seated',seated)
else
end

Now if you run the park again and sit down, the SPACEBAR is the only button that will make the avatar stand up.
Final park

seatNode.jpg - Placing the seat Node (20.2 kB) Tim Glew, 06/02/2010 02:04 pm

sittingWrong.jpg - Avatar sitting the wrong way (19.2 kB) Tim Glew, 06/02/2010 02:04 pm

sittingRight.jpg - Avatar sitting correctly (21.7 kB) Tim Glew, 06/02/2010 02:04 pm

basicSetup.imml - Basic world setup (3.5 kB) Tim Glew, 06/02/2010 02:17 pm

ScriptsForChairs.imml - World with chairs scripted (4.1 kB) Tim Glew, 06/02/2010 02:17 pm

ScriptsForChairsAdv.imml - World with chairs scripted and key press trigger (4.4 kB) Tim Glew, 06/02/2010 02:17 pm