Unit 1 · Lesson 3 Beginner ⏱ ~40 minutes

Writing Your First Scripts

So far you've placed controls and navigated between cards. Now it's time to make things actually happen. In this lesson you'll learn how HyperXTalk's messaging system works, how to write handlers, how to use variables, and how to find and fix mistakes using the Message Box and Debugger.

🎯 Learning Objectives
  • Understand how HyperXTalk's message-passing system works
  • Write handlers that respond to user actions
  • Use local and global variables to store data
  • Use the Message Box to test commands interactively
  • Use the Debugger to step through a script line by line

📨 Handlers and Messages

Everything that happens in HyperXTalk starts with a message. When you click a button, HyperXTalk sends a mouseUp message. When a card opens, it sends an openCard message. When a key is pressed, it sends a keyDown message.

A handler is a block of script that responds to a message. It starts with on messageName and ends with end messageName. Everything in between is the code that runs when that message arrives:

button "Submit"
on mouseUp -- This runs when the button is clicked answer "You clicked Submit!" end mouseUp

Common Messages

mouseUp — fired when the mouse button is released over a control. The most common button handler.

mouseDown — fired the moment the mouse button is pressed down.

mouseEnter — fired when the mouse moves over a control.

mouseLeave — fired when the mouse moves away from a control.

openCard — fired when a card is navigated to. Useful for initialising a card's content.

openStack — fired when the stack is first opened.

keyDown — fired when a key is pressed. The key that was pressed is passed as a parameter.

Multiple Handlers in One Script

A control can have many handlers in its script — one for each message it wants to respond to. They sit one after another in the Script Editor:

Script Editor showing multiple handlers in the handler list
Multiple handlers listed on the left panel — click to enlarge

The left panel of the Script Editor lists all the handlers in your script. Click any handler name to jump straight to it — very useful when scripts get longer.

Sending Your Own Messages

You can also send messages yourself using send. This lets one handler trigger another, or call custom handlers you've written:

-- Call a custom handler on mouseUp send "resetForm" to this card end mouseUp -- The custom handler on the card on resetForm set the text of field "Name" to empty set the text of field "Email" to empty end resetForm
🟢 Breaking your code into small, named handlers like resetForm makes it much easier to read, test, and reuse. This is one of the most important habits to develop as a scripter.

✏️ The Script Editor

To open the Script Editor for any control, right-click it in Edit mode and choose Edit Script.

The Script Editor showing a mouseUp handler
The Script Editor — handler list on the left, code on the right — click to enlarge

The Script Editor has two panels. The left panel lists all the handlers in the script and lets you add new ones. The right panel is where you write and edit your code. When you're done, click Apply to save your changes.

💡 Always click Apply after editing a script — your changes won't take effect until you do. The title bar shows "editing" while you have unsaved changes.

📦 Variables

A variable is a named container for storing a value. In HyperXTalk you don't need to declare variables before using them — just assign a value and start using it:

on mouseUp -- Assign a value to a variable put "Emily-Elizabeth" into tName put 42 into tAge -- Use the variables answer tName & " is " & tAge & " years old." end mouseUp

Local Variables

By default, variables in HyperXTalk are local — they only exist while the handler is running. When the handler finishes, the variable and its value disappear. Local variable names conventionally start with t (for "temporary"), like tName or tCount.

Global Variables

Global variables persist for the lifetime of the running stack, and can be read and written by any handler in any script. You must declare a global variable before using it in each handler with the global keyword:

-- On card 1 — store the player's name on mouseUp global gPlayerName ask "What is your name?" put it into gPlayerName go to the next card end mouseUp -- On card 2 — use the player's name on openCard global gPlayerName set the text of field "Welcome" \ to "Hello, " & gPlayerName & "!" end openCard
🟢 A common naming convention: local variables start with t (e.g. tResult), global variables start with g (e.g. gScore). This makes it immediately obvious which type of variable you're looking at.

