TutorialsDijkstra’s Algorithm

5  Find and Pause

Goal

By the end of this chapter, the search runs by itself. Press Find route, and the rings spread across the map while you watch. Press it again, and everything holds still. When the compass stands on the tower, Find route stops, and one more press starts a new search.

The game as it is at the end of this chapter. Click the game to play.

If your game and this chapter part ways, here is the chapter’s finished project. It’s the finished game, the one the Welcome page plays. Open it and compare.

Weighted Maze, Chapter 5 finishedMyth project · 2.0 MB

To open it, start Myth Engine, choose File › Open Project... and pick the downloaded file.

Starting with this chapter? It begins where Chapter 4 ends, and Chapter 4’s Goal has that chapter’s finished project.

5.1 The Idea

In Chapter 4, your thumb was the clock. One trial on the supplied map took 127 presses of Step: 78 to settle the tower, 25 to trace the route home, and 24 to walk the compass there.

Press Find route in the game above. The button reads Pause at once, and the Status line names a new cell about twenty times a second. The thick blue spreads out from the camp. In three or four seconds the tower settles, the yellow line runs home, and the compass sets off. About ten seconds after you pressed, it stands on the tower.

Now press Find route while it runs. Everything holds still, with “Paused.” on the Status line, and the button reads Find route again. Press it once more. The search goes on from the cell where it stopped.

Half of this chapter is a timer that presses Step for you. It’s searchTick, and it rings every 0.045 seconds. The other half is the button, and it’s the harder one. One button sends one message, findRequested. That one message has to start the search, pause it, go on, and after the end, start a new one. How can the same press mean all of that?

Think of a light switch by a door, where every press is the same press. What it does depends on which way the switch is now. Find Route keeps its switch in Modes, the way Step Trial keeps where the trial is. Its one Event for findRequested has three: Start, Pause and Start Over. While the game runs, Actions move the switch from one to the next.

Watch for the capital R. Find Route is the Logic Component that keeps the switch. Find route, with a small r, is the button, and what it does when you press it. The Command on F is named Find Route too.

The search itself doesn’t change at all. Each ring of searchTick sends stepRequested, the message the Step button sends, so Step Trial makes the very same Step. A search run by Find route leaves Maze Cells exactly as the same number of Steps would. Every distance matches, and every parent, and every place on the route. A pause always comes between two Steps, never in the middle of one.

First, though, every ending of a trial needs one door, so Find route can hear about it.

5.2 The Plan

Here’s how the pieces work together. Read each line from left to right. Most of the names are new again, so skim them now, and come back when Find route runs.

  • Every ending -> Step Trial calls End Trial -> the ending’s words, and trialEnded. Then Step Trial switches to Finished.
  • The Find route button, or F -> Request Find sends findRequested -> Find Route’s Start starts the timer, and the button reads Pause.
  • The timer rings, every 0.045 seconds -> stepRequested to Maze Rules -> one Step.
  • A second press -> Find Route’s Pause calls Stop Finding -> the timer stops, and the button reads Find route.
  • Escape, or a Step -> pauseRequested -> Stop Finding.
  • Stop Finding sends findStopped -> Find Route turns Pause off and Start on.
  • End Trial’s trialEnded -> Find Route stops, and turns on Start Over, so the next press starts a new trial.
  • Reset, a painted cell, or Start Over -> trialCleared -> Find Route stops, and turns Start Over off.

5.3 Set Up

This chapter’s Set up card adds two Button Commands. Find Route goes on F, and Pause on Escape. Make them in the Input Manager, as you made Step in Chapter 3, and give each its key.

One key needs care. Escape is also how a row stops waiting for its key. Press it once, and the editor says “Input capture cancelled.” and stops listening. So press Escape twice, quickly. Two in a row give Pause its key, and the row reads Escape.

Set upWhat this chapter adds
COMMANDS (Game Settings > Input Manager)
Find RouteButton
KeybindingsF
PauseButton
KeybindingsEscape

5.4 One Door for Every Ending

A trial can end in four ways, and this part sends every one of them through one Function. The compass reaches the tower, and “Cheapest route.” goes on the Status line. Walls shut the tower off, and “No route.” goes there instead. A link fails its check on the way home, and “Invalid route.” ends the trial. Or the map breaks one of Check Maze Input’s rules, and the Status line names the rule.

Until now, Step Trial ended a trial in five places. Each one wrote its own words on the Status line, then switched to Finished. The cheapest route ends in two of them. On Marker Landed ends it when the compass reaches the tower. Replaying’s Else ends it on the odd map from Chapter 4, whose camp is its tower.

