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.
How this lesson works: Predict, Try, Investigate, Modify, Make
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
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
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
Start a New project called Water tracker.
Insert a Text object called GlassText with the text Glasses: 0 of 8.
Insert a Button called AddButton with the text Add a glass, and another called ResetButton with the text Reset.
Variables and the display
In the event sheet, add two global variables: Glasses (starting at 0) and Goal (starting at 8).
Add an event: System, Every tick. Add the action GlassText, Set text: "Glasses: "&Glasses&" of "&Goal
The buttons
Add an event: AddButton, On clicked. Add the action System, Add to, Glasses, 1.
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.
Add an event: ResetButton, On clicked, with the action System, Set value, Glasses, 0.
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?
This is the cap you built. Click the line that lets the count pass the goal.
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 textGoal 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
Lesson done. You've finished every step you need.
Now you can
Use Every tick to keep what's on screen up to date.
Say which actions a sub-event's condition controls.
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.
I can use Every tick to keep what's on screen up to date.
I can say which actions a sub-event's condition controls.
I can make a progress bar from a number.
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
Predict: compare with lesson 1, where the screen didn't change without Set text. What's different now?
Build it: everyone clicks 12 times at step 9 before anyone explains.
Investigate: draw the event and its sub-event on the board; circle which actions belong to which.
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
The count shows the word Glasses or the & signs. Ask: Which parts are inside the quote marks? Fix: Only the words to show go in quotes; the variables and & go outside.
The bar doesn't move. Ask: Which event sets its width, and does it use the right sprite? Fix: The Set width action goes in the Every tick event, on BarFill.
After the fix, Add does nothing. Ask: Where is the Add 1 action now? Fix: It should be inside the sub-event, beside Glasses < Goal, not deleted.
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
who sets the text?: Predicting whether the count changes with no Set text in the button's event sets up the move: Every tick keeps the display in step on its own.
Try the finished app: Using the finished tracker, with its event sheet beside it, gives a target for the build and shows Every tick running all the time.
Build it: the water tracker: The build includes the cap exactly as many beginners write it, so the next step can test it and find why it doesn't stop at the goal.
did the cap work?: Tracing the cap and reading the goal message test the two halves of the move: what a sub-event's condition controls, and conditions checked every tick.
fix the cap: Moving the action into the sub-event is the fix, and testing at the goal confirms students now know which actions a condition controls.
a message and a progress bar: The message and progress bar use the every-tick idea twice, plus an Else from last lesson, so the move is used in a new place.