Week 2

If...Then...Else, Text Fields, Variables, Debugging

First, some random thoughts...

Versions - THIS IS IMPORTANT!
Save versions of your movies: e.g. mymovie.1.dir, mymovie.2.dir
If something goes drastically wrong and you can't figure out how to fix it,
you can go back to a previous version that works.
This applies to all work on computers!!!!
Also, back up your work!

Comments
Comment your work.
--Put two dashes before every comment.
It will help your classmates understand your scripts. It will help me understand them. And it will help you understand them when you come back to it after some time away.

Frustration
Frustration is inevitable. Even the best programmers run into trouble.
Some strategies for when frustration strikes: Accept and understand that things will take longer than you think they will. Leave the computer for a while. If you are a pacer, pace. Get some fresh air. Drink some water. Come back to your work with fresh eyes and a fresh perspective.

Get Help
Ask for help - from me, from your classmates, from Director online help.

If...Then...Else

Building on last week...

if logicalExpression then
   statement
else if logicalExpression then
   statement
else if logicalExpression then
   statement
else
   statement
end if


Case is similar (see Director online help)

Variables

What is a variable?
It's a placeholder for a value that you may want to change in your scripts.

Why might you want to use a variable?
- If you are getting input from a user (e.g. from a text field)
- If you think you might want to change some hardcoded value in your movie, it will be much, much easier if you only have to change it once. This is known as a CONSTANT.
- It can help make your scripts more readable and meaningful.
sprite(2).locV = sprite(2).locV + member(6).text
sprite(2).locV = sprite(2).locV + userInputtedSpeed


Anyone remember algebra? x & y are variables - because their values vary.
X + 2y = 5
If x = 0, y = 2.5
If x = 1, y = 2
If x = 2, y = 0.5
And so on...
If there are lots of different values that you might use, it's a good time for a variable.

Kinds of Variables
Variables can hold any type of information:
Numbers
Text Strings
Boolean values (TRUE or FALSE)
Objects (we will talk about objects in a few weeks)

Scope - Global or Local
If you want to use the same variable in more than one handler, you must use a global variable.
Global variables are accessible from anywhere within your Director movie -
but you must declare them - either at the top of your script or in each
and every handler where you want to use them.
Local variables are variables that you use in just one handler and don't need to keep track of anywhere else.

Variable names
Call them whatever you want - but make them meaningful.
The only rule is that they can not be Lingo keywords (which appear colored when you type them in the script window). So you couldn't name a variable "end" or "then" etc.
Some people like to note global variables by putting a g in front of the name, gMyVariable. Some people like to put underscores between words, as in my_variable. And others like to capitalize each word besides the first, as in myVariable. It's a question of personal style.

Initializing variables
It is often a good idea to set the values of your variables before the movie begins so that everything is the way you want it to be when the user starts interacting with the movie. You can do this in a begin sprite handler, like so...

on beginSprite
global myVariable, yourVariable
   myVariable = 1
   yourVariable = "funny"
end


Text Fields

Create them from tool palette.
If you want the user to be able to type in them, make sure to mark them as editable in the property inspector. You can do it with lingo too.
member(whichCastMember).editable
sprite(whichSprite).editable


Get your hands on what the user has typed in through member(CastNum).text
All the data comes in as text. If you want to use a number that you've put into the field, use the lingo command value(), i.e. value(member(6).text)

Debugging

Form a hypothesis. Test it. If it works, the bug is fixed. Otherwise, form another hypothesis

Director's Debugging Tools:
1. The Good Old Message window. Use put statements as we did last week.
2. The Debugger and the Watcher
More sophisticated tools, good in particular for watching a number of variables/values at once.
In the script window, click on the left hand margin where you would like the debugger/watcher to let you know what's going on. That will automatically insert a toggle point and open the Debugger when the script gets to it.

Variable pane - automatically includes all your variables and shows the values
Watcher pane - you can add whatever you want to it by highlighting and clicking the "watch expression" button on top of script window.