Find route has to hear about every ending. A trial that has ended has nothing left to run. One Function that every ending goes through can send that news, once, for all of them.

5.4.1 End Trial

End Trial takes two Parameters. Its rules is Maze Rules, as Clear Trial’s is. Its ending is the name of a row of Messages: cheapestRoute, noRoute, invalidRoute, or the row of a broken rule.

Its first step writes that row’s words on the Status line. Its second sends trialEnded to Maze Rules alone, with Trigger Custom Event on Object. Nothing listens for it yet. Find Route will.

It doesn’t switch Step Trial to Finished. It can’t, for the reason Clear Trial couldn’t in Chapter 3: only the Logic whose Event it is can change that Event’s Modes. So each ending calls End Trial, and then Step Trial switches itself.

Make it with Add > Function. Set up rules as Clear Trial’s was: an Object Parameter, Nominal, Maze Rules. Add ending as a Text Parameter, whose Default is cheapestRoute, with no quote marks.

FunctionEnd Trial

Ends the trial on rules (Maze Rules). ending is the Messages row that says how it ended (cheapestRoute, noRoute, invalidRoute, or the row of a broken input rule): Status shows its words, and trialEnded tells Find Route on rules that the trial has ended. Step Trial, which calls it, then goes to Finished.

PARAMETERS
NameTypeStarts at
rulesObjectMaze Rules
endingTextcheapestRoute
ACTIONS
1Change Variable
Variable
$status
Value
data('Messages', ending, 'words')
2Trigger Custom Event on Object
Applies To
rules
Custom Event
trialEnded

5.4.2 Step Trial

Now all five endings call End Trial. In each one, the Change Variable of $status goes, and a call to End Trial takes its place, right above the Switch Mode to Finished. That Switch Mode stays. Four of the five sit on Else lines: Ready’s at step 13, Searching’s at step 7, Tracing’s at step 10, and Replaying’s at step 7. The fifth is on the True line of On Marker Landed’s If.

First, click the heading of Step Trial’s Logic Component card, and set Attach to Maze Rules, as you did for Reset Trial. End Trial’s rules takes only Maze Rules. Locked to Maze Rules, Step Trial can hand it self. Then a new End Trial call’s rules box reads self · Maze Rules by itself. There’s nothing to change.

The card strikes Step Trial’s DATA STRUCTURES line. A Logic locked to one Object reads what that Object carries, and Maze Rules carries Step Trial Data already. On the canvas, the Data Structures card still lists it, and every box still reads self.stepTrialData.traceCell. Leave them as they are. It’s the same field, on the same Maze Rules.

For each ending, add the call with the If’s Add inside Else, or Add inside Then for On Marker Landed. Join it above the Switch Mode with the round handles. Then delete the old Change Variable. Actions, one at a time on the Making page shows how the round handles work.

Each call’s Ending box holds the ending’s row. Ready hands it inputCheck.value, the answer of the Check Maze Input call named inputCheck. It’s the row of the rule the map broke. Searching and Tracing write their row’s name, noRoute and invalidRoute. Step Trial has no Parameters, so these boxes take a word as it is, like Choose Brush’s calls in Chapter 2. Type each name bare. The box then draws it in double quotes. It’s the same word.

Logic ComponentStep TrialAttach to: Maze RulesAddDelete

Makes one Step of the trial each time stepRequested arrives (the Step button or the Step Command). What a Step does depends on the trial's phase, the Mode of stepRequested: Ready begins the search and settles the Camp, Searching settles the next cheapest cell, and Finished does nothing. Before the first settle, Ready checks the map and Maze Settings (Check Maze Input); maxCostLimit and unreachedLimit are the largest maxCost and unreached that check allows. Once the Tower is settled, Tracing marks one cell of the Route per Step, from the Tower back to the Camp, checking each link (Check Route Link); traceCell is the cell the next Step marks and routeLength how many cells are marked. Then Replaying moves the Marker one cell of the Route per Step, toward the Tower: it commits the next cell (markerCell) and waits (Waiting For Marker) until Slide Marker says it has landed (markerLanded). On the Tower the Route is complete. Every way the trial ends goes through End Trial, which also tells Find Route. markerOrder is the routeOrder of the Marker's cell. A cleared trial (trialCleared, from Clear Trial) starts again from Ready.