💬 The Message Box

The Message Box is one of HyperXTalk's most useful tools. It lets you type and run any HyperXTalk command instantly, without writing a full handler. Open it from the toolbar or by pressing Cmd+M (macOS) or Ctrl+M (Windows/Linux).

The Message Box open and ready for input
The Message Box — type any command and press Enter — click to enlarge

Some things you can do in the Message Box:

Message Box
-- Check the value of a variable put gScore -- Set a property directly set the text of field "Status" to "Ready" -- Navigate to a card go to card 2 -- Do a quick calculation put 100 * 3.14159

The result of a put command appears in the Message Box itself. This makes it an excellent scratchpad for testing ideas before adding them to a real handler.

🐛 Debugging

When a script doesn't work as expected, the Debugger helps you find out why. It lets you pause execution and step through your code one line at a time, watching exactly what happens at each step.

To start debugging, click the play button in the Script Editor toolbar while the script is open. The Debugger will pause on the first line and highlight it:

The Debugger paused on line 2 with the current line highlighted
The Debugger paused on line 2 — current line highlighted — click to enlarge

The Debugger toolbar buttons let you:

Step Over — run the current line and pause on the next one.

Step Into — if the current line calls a handler, step inside it.

Step Out — finish the current handler and return to the caller.

Run — continue running until the script finishes or hits a breakpoint.

Stop — halt execution immediately.

💡 You can also use the Message Box while paused in the Debugger to inspect variable values — just type put tMyVariable and the value will appear. This is one of the most powerful debugging techniques in HyperXTalk.
🛠️ Exercise — A Simple Score Tracker

In this exercise you'll build a simple score tracker that uses a global variable to keep score across button clicks. Click any thumbnail to enlarge it.

  1. Create a new stack. Add a Label with the text Score Tracker, a Field named ScoreDisplay with Lock text enabled, and two buttons: one named AddPoint labelled + Point and one named ResetScore labelled Reset.
  2. Right-click the + Point button and edit its script. Add the following handler:
    button "AddPoint"
    on mouseUp global gScore add 1 to gScore set the text of field "ScoreDisplay" \ to "Score: " & gScore end mouseUp
    Script editor
    Script Editor
  3. Edit the Reset button script:
    button "ResetScore"
    on mouseUp global gScore put 0 into gScore set the text of field "ScoreDisplay" \ to "Score: 0" end mouseUp
  4. Switch to Browse mode and test it — click + Point several times and watch the score climb. Click Reset to set it back to zero.
  5. Open the Message Box (Cmd+M / Ctrl+M) and type put gScore to see the current value of the global variable.
    Message Box
    Message Box

Bonus challenge: Add a third button labelled - Point that subtracts 1 from the score but never lets it go below zero. Hint: you'll need an if statement — something to look forward to in Lesson 4!

📝 Review Questions
Question 1
What is the difference between a message and a handler?
A message is an event sent by HyperXTalk when something happens — like a button click (mouseUp) or a card opening (openCard). A handler is a block of script that responds to a specific message, beginning with on messageName and ending with end messageName.
Question 2
What is the difference between a local variable and a global variable?
A local variable only exists while its handler is running — it disappears when the handler ends. A global variable persists for the lifetime of the stack and can be accessed by any handler, but must be declared with the global keyword in each handler that uses it.
Question 3
What command do you use to store a value in a variable?
The put command: put "Hello" into tGreeting. You can also use add, subtract, multiply, and divide for arithmetic directly on variables.
Question 4
What is the Message Box used for?
The Message Box lets you type and run any HyperXTalk command instantly without writing a full handler. It's useful for testing commands, inspecting variable values, and experimenting with HyperXTalk syntax interactively.
Question 5
What does the Debugger's "Step Over" button do?
Step Over runs the currently highlighted line and then pauses on the next line. It lets you move through a script one line at a time to see exactly what each line does, without stepping inside any handlers that are called.