← Construct apps

A water tracker

Build a water tracker with a goal: add a glass, reset the day, and see a message and a progress bar. An Every tick event keeps the display in step without setting the text in every button's event. A condition that isn't a trigger, such as Compare variable, is checked every tick, and an action only waits for a sub-event's condition if it sits inside that sub-event.

Most apps show the same numbers in several places. Keeping them all up to date by hand in every button gets messy fast; there's a simpler way.

In the practice tracker, the Save event changed the total and set the text. That works with one button, but with three or four buttons you'd need a Set text in every one, and it's easy to miss one.

This time you'll use Every tick. Construct checks the event sheet over and over, about 60 times a second. An Every tick event runs on each of those checks, so it can keep the screen in step with the variables, whatever changed them.

The water tracker: "Glasses: 5 of 8", a progress bar five-eighths full, Add a glass and Reset buttons, and the message "Keep going".

How this lesson works: Predict, Try, Investigate, Modify, Make
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Who sets the text?

When (condition) Do (actions)
Every tick GlassText: set text to "Glasses: " & Glasses & " of " & Goal
AddButton: on clicked System: add 1 to Glasses
The Add button's event only adds 1 to Glasses. It has no Set text. You click Add. Does the count on the screen change?

TryTry it on the board and compare the result with your prediction.

Try the finished app

This is the finished app you'll build. The Every tick row keeps flashing: it runs all the time, not just when you click.

Try it yourselfYour edits are saved in this browser.

Add glasses until you reach the goal, then try to go past it. Watch the Every tick row on the event sheet.

Use the board. This ticks when you get there.

TryTry it on the board and compare the result with your prediction.

Build it: the water tracker

Make the screen

  1. Start a New project called Water tracker.
  2. Insert a Text object called GlassText with the text Glasses: 0 of 8.
  3. Insert a Button called AddButton with the text Add a glass, and another called ResetButton with the text Reset.

Variables and the display

  1. In the event sheet, add two global variables: Glasses (starting at 0) and Goal (starting at 8).
  2. Add an event: System, Every tick. Add the action GlassText, Set text: "Glasses: " & Glasses & " of " & Goal

The buttons

  1. Add an event: AddButton, On clicked. Add the action System, Add to, Glasses, 1.
  2. Now a cap, so it stops at the goal. Select the AddButton event and press S to add a sub-event. Give it the condition System, Compare variable: Glasses < Goal. Leave the Add 1 action where it is, on the On clicked event.
  3. Add an event: ResetButton, On clicked, with the action System, Set value, Glasses, 0.
  4. Preview. Click Add 12 times. What does the screen say? Then click Reset.

Keep this version: you'll find out what happened in the next step.

InvestigateWork out why it happens, one step at a time.

Did the cap work?

You built the cap as in step 7 and clicked Add 12 times, with a goal of 8. What did the screen say?

This is the cap you built. Click the line that lets the count pass the goal.

Another event: Glasses = Goal → set Message to "Goal reached!". It has no green arrow. When does it run?

ModifyChange the problem on purpose and see what happens.

Fix the cap

Drag the Add 1 action from the On clicked event down onto the sub-event, so it sits beside the Compare variable condition. Then preview and click Add 12 times again.

When (condition) Do (actions)
AddButton: on clicked
↳ System: Glasses < Goal System: add 1 to Glasses

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

A message and a progress bar

  • Message: insert a Text called Message. Add an event System, Compare variable, Glasses = Goal, with the action Message, Set text Goal reached!. Press X to add an Else that sets it to Keep going.
  • Progress bar: insert a Sprite called BarFill: draw a blue rectangle and make it 400 wide. Insert a grey one called BarBack the same size, right-click it and choose to send it to the back, and put it behind BarFill. In the Every tick event add BarFill, Set width: Glasses / Goal * 400
The bar is a sprite 400 pixels wide when full. Glasses is 2 and Goal is 8. What width should the bar be?

Now you can
Next lesson4. A values journalBuild a journal app with three screens: pick a value you showed today, say what you did, then see every entry in a list. A global variable carries your choice from one screen to the next, an array keeps the entries, and a Repeat loop puts them in the list.
For teachers

Students build a water tracker with two global variables (Glasses, Goal), an Every tick event that keeps the display up to date, Add and Reset buttons, and a cap. The cap is first built the way beginners often write it (Add 1 left on the On clicked event, the check in a sub-event), so it fails at the goal; students find why and move the action into the sub-event. The Make step adds a goal message with Else and a progress bar. About 60 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The cap in step 7 is wrong on purpose: Add 1 stays on the parent On clicked event, so it runs on every click and the sub-event's condition (Glasses < Goal) controls nothing. The Investigate step finds it; the Modify step fixes it by dragging the action into the sub-event.

Every tick runs about 60 times a second. Setting text there is fine. Adding to a variable there is not (it would add 60 a second), which is why the exit question asks about it.

These steps were written fresh for this site and have not yet been checked click by click in the current editor. Run through them once before class; Construct updates itself, so a menu name may differ slightly.

Pick any goal and thing to track if water doesn't suit your class (pages read, laps, minutes outside). The app is about counting towards a goal, not health advice.

Running the lesson

  1. Predict: compare with lesson 1, where the screen didn't change without Set text. What's different now?
  2. Build it: everyone clicks 12 times at step 9 before anyone explains.
  3. Investigate: draw the event and its sub-event on the board; circle which actions belong to which.
  4. Make: work out one bar width together before students build it.

The hard part (on purpose)

The deliberate slip in step 7 can feel like a trick. Say up front that the build includes one mistake beginners often make, and the class will find it together.

When students get stuck

Stretch and support

Stretch: Let the user set their own goal with a Text Input and a Set goal button (int() from lesson 1).

Support: Give the finished Every tick event as a table and have students check theirs row by row.

Where it leads

Next: Values journal, with three screens, a choice that carries between them, and a list.

What each step is for