Attach toAny Object
Attach toMaze Rules
DATA STRUCTURES
Step Trial Dataself.stepTrialData
SHARED VARIABLES
NameTypeKindStarting Value
maxCostLimitNumberConstant99
unreachedLimitNumberConstant1000000000
On Step Requested
WhenstepRequestedCustom Event
ModeReady (Starting Mode)
1Check Maze InputFunction
Action Name
inputCheck
maxCostLimit
@self.maxCostLimit
unreachedLimit
@self.unreachedLimit
2IfinputCheck.value == ""
Then
3Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', data('Maze Settings', 'rules', 'startCell'))
Column
distance
Value
0
4Change Component Value
Component Value
self.stepTrialData.traceCell
Value
data('Maze Settings', 'rules', 'goalCell')
5Change Component Value
Component Value
self.stepTrialData.routeLength
Value
0
6Settle Cheapest CellFunction
Action Name
firstSettle
7IffirstSettle.value == data('Maze Settings', 'rules', 'goalCell')
Then
8Switch Mode
Event
On Step Requested
Mode
Tracing
9Otherwise
10Relax NeighborsFunction
cell
firstSettle.value
11Switch Mode
Event
On Step Requested
Mode
Searching
12Trigger Custom Event
Custom Event
searchChanged
13Otherwise
14Change Variable
Variable
$status
Value
data('Messages', inputCheck.value, 'words')
14End TrialFunction
rules
self
ending
inputCheck.value
15Switch Mode
Event
On Step Requested
Mode
Finished
On Step Requested
WhenstepRequestedCustom Event
ModeSearching
1Settle Cheapest CellFunction
Action Name
settle
2Ifsettle.value >= 0
Then
3Ifsettle.value == data('Maze Settings', 'rules', 'goalCell')
Then
4Switch Mode
Event
On Step Requested
Mode
Tracing
5Otherwise
6Relax NeighborsFunction
cell
settle.value
7Otherwise
8Change Variable
Variable
$status
Value
data('Messages', 'noRoute', 'words')
8End TrialFunction
rules
self
ending
noRoute
9Switch Mode
Event
On Step Requested
Mode
Finished
10Trigger Custom Event
Custom Event
searchChanged

The rest of the card starts with Tracing. Its Else at step 10 is the third ending to change, the same way.

The last two endings say “Cheapest route.” That’s End Trial’s Default, and a new call’s Ending box starts at it, "cheapestRoute". So in Replaying’s Else and in On Marker Landed, there’s nothing to type.

Logic ComponentStep TrialcontinuedAddDelete
On Step Requested
WhenstepRequestedCustom Event
ModeTracing
1Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', self.stepTrialData.traceCell)
Column
routeOrder
Value
self.stepTrialData.routeLength
2Change Component Value
Component Value
self.stepTrialData.routeLength
Operator
+=
Value
1
3Ifself.stepTrialData.traceCell == data('Maze Settings', 'rules', 'startCell')
Then
4Change Variable
Variable
$status
Value
data('Messages', 'followRoute', 'words')
5Switch Mode
Event
On Step Requested
Mode
Replaying
6Otherwise
7Check Route LinkFunction
Action Name
link
cell
self.stepTrialData.traceCell
8Iflink.value == 1 && self.stepTrialData.routeLength < dataCount('Maze Cells')
Then
9Change Component Value
Component Value
self.stepTrialData.traceCell
Value
data('Maze Cells', self.stepTrialData.traceCell, 'parent')
10Otherwise
11Change Variable
Variable
$status
Value
data('Messages', 'invalidRoute', 'words')
11End TrialFunction
rules
self
ending
invalidRoute
12Switch Mode
Event
On Step Requested
Mode
Finished
13Trigger Custom Event
Custom Event
searchChanged
On Step Requested
WhenstepRequestedCustom Event
ModeReplaying
1Declare Variable
Name
markerOrder
Value
-1
2Change Variable
Variable
markerOrder
Value
data('Maze Cells', $markerCell, 'routeOrder')
3IfmarkerOrder > 0
Then
4Change Variable
Variable
$markerCell
Value
dataFind('Maze Cells', 'routeOrder', markerOrder - 1)
5Switch Mode
Event
On Step Requested
Mode
Waiting For Marker
6Trigger Custom Event
Custom Event
markerMoved
7Otherwise
8Change Variable
Variable
$status
Value
data('Messages', 'cheapestRoute', 'words')
8End TrialFunction
rules
self
ending
cheapestRoute
9Switch Mode
Event
On Step Requested
Mode
Finished
On Step Requested
WhenstepRequestedCustom Event
ModeWaiting For Marker
(no Actions)
On Step Requested
WhenstepRequestedCustom Event
ModeFinished
(no Actions)
On Marker Landed
WhenmarkerLandedCustom Event
1If$markerCell == data('Maze Settings', 'rules', 'goalCell')
Then
2Change Variable
Variable
$status
Value
data('Messages', 'cheapestRoute', 'words')
2End TrialFunction
rules
self
ending
cheapestRoute
3Switch Mode
Event
On Step Requested
Mode
Finished
4Otherwise
5Switch Mode
Event
On Step Requested
Mode
Replaying
On Trial Cleared
WhentrialClearedCustom Event
1Reset Modes
Event
On Step Requested

5.4.3 Clear Trial

Clear Trial already sends trialCleared to Maze Rules, as its last step, since Chapter 3. Step Trial hears it and goes back to Ready. Soon Find Route will hear it too. That’s the only change, and it’s in the card’s Description, which now names them both. A Reset sends it, and so does a painted cell, and so does the Scene starting.

FunctionClear TrialAddDelete

Puts the trial back to Ready, keeping the terrain: every cell of Maze Cells gets distance unreached (Maze Settings), no parent (-1) and settled 0 and no place on the Route (routeOrder -1), Visited goes back to 0, no cell is current (currentCell -1), the Marker's cell is the Camp (markerCell), Status asks the learner to paint terrain (the Messages ready words), and searchChanged clears every Search Outline. Last, trialCleared tells Step Trial on rules (Maze Rules) that the trial was cleared, so the next Step starts from Ready, and Find Route, so Find route stops.

PARAMETERS
NameTypeStarts at
rulesObjectMaze Rules
ACTIONS
1RepeatdataCount('Maze Cells')times
Each time
2Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', local.index)
Column
"distance"
Value
data('Maze Settings', 'rules', 'unreached')
3Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', local.index)
Column
"parent"
Value
-1
4Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', local.index)
Column
"settled"
Value
0
5Set Data Cell
Table
"Maze Cells"
Row
dataRow('Maze Cells', local.index)
Column
"routeOrder"
Value
-1
6Change Variable
Variable
$visited
Value
0
7Change Variable
Variable
$currentCell
Value
-1
8Change Variable
Variable
$status
Value
data('Messages', 'ready', 'words')
9Change Variable
Variable
$markerCell
Value
data('Maze Settings', 'rules', 'startCell')
10Trigger Custom Event
Custom Event
searchChanged
11Trigger Custom Event on Object
Applies To
rules
Custom Event
trialCleared

Run the game, and play a whole trial with Step. Nothing looks new. That’s the point. The compass reaches the tower, and the Status line says “Cheapest route.” as before. Now wall the camp in, on its right and below it, and press Step twice. The second Step puts “No route.” on the Status line. Press D to bring the supplied map back. Every ending still says its own words, and now each one also tells Maze Rules that the trial is over.

5.5 Find Route

Now the button that does the pressing.

Maze Settings gets tickSeconds, the time between two Steps of Find route: 0.045 seconds, about twenty Steps every second. Add it to Maze Settings Entry with Add Field, Value 0.045. Add Field puts it last, and the card lists it last too. Type 0.045 into the rules row as well, because a new field gives that row 0.

Messages gets four rows: searching and paused, for the Status line, and findCaption and pauseCaption, the button’s own words. Add each with Add Row.

Data StructureMaze Settings EntryAddDelete

One row of the "Maze Settings" Data Table.

columnsNumberdefault 12
rowsNumberdefault 10
startCellNumberdefault 0
goalCellNumberdefault 119
maxCostNumberdefault 99
unreachedNumberdefault 1000000
tickSecondsNumberdefault 0.045
Data TableMaze SettingsData Structure: Maze Settings EntryAddDelete
Row 0rules
columns12
rows10
startCell0
goalCell119
maxCost99
unreached1000000
tickSeconds0.045
Data TableMessagesData Structure: Messages EntryAddDelete
Row 0ready
wordsPaint terrain.
Row 1cellSettled
words: cost
Row 2noRoute
wordsNo route.
Row 3wrongMapSize
wordsFix columns and rows.
Row 4wrongSettings
wordsCheck search settings.
Row 5wrongEndpoints
wordsCheck start and goal.
Row 6wrongCost
wordsUse costs 0 to maxCost.
Row 7blockedEndpoint
wordsUnblock S and G.
Row 8buildingRoute
wordsBuilding route...
Row 9followRoute
wordsFollow the route.
Row 10invalidRoute
wordsInvalid route.
Row 11noCost
words--
Row 12cheapestRoute
wordsCheapest route.
Row 13searching
wordsSearching...
Row 14paused
wordsPaused.
Row 15findCaption
wordsFind route
Row 16pauseCaption
wordsPause

5.5.1 Find Route

Find Route, on Maze Rules, runs the search by itself. Here is the chapter’s one hard idea. Take it slowly, because one Event answers the same press in three ways.

Its Event for findRequested has three Modes: Start Over, Start and Pause. It starts at Start. Count the presses.

Press Find route, and Start writes the searching words on the Status line, starts the searchTick timer, and sends findStarted. So the button reads Pause. Last, Switch Mode turns Pause on, and the others off. So the next press means stop.

While the timer runs, each ring sends stepRequested to Maze Rules, and Step Trial makes one Step. The first ring comes at once. So the searching words last only until a Step writes its own.

Press it again. Pause runs. It calls Stop Finding, which stops the timer and sends findStopped. Find Route hears that in an Event of its own, which turns Pause off and Start on. After that, Pause writes the paused words. So the next press means go, and the search goes on from where it stopped. Nothing was cleared.

Now the end. When a trial ends, End Trial sends trialEnded, and Find Route hears it and calls Stop Finding. Next, Turn On Mode turns Start Over on, beside Start. That switches the Event to Combined, so two Modes now answer the next press, one after the other, in their order. There’s nothing to set for that. Leave Modes at One at a time. Turn On Mode is what lets a second one answer, while the game runs.

Press Find route after the end. Start Over goes first, because it stands before Start. It calls Clear Trial, which clears the trial and sends trialCleared, as a Reset would. Find Route hears that, and Turn Off Mode turns Start Over off again. After that, Start runs, on a clean trial. One press, and a new search begins on the same map. That’s why the card lists Start Over first: the order of the Modes is the order they run in.

Build it in this order. Make Find Route, and set Attach to Maze Rules, as for Step Trial. Add its first Event, for findRequested, with Add Custom Event…, since nothing sends it yet. Rename its one Mode Start. Add Start Over and Pause with New Mode…, and put Start Over before Start with Move Left. Starting Mode should read Start. Modes on the Making page has each move.

Its Actions call Stop Finding, the Function that comes next. So leave them for a moment. Find Route’s card comes after Stop Finding’s, with them.

5.5.2 Stop Finding

Stop Finding stops Find route, in one place, for every Event that needs it. Its first step stops the searchTick timer on rules. Its second sends findStopped. That’s for the button’s words, and for Find Route’s switch.

Why not move the switch right here? It can’t, any more than End Trial can switch Step Trial. Those Modes belong to Find Route’s Event. So Stop Finding sends word, and Find Route does the rest. Turn Off Mode and Turn On Mode change Pause and Start, and nothing else. So a Step or an Escape after the end, which calls Stop Finding too, leaves Start Over on. The next press still starts over. Stopping twice changes nothing.

Its rules is an Object Parameter, Nominal, Maze Rules, as End Trial’s is. Now look at the timer’s name in step 1, because it isn’t written the way Find Route writes it. Here it’s "searchTick", with quote marks. In Find Route’s Start, it’s bare. A Function reads that box as a formula, so a word there needs its quotes. The card already shows each one the way its box takes it.

FunctionStop Finding

Stops Find route on rules (Maze Rules): its searchTick timer stops, and findStopped tells the Find route button and Find Route, so the next findRequested starts Find route again. Stopping a Find route that is not running changes nothing. The trial is kept as it is.

PARAMETERS
NameTypeStarts at
rulesObjectMaze Rules
ACTIONS
1Stop Recurring Timer
Applies To
rules
Timer Name
"searchTick"
2Trigger Custom Event
Custom Event
findStopped

Now give Find Route its Actions. Start’s timer is named searchTick, and its Repeat Every (s) reads tickSeconds from Maze Settings. Ring Once stays as it starts, at No. So the timer rings again and again, until Stop Finding stops it.

The timer’s own Event is a Recurring Timer named searchTick, made with Type a timer name…, as markerSlide was in Chapter 4. Its one Action is Trigger Custom Event on Object, and its Applies To stays at Self. So stepRequested goes to Maze Rules alone.

Four more Events listen for messages, and three of them call Stop Finding. The messages trialCleared, trialEnded and findStopped are in the Add Event window already, because Clear Trial, End Trial and Stop Finding send them. But pauseRequested needs Add Custom Event…, since nothing sends it yet. On Trial Cleared also turns Start Over off, and On Trial Ended turns it on. On Find Stopped calls nothing. It only moves the switch back to Start, ready for the next press.

Logic ComponentFind RouteAttach to: Maze Rules

Runs the trial by itself: each findRequested (the Find route button or the Find Route Command) starts Find route or pauses it, by the Mode of findRequested. While it runs, the searchTick timer sends stepRequested every Maze Settings tickSeconds, the same Step the Step button asks for, and the button reads Pause (findStarted). A cleared trial (trialCleared) stops it (Stop Finding), and so does pauseRequested (Escape, or a Step). A trial that has ended (trialEnded) stops it too, and the next findRequested then starts over: Start Over clears the trial before Start runs Find route on the new one. Whenever it stops (findStopped), Pause is taken out and Start put in.

Attach toMaze Rules
On Find Requested
WhenfindRequestedCustom Event
ModeStart Over
1Clear TrialFunction
rules
self
On Find Requested
WhenfindRequestedCustom Event
ModeStart (Starting Mode)
1Change Variable
Variable
$status
Value
data('Messages', 'searching', 'words')
2Start Recurring Timer
Timer Name
searchTick
Repeat Every (s)
data('Maze Settings', 'rules', 'tickSeconds')
3Trigger Custom Event
Custom Event
findStarted
4Switch Mode
Event
On Find Requested
Mode
Pause
On Find Requested
WhenfindRequestedCustom Event
ModePause
1Stop FindingFunction
rules
self
2Change Variable
Variable
$status
Value
data('Messages', 'paused', 'words')
On the Search Tick Timer
WhenRecurring TimerTimer name: searchTick
1Trigger Custom Event on Object
Custom Event
stepRequested
On Trial Cleared
WhentrialClearedCustom Event
1Stop FindingFunction
rules
self
2Turn Off Mode
Event
On Find Requested
Mode
Start Over
On Pause Requested
WhenpauseRequestedCustom Event
1Stop FindingFunction
rules
self
On Trial Ended
WhentrialEndedCustom Event
1Stop FindingFunction
rules
self
2Turn On Mode
Event
On Find Requested
Mode
Start Over
On Find Stopped
WhenfindStoppedCustom Event
1Turn Off Mode
Event
On Find Requested
Mode
Pause
2Turn On Mode
Event
On Find Requested
Mode
Start

Attach Find Route to Maze Rules. Maze Rules’ card also strikes its From Logic line, because Step Trial now reads Step Trial Data as Maze Rules’ own. Yours may still say From Logic: Step Trial. Leave it.

ObjectMaze RulesAddDelete
Data Structure
From LogicStep Trial
Data StructureStep Trial Data
Logic
Change Terrain
Restore Defaults
Step Trial
Reset Trial
Find Route

5.5.3 Request Find

The Find route button only asks. Request Find sends findRequested when the button is clicked or F is pressed. It has three Events, each with a line of its own: UI Activated, and a Command Pressed for each Command.

The third is for the Pause Command, on Escape, and it sends pauseRequested instead. Find Route hears that in an Event of its own, which calls Stop Finding and nothing more. So Escape can stop the search, but never start it. It never reaches the Pause Mode either, the only one that writes “Paused.” on the Status line. That’s why Escape leaves the last cell’s words there.

Logic ComponentRequest FindAttach to: Find Route Button

Lets the learner start or pause Find route with a click or a touch on the Find route button, or with the Find Route Command (F in the Input Manager): each sends findRequested, which Find Route on Maze Rules answers. The Pause Command (Escape) only pauses: it sends pauseRequested.

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
Custom Event
findRequested
When Find Route Is Pressed
WhenCommand PressedCommand: Find Route
1Trigger Custom Event
Custom Event
findRequested
When Pause Is Pressed
WhenCommand PressedCommand: Pause
1Trigger Custom Event
Custom Event
pauseRequested

5.5.4 Show Find Caption

Show Find Caption keeps the button’s words true. It never watches the button. It listens for the timer starting and stopping: findStarted from Start, and findStopped from Stop Finding. Both are in the Add Event window already. Start and Stop Finding send them.

Every way of stopping goes through Stop Finding: a second press, Escape, Reset, a painted cell, the end. Soon a Step will too. So the button never says Pause while nothing runs. Its two Change Component Values write self.text.text, so its Components card needs the Text / UI, self.text, as Show Route Cost’s did in Chapter 4.

Logic ComponentShow Find CaptionAttach to: Find Route Button

The words on the Find route button: the Messages pauseCaption words ("Pause") while Find route runs (findStarted), and the findCaption words ("Find route") when it stops (findStopped).

Attach toAny Object
COMPONENTS
self.textText / UI
On Find Started
WhenfindStartedCustom Event
1Change Component Value
Component Value
self.text.text
Value
data('Messages', 'pauseCaption', 'words')
On Find Stopped
WhenfindStoppedCustom Event
1Change Component Value
Component Value
self.text.text
Value
data('Messages', 'findCaption', 'words')

Attach both to the Find Route Button. It has kept its UI Interaction since Chapter 1, waiting for this.

ObjectFind Route ButtonAddDelete
Transform
Presentation > SpaceWindow / UI
Presentation > Render LayerUI
Bounding Box > SizeX 146, Y 48
Text / UI
TextFind route
Font Size18
EmphasisBold
Color#203F43
AlignmentCenter
RenderingVector
Sprite
Z Index Within Object-1
SpritePanel
Transform > SizeX 146, Y 48
Tint (on its placement)#D6B06B
UI Interaction(as it is added)
Logic
Request Find
Show Find Caption

Run the game, and press Find route. The button reads Pause, and the rings spread from the camp. Press it again, and everything holds still, with “Paused.” on the Status line. Once more, and the search goes on. Let it run to the end, and the compass walks to the tower. The button reads Find route again. Press it, and a new search begins on the same map.

5.6 A Step Pauses First

One more press needs a thought: a Step while Find route runs. Until now, Step sent stepRequested alone. During Find route, that Step would land between two of the timer’s, and the search would race on. So from now on, a Step pauses Find route first.

5.6.1 Ask For One Step

Ask For One Step sends pauseRequested, then stepRequested. A message is answered the moment it’s sent, before the next Action runs. So by the time your Step arrives, Stop Finding has stopped the timer. Your Step is the only one that runs. This Function has no Parameters, and its card has two Actions.

Its card calls one Step a transition. That’s the game’s own word for one change in the trial: a cell settled, a cell of the route traced, or a move of the compass.

FunctionAsk For One Step

Asks Maze Rules for one Step: first pauseRequested, so Find Route stops Find route if it is running, then stepRequested, which Step Trial answers with one transition. Both are heard at once, in the order they are sent, so the Step is the only transition the learner asked for.

ACTIONS
1Trigger Custom Event
Custom Event
pauseRequested
2Trigger Custom Event
Custom Event
stepRequested

5.6.2 Request Step

Both of Request Step’s Events call it now, so that order is written in one place. In each Event, take the old Trigger Custom Event out with Delete, and add the call in its place.

Logic ComponentRequest StepAttach to: Step ButtonAddDelete

Lets the learner ask for one Step with a click or a touch on the Step button, or with the Step Command (Space in the Input Manager): each sends stepRequested, which Step Trial on Maze Rules answers. Each first sends pauseRequested, so a Step while Find route runs pauses it before the Step (the Function Ask For One Step).

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
Custom Event
stepRequested
1Ask For One StepFunction
When Step Is Pressed
WhenCommand PressedCommand: Step
1Trigger Custom Event
Custom Event
stepRequested
1Ask For One StepFunction

Run the game. Press Find route, then Step. The button reads Find route at once, and the search stands still after that one Step. Press Find route again, then Escape. It stops the same way. The Status line keeps the last cell’s words, because Escape never reaches the Pause Mode.

Play it!

Watch for each of these.

  • Before you press anything, “Paint terrain.” is on the Status line, and the button reads Find route.
  • Press Find route. The button reads Pause at once. The Status line names a new cell about twenty times a second, and Visited climbs with it.
  • Press it again. The Status line says “Paused.” and the button reads Find route. Watch Visited for a moment. It doesn’t move.
  • Press Find route once more, and Visited climbs on from the same number.
  • Press Escape while it runs. It stops, the button reads Find route, and the Status line keeps the last cell’s words.
  • Press Find route, then Space. The button reads Find route at once, and the search stands still after that one Step.
  • Press R, then Find route, and let it run. The tower settles in three or four seconds, and Cost says 24. The yellow line runs home after it, and the compass sets off along it. About ten seconds after your press, it stands on the tower, with “Cheapest route.” on the Status line and Visited at 78.
  • The button reads Find route again. Press it. The rings clear, and a new search starts on the same map.
  • Press Find route while the compass slides. It finishes its slide and comes to rest on that cell’s lower-right corner, where it always stands. A pause never leaves it halfway between two cells.
  • Paint a wall on the yellow road in the sixth row, on its fourth cell. Press Find route. The search runs from the camp again, the route goes round your wall, and Cost says 25.
  • Press D, then wall the camp in, on its right and below it. Press Find route, and “No route.” comes almost at once. Press it again, and it tries again, with the same answer.
  • Last, run a whole trial with Find route, and another with Step alone. The route is the same, 24 yellow cells after the camp, because the Steps are the same.
How it works

Find route is a timer that presses Step for you. Its button is a switch. Start sets the timer going, and Pause stops it. After the end, Start Over joins Start, and both answer the next press, so one press clears the trial and starts a new search. Switch Mode, Turn On Mode and Turn Off Mode move the switch while the game runs. Every ending goes through End Trial, so Find route always hears when a trial is over.

Underneath it is one idea. Find route doesn’t search any differently from you. Each ring of its timer sends the same stepRequested the Step button sends. So the records it leaves are the ones your own presses would leave, Step for Step. A pause never cuts a Step in half. It only waits between two of them.

Did you know?

Each Step of the search, Settle Cheapest Cell asks Choose Cheapest Cell for the cheapest frontier cell. That’s a cell the search has reached but not settled. Choose Cheapest Cell reads all 120 rows of Maze Cells to find it, the way a finger runs down a price list. On a much larger map, a priority queue would find that cell faster. It keeps the frontier so that its cheapest cell is always ready to take, with no list to read down. This game keeps the scan, because you can watch it.

5.7 If It Didn’t Work

Something may be off the first time, and that’s all right. Three things change in this chapter: every ending goes through End Trial, Find route runs and stops, and a Step pauses first. So first, see where your game parts ways with the chapter.

Every trial ends with “Cheapest route.” on the Status line, even with the camp walled in? Look at Searching’s End Trial call, at step 8. Its Ending box should read noRoute. A new call starts at cheapestRoute, and perhaps this one kept it.

End Trial’s calls in Step Trial don’t read self · Maze Rules in their rules box? Perhaps Step Trial still says Any Object. Click the heading of its Logic Component card, and set Attach to Maze Rules.

Find route does nothing at all? Look for Request Find on the Find Route Button, and Find Route on Maze Rules. After that, look at the name findRequested in both.

The button reads Pause, but nothing moves on the map? Look at Find Route’s timer Event. It should name searchTick, the same timer that Start starts. Look at its Trigger Custom Event on Object too, which sends stepRequested.

The second press doesn’t pause? Look at Start’s step 4. Its Switch Mode should turn On Find Requested to Pause.

The first press pauses, but every press after it only says “Paused.” again? Look at Find Route’s On Find Stopped. It should turn Pause off, then Start on, so the next press means go.

The button keeps saying Find route while the search runs? Look for Show Find Caption on the Find Route Button, and at the name findStarted, in Start and in Show Find Caption.

After the trial ends, Find route says “Searching…” and turns the button to Pause, but nothing else happens? Start Over never joined Start, and Step Trial is still Finished. Look at End Trial’s step 2, which sends trialEnded, and at On Trial Ended’s step 2.

After a trial ends, your next press of Find route only clears the search away, and the new one waits for a second press? Start Over stands after Start. Right-click it and choose Move Left. The Modes run in their order, and Start Over has to clear the trial before Start begins.

You press R while Find route runs, and a moment later the search starts again by itself? Look at Clear Trial’s step 11, which sends trialCleared. And look at Find Route’s On Trial Cleared, which hears it.

You press Step while Find route runs, and the search races on regardless? Look at Request Step’s two Events. Each should call Ask For One Step, which sends pauseRequested first.

Escape does nothing? Open the Input Manager, and look at Pause’s row. It should read Escape. A single Escape cancels the capture, so press it twice, quickly.

Each of these is one small fix. Make it, run the game again, and you’re back with the chapter. The catch-up download in the Goal is the finished game, to compare with yours.

Challenge

The game is finished, and it’s yours to change. Slow the search down: set tickSeconds to 0.5, and watch each cell settle, half a second apart. The route at the end is the same. Or hand the game to someone else. Paint them a map, and ask where the route will go before they press Find route.

And then there’s the game you’ll make next. What would you want to find a route through?

Back in Chapter 1, the compass waited at the camp, and nothing on the map answered it. Now paint a map of your own, and press Find route. Every ring, every yellow cell and every slide of the compass on that screen is one you put there.