- Learn
- Build a Game
- Chess
- 11 Settings and Saving
11 Settings and Saving
By the end of this chapter, a Settings button opens a panel of three choices: the Computer’s Strength, four Board Finishes and three Piece Sets. Each one shows the moment you press it. And the game remembers. Close it, open it again, and your game is waiting where you left it, in the looks you chose.
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. Open it and compare, or start Chapter 12 from it.
To open it, right-click the downloaded file, choose Extract All... and then Extract. Start Myth Engine, choose File › Open Project... and pick the .mythproj file in the new folder. CREDITS.md and the licenses folder beside it come with the game.
11.1 The Idea
Twelve Moves into a good game, you close the window to make dinner. Later you open it again. The Board is new. Every Piece is back on its first Square, and your twelve Moves are gone.
This chapter gives the game a memory. It also gives it new looks. Four Board Finishes: the Walnut you know, a green Tournament, a blue-gray Slate and a gray-and-white Marble. And three Piece Sets: the Carved walnut, an Ivory & gold, and a Sculpted marble. A Settings button opens a cream panel with all of them, and the Computer’s Strength.
Your game already remembers some things. Since Chapter 6, it keeps your Side and the Strength with Save Persistent Value. Those are your choices, kept on this computer. They belong to you, not to one game. The Board Finish and the Piece Set will be kept the same way.
A game in progress needs a bigger memory. Save Game takes a photograph of the whole table: the Board, the Move History, Board State, whose Turn it is, every Object as it is. Load Game sets the table back from the photograph. Each photograph goes in a save slot, a place with a name. This game has one, named auto.
Here’s the hard part. A loaded game doesn’t arrive at once, the moment the game opens. The game starts its own new game first, and the saved one lands a moment later. So the game must take care not to save its new game over the one on its way.
11.2 The Plan
Here’s how the pieces work together. Read each line from left to right, once now for the shape, and again after the panel first opens.
- The Settings button is pressed -> Report Settings sends settingsPressed -> Settings opens the Settings Panel -> choiceChanged -> Show Option marks each chosen button green.
- A Board Finish is pressed -> Report Board Finish sends boardFinishChosen -> Settings keeps it, and sends lookChanged -> the Board, its labels, the Pieces and the Moving Pieces wear the new look.
- A Turn goes on -> gameContinues -> Save The Game saves in the slot auto.
- The game opens -> Continue The Game loads auto -> load:succeeded -> savedGameLoaded, positionChanged and gameContinues, in that order.
11.3 Set Up
One line near the end of this card says REMOVED. This time it means a rename, not a Delete, so read the paragraph after the card before you touch it.
Off (One Mesh per Sprite)ART names five new pictures: three Boards and two Piece Sets. The Starter Kit brought them in Chapter 1, so they’re in your project already.
GAME SETTINGS has one line. In Display’s Rendering group, set Sprite Batching to Off (One Mesh per Sprite). With batching on, Myth may draw many pictures together, to save time. Off, each picture is drawn on its own. Then a new picture shows as soon as Set Sprite gives it, and that’s what a new Board Finish needs.
REMOVED names New Game Option. Don’t delete it this time. The second part of this chapter renames it instead, and the rename keeps everything that uses it. Once it’s renamed, the old name is gone. That’s all REMOVED asks.
The placements come later too, once the Settings panel’s Objects exist. The chapter says when, and sorts the long list for you first.
11.4 The Board’s New Looks
First, the looks. This part changes nothing you can see yet, because Walnut and Carved walnut are what your game already wears.
11.4.1 Board State
What the Game Rules remember about the game besides the Position table. sideToMove: the Side whose Turn it is (White moves first). selected: the Square of the selected Piece (empty when none is selected). lastFrom and lastTo: the two Squares of the last Move (empty before the first Move). checkSquare: the Square of the King in Check (empty when the Side to move is not in Check). enPassantSquare: the Square a Pawn has just passed over with a two-Square Move, where an enemy Pawn may take it en passant on the very next Move (empty otherwise). promotionSquare: while the Promotion choice is open, the Square the selected Pawn will be promoted on (empty otherwise). playerSide: the Side the Player plays (White unless chosen otherwise), so the end of the game can say who won. fiftyMoveCount: the Moves made since the last Capture or Pawn Move, both Sides counted, for the fifty-move rule. isFlipped: whether the Board is drawn turned over, Black at the bottom (false: White at the bottom, a1 at the bottom-left). strength: the Strength the Computer plays at (Gentle, Casual, Club, Strong, or Expert; Casual unless chosen otherwise). chosenSide: while the New Game panel is open, the Side it shows as chosen. chosenStrength: while the New Game or the Settings panel is open, the Strength it shows as chosen. isShowingThreats: whether Threats mode is on (the Threats button; kept on this device). inspected: in Threats mode, the Square of the Piece the Player last clicked to look at it, of either Side (empty when none is inspected). boardFinish: the Board Finish shown, a row of the Board Finishes table (Walnut unless chosen otherwise). pieceSet: the Piece Set shown, a row of the Piece Sets table (Carved walnut unless chosen otherwise).
Board State gets two fields at the end. boardFinish starts as Walnut, and pieceSet as Carved walnut. Each one names a row of a table you make next. Its Description changes a little too, because the Settings panel will use chosenStrength as well. Nothing reads a Description, so retype it only if you’d like.
11.4.2 Board Finishes
One row of the "Board Finishes" Data Table, which follows it. The four looks of the Board the Player can choose (Settings). One row per Board Finish, named by the word the Player reads. board: the Board art. labelOnLight: the color of a Rank number or File letter on a light Square. labelOnDark: the color of one on a dark Square.
Walnut Board#503a2b#dfc69bTournament Board#314b3e#e8ead5Slate Board#344756#dce4e8Marble Board#424840#eae5dbBoard Finishes has one row for each finish. The table makes its Entry. Fill in its fields before you add the table’s rows. Its board column holds the Board’s picture. It’s a Sprite column, so you don’t type in its cells. Click one, and pick the picture from your project’s list. Each picture is the whole Board, eight Squares by eight, so every finish fits the same Board Object with no change of size.
The other two columns are for the Rank and File labels. A label on a light Square needs a dark color, and one on a dark Square a light color. So each finish has two codes, labelOnLight and labelOnDark. They’re Text columns that hold the codes, as the Arrows table held its colors in Chapter 10.
11.4.3 Piece Sets
One row of the "Piece Sets" Data Table, which follows it. The three looks of the Pieces the Player can choose (Settings). One row per Piece Set, named by the words the Player reads. pieces: the Piece Set art, twelve frames in the order of the Kinds table. size: how large the art is drawn, 1 being the size of the Carved walnut art, so that every set's King is 0.84 of a Square tall.
Carved Walnut Pieces1Ivory And Gold Pieces0.9591Sculpted Marble Pieces0.9535Piece Sets has one row for each set. Its columns come from Piece Sets Entry. Its pieces column holds the picture with all twelve Pieces of that set, picked from the list as before. The sets weren’t drawn the same size. So size scales each set to stand as tall as the Carved walnut does. Type each row’s name as the card shows it, small letters and all. Ivory & gold has an ampersand in it.
11.4.4 Show Piece Set
Shows a Piece Set's art on one Object that shows a Piece (a Piece, a Moving Piece, or a Promotion button), at the set's size. Changing the art shows its first frame, so the caller shows the right Piece afterwards. piece: the Object. pieceSet: the Piece Set, a row of the Piece Sets table.
- Applies to
piece- Sprite
data('Piece Sets', pieceSet, 'pieces')
- Applies to
piece- Horizontal Scale
data('Piece Sets', pieceSet, 'size')- Vertical Scale
data('Piece Sets', pieceSet, 'size')
Show Piece Set dresses one Object in a Piece Set. Set Sprite gives it the set’s picture, and Set Scale its size, both read from the set’s row. Its Applies to is piece, the Object it’s handed.
Set Sprite’s Sprite box can hold a picture’s name, as on the Board’s card, or a formula. Here it’s a formula. Type the card’s data(...) into the box, and the row it reads decides the picture.
There’s a small catch. A new picture always starts on its first frame. So whoever calls this Function shows the right Piece afterwards, with its own frame.
11.4.5 Show Board Finish
Shows the Board in the Board Finish chosen (Board State's boardFinish, a row of the Board Finishes table) when the game begins and whenever the choice changes (Settings, on the Game Rules, sends "lookChanged" and is its Other).
Any Object- Sprite
data('Board Finishes', other.boardState.boardFinish, 'board')
Show Board Finish is one step. When lookChanged arrives, the Board takes the picture of the chosen finish. Its other is the sender, the Game Rules, so it can read their Board State.
11.4.6 Board
X 30, Y 30Walnut BoardX 30, Y 30Lay Out The BoardLay Out The Board DataPieceRank LabelFile LabelSquareMove MarkThreat MarkPin MarkAttach Show Board Finish to the Board.
11.4.7 Show Label
Writes the Rank number or the File letter of the Square this label stands on (its Square Info), read from the Position table: shows names the column (rank, or fileLetter). Its color follows the Board Finish chosen (Settings, on the Game Rules, sends "lookChanged"): the Board Finishes table gives one color for a label on a light Square and one for a label on a dark Square (a Square is dark when its File and Rank add up to an even number, as a1). It shows only while its Square is drawn on the edge of the Board where labels go, the left column or the bottom row, whichever way round the Board is drawn (Flip Board, on the Game Rules, its Other, sends "boardFlipped"). edge: the Position table's column that counts from that edge, file for a Rank Label (the left column) and rank for a File Label (the bottom row).
Any Objectself.squareInfoText / UI- Component Value
self.text.text- Value
data('Position', self.squareInfo.name, @self.shows)
- Name
edgeLine- Value
0
- Variable
edgeLine- Value
data('Position', self.squareInfo.name, @self.edge)
other.boardState.isFlipped == true- Variable
edgeLine- Value
9 - edgeLine
edgeLine == 1- Visible
Yes
- Visible
No
- Type
Text- Name
finish
- Name
squareShade- Value
0
- Name
rank- Value
0
- Name
file- Value
0
- Variable
finish- Value
other.boardState.boardFinish
- Variable
file- Value
data('Position', self.squareInfo.name, 'file')
- Variable
rank- Value
data('Position', self.squareInfo.name, 'rank')
- Variable
squareShade- Value
(file + rank) % 2
squareShade == 1- Component Value
self.text.color- Value
data('Board Finishes', finish, 'labelOnLight')
- Component Value
self.text.color- Value
data('Board Finishes', finish, 'labelOnDark')
Show Label used to color its words once, from two Shared Variables of its own. Now the color comes from the chosen finish’s row, and it changes whenever lookChanged arrives.
So the coloring moves. In On Board Laid Out, keep the Change Component Value that writes the label’s words. Delete the rest of that line, including its Declares. Those colors leave Shared Variables too. Delete labelOnLight and labelOnDark there, since the table now holds the colors for every finish.
Add On Look Changed, with its own Declares first. Here is the full card. Steps 6 to 12 are the old coloring work, built in the new Event. Only the two colors are new. They read the table, with finish as the row.
11.4.8 Show Piece
Shows the Piece that stands on this Piece Object's Square: once the Board is laid out (Lay Out The Board sends "boardLaidOut"), and again after every Move (the Game Rules send "positionChanged"). While the Computer's Move slides ("slideStarted"), a Piece on a Square a slide starts from (the Slides table) shows nothing, because a Moving Piece carries it. It shows the Piece in the Piece Set chosen, again whenever the choice changes (Settings sends "lookChanged").
Any Objectself.squareInfo- piece
self
- piece
self
dataFind('Slides', 'from', self.squareInfo.name) != (-1)- Alpha
0
- piece
self- pieceSet
other.boardState.pieceSet
- piece
self
Show Piece needs to know which set to wear, and that’s in the Game Rules’ Board State. The sender of lookChanged is its Other, as it is for Show Label. There’s nothing to add for that name. The new On Look Changed calls Show Piece Set on this Piece. Then Show Piece On Square picks the right frame, and the see-through Alpha on an empty Square.
11.4.9 Show Promotion Choice
Shows this Promotion Button's Kind (its Promotion Choice) in the Piece Set art of the Side to move, after every change (the Game Rules send "positionChanged" and are its Other), so the panel always offers the Pieces of the Side that promotes. It shows the Piece Set chosen, whenever the choice changes (Settings sends "lookChanged").
Any Objectself.promotionChoice- Type
Text- Name
choiceKind
- Type
Text- Name
side
- Variable
side- Value
other.boardState.sideToMove
- Variable
choiceKind- Value
self.promotionChoice.kind
- Frame
data('Kinds', choiceKind, side)
- piece
self- pieceSet
other.boardState.pieceSet
The Promotion buttons wear the set too. One new Event, with one call. Their frame comes on the next positionChanged, as it always has.
11.4.10 Slide Piece
Slides one Piece of the Computer's Move across the Board before the Move is made. When the Game Rules say "slideStarted" (they are its Other), it reads its row of the Slides table: if the row names a Square, it shows the Piece standing there, in the Piece Set chosen, and glides from that Square to the row's other Square in slideSeconds, starting and ending slowly. The Moving Piece that says so (saysWhenLanded) then tells the Game Rules "pieceLanded", and they make the Move; the next "positionChanged" hides it again. While it slides, a Board turned over or back ("boardFlipped") ends the slide at once (Event Logic Land At Once): the Move is made, drawn the new way round; and another Piece Set chosen meanwhile ("lookChanged") shows on it at once (Show The New Piece Set); between slides a turned Board changes nothing here (Stay Hidden). slide: which row of the Slides table it follows (Piece, or Rook for a castling). slideSeconds: how long the slide takes. saysWhenLanded: true for the one Moving Piece that says when the slide is over (a King and its Rook land together). squareMeters, boardLeft, and boardBottom: where the Squares are (as for Place On Board).
Any Objectself.slidePieceData- Type
Text- Name
toSquare
- Type
Text- Name
fromSquare
- Variable
fromSquare- Value
data('Slides', self.slide, 'from')
- Variable
toSquare- Value
data('Slides', self.slide, 'to')
fromSquare != ""- Component Value
self.slidePieceData.isFlipped- Value
other.boardState.isFlipped
- Action Name
startSquare- square
fromSquare- isFlipped
self.slidePieceData.isFlipped- squareMeters
@self.squareMeters- boardLeft
@self.boardLeft- boardBottom
@self.boardBottom
- Action Name
endSquare- square
toSquare- isFlipped
self.slidePieceData.isFlipped- squareMeters
@self.squareMeters- boardLeft
@self.boardLeft- boardBottom
@self.boardBottom
- Component Value
self.slidePieceData.kind- Value
data('Position', fromSquare, 'kind')
- Component Value
self.slidePieceData.side- Value
data('Position', fromSquare, 'side')
- piece
self- pieceSet
other.boardState.pieceSet
- Frame
data('Kinds', self.slidePieceData.kind, self.slidePieceData.side)
- X
startSquare.x- Y
startSquare.y
- Visible
Yes
- Event
On Board Flipped- Mode
Land At Once
- Event
On Look Changed- Mode
Show The New Piece Set
- Value
self.transform.position.x- Target
endSquare.x- Duration
@self.slideSeconds- Ease
Sine in Out
- Value
self.transform.position.y- Target
endSquare.y- Duration
@self.slideSeconds- Ease
Sine in Out
self.saysWhenLanded == true- Timer Name
"landing"- Repeat Every (s)
@self.slideSeconds- Ring Once
Yes
- Custom Event
pieceLanded
- Visible
No
- Timer Name
"landing"
- Event
On Board Flipped- Mode
Stay Hidden
- Event
On Look Changed- Mode
Stay Hidden
self.saysWhenLanded == true- Custom Event
pieceLanded
- piece
self- pieceSet
other.boardState.pieceSet
- Frame
data('Kinds', self.slidePieceData.kind, self.slidePieceData.side)
The Moving Pieces carry the Computer’s Pieces. So they wear the set as well. Step 11 dresses the Moving Piece in the chosen set, right before step 12 picks its frame.
What if you choose a new set while a Piece is gliding? Most of the time nothing listens, so the new On Look Changed has two Modes, as On Board Flipped does. Stay Hidden is the one it starts with. It has no Actions. Show The New Piece Set dresses the Piece again and picks its frame. Step 16 switches to it when a slide starts, and step 4 of On Position Changed switches back.
Make the new Event and its two Modes first. Then the two Switch Modes can pick them from their lists. Step 11 goes before Set Frame. Add the call with step 5’s Add Inside Then, and join it after step 10 with the round handles. Set Frame stays behind it. Step 16 goes in the same way, before the Tween Properties.
Run the game. It looks exactly as it did before this part. That’s right. Board State still says Walnut and Carved walnut, and nothing sends lookChanged yet.
11.5 One Kind of Option for Two Panels
The Settings panel needs option buttons, and Chapter 6 made them. Only one thing ties them to the New Game panel: the name of their Data Structure.
11.5.1 Panel Option
What one option button of a panel stands for. option: on the New Game panel a Side (a row of the Sides table: White or Black) or a Strength; on the Settings panel a Strength (a row of the Strengths table: Gentle, Casual, Club, Strong, or Expert), a Board Finish (a row of the Board Finishes table), or a Piece Set (a row of the Piece Sets table).
Don’t build this one new. Rename the old one. In the Resources, right-click New Game Option and choose Rename “New Game Option”. Type Panel Option, and press Enter. If you’d like, type the card’s Description too.
Everything that uses it follows the new name, so the Option Button carries a Panel Option now. And the seven buttons of the New Game panel keep their White, Black and five Strengths. That’s why the Set up card says REMOVED: the old name is gone.
11.5.3 Use The Last Strength
Takes the Strength kept on this device (the Persistent Value "strength") into Board State, when the game has it (a row of the Strengths table; otherwise Board State's own strength stays). gameRules: the Object that carries Board State.
boardStateData StructureBoard State- Action Name
savedStrength- Key
"strength"- Default
gameRules.boardState.strength
- Action Name
isKnownStrength- Table
Strengths- Row
savedStrength.value
isKnownStrength.value == true- Component Value
gameRules.boardState.strength- Value
savedStrength.value
Use The Last Strength is New Game’s old way of reading the kept Strength, moved into a Function of its own. It loads the Strength kept under 'strength', checks that the Strengths table has that row, and puts it in Board State. Later, a saved game will need the same steps, so they live in one place.
11.5.4 New Game
Starts every game with the Side and the Strength the Player chose last, and lets the Player start a new game. When the game begins (Game Start, before the first Turn), it takes the choices saved on this device (the Strength through Use The Last Strength, which it uses again when a saved game has been brought back, "savedGameLoaded") (Persistent Values), when the game has them (the Sides and Strengths tables; otherwise, and the first time, Board State's own playerSide and strength), and turns the Board over for a Player who plays Black. When the Player presses New Game ("newGamePressed"), it opens the New Game panel, a menu that holds the game while it is open, showing the current choices as chosen and asking "Replace the current game?" when a game is in progress and has Moves. Each Side or Strength pressed ("sideChosen", "strengthChosen"; the button is its Other) becomes the chosen one. Play ("playPressed") saves the choices, forgets the saved game (the slot "auto"), and restarts the game, which starts again from the beginning with them.
Any Objectself.boardState- Action Name
savedSide- Key
"playerSide"- Default
self.boardState.playerSide
- Action Name
savedStrength- Key
"strength"- Default
self.boardState.strength
- Action Name
isKnownSide- Table
Sides- Row
savedSide.value
isKnownSide.value == true- Component Value
self.boardState.playerSide- Value
savedSide.value
- Action Name
isKnownStrength- Table
Strengths- Row
savedStrength.value
isKnownStrength.value == true- Component Value
self.boardState.strength- Value
savedStrength.value
- gameRules
self
self.boardState.playerSide == "Black"- Component Value
self.boardState.isFlipped- Value
true
- Component Value
self.boardState.chosenSide- Value
self.boardState.playerSide
- Component Value
self.boardState.chosenStrength- Value
self.boardState.strength
- Menu Element
New Game Panel
- Menu Element
New Game Panel
- Action Name
isOver- Game State
Game Over
self.boardState.lastFrom != "" && isOver.value == false- Element
Replace Question
- Element
Replace Question- Visible
No
- Custom Event
choiceChanged- Custom Event
newGameChoiceChanged
- Component Value
self.boardState.chosenSide- Value
other.panelOption.option- Value
other.newGameOption.option
- Custom Event
choiceChanged- Custom Event
newGameChoiceChanged
- Component Value
self.boardState.chosenStrength- Value
other.panelOption.option- Value
other.newGameOption.option
- Custom Event
choiceChanged- Custom Event
newGameChoiceChanged
- Action Name
persistentWriteResult- Key
"playerSide"- Value
self.boardState.chosenSide
- Action Name
persistentWriteResult2- Key
"strength"- Value
self.boardState.chosenStrength
- Slot
"auto"
- gameRules
self
New Game changes in four places, so take them one at a time. On its card, a struck step keeps its old number, so a few numbers show twice: once struck, and once on the new step that took its place.
First, the name. The renamed Data Structure now reads as panelOption. In On Side Chosen and On Strength Chosen, change the Value to other.panelOption.option. Every newGameChoiceChanged becomes choiceChanged, the name both panels share.
Second, When the Game Starts. Delete its old step 2, and its old steps 6 to 8, the three that read the Strength. Use The Last Strength replaces them. Add it apart from the line, and join it after that If with the round handles. It sits on the main line. The If that asks about Black follows it.
Third, On Play Pressed gets Delete Save, before Restart Game. Its Slot starts on "default", so type "auto". Here’s why it’s there. Restart Game runs When the Game Starts again, and soon that will look for a saved game. A new game shouldn’t find the old one waiting.
Fourth, a new Event, savedGameLoaded, with the same call. Nothing sends it yet. Continue The Game will, at the end of the chapter.
11.5.5 Show Option
Marks this option of a panel (New Game or Settings) when it is chosen, whenever the choice changes (the Game Rules send "choiceChanged" and are its Other): the Side or the Strength the panel shows as chosen, the Board Finish, or the Piece Set. chosenColor: the color of a chosen option. plainColor: the color of the others.
Any Objectself.panelOptionself.newGameOption- Type
Text- Name
myOption
- Variable
myOption- Value
self.panelOption.option- Value
self.newGameOption.option
myOption == other.boardState.chosenSide || myOption == other.boardState.chosenStrengthmyOption == other.boardState.chosenSide || myOption == other.boardState.chosenStrength || myOption == other.boardState.boardFinish || myOption == other.boardState.pieceSet- Value
@self.chosenColor
- Value
@self.plainColor
Show Option marks one button green when it’s chosen. Now it knows four kinds of choice: a Side, a Strength, a Board Finish and a Piece Set. The If gets two more tests joined with ||, one for boardFinish and one for pieceSet.
The Data Structure on Show Option’s own card now names Panel Option. It reads as self.panelOption. Change step 2’s Value to self.panelOption.option, as the card shows in green.
The Event’s message changes, from newGameChoiceChanged to choiceChanged. The old blocks stay. Right-click its Event block and choose Change Event…. Pick Custom Event. Its Event Name takes choiceChanged, the new message both panels listen for. The Mode and its Actions stay. Name the Event On Choice Changed, as the card has it.
Run the game, and press New Game. The panel opens as it did, with your Side and Strength green. Press Black, and the green moves.
11.6 The Settings Panel
Now the panel itself, and the button that opens it.
11.6.1 Report Settings and the Three Reports
What the Settings button does: it tells whoever listens (the Game Rules) with "settingsPressed".
Any Object- Custom Event
settingsPressed
What a Strength button of the Settings panel does (Gentle to Expert): it tells whoever listens (the Game Rules) with "strengthSettingChosen"; the listener reads the button's Panel Option as Other.
Any Object- Custom Event
strengthSettingChosen
What a Board Finish button of the Settings panel does (Walnut, Tournament, Slate, Marble): it tells whoever listens (the Game Rules) with "boardFinishChosen"; the listener reads the button's Panel Option as Other.
Any Object- Custom Event
boardFinishChosen
What a Piece Set button of the Settings panel does (Carved walnut, Ivory & gold, Sculpted marble): it tells whoever listens (the Game Rules) with "pieceSetChosen"; the listener reads the button's Panel Option as Other.
Any Object- Custom Event
pieceSetChosen
Each Report sends one message on UI Activated, as Report Flip did in Chapter 6. Each kind of button gets its own Logic Component. The sender becomes Other. That’s how Settings will know which Board Finish you pressed.
11.6.2 Close Settings
What the Done button of the Settings panel does: it closes the panel, and the game goes on with the choices made.
Any Object- Operation
Hide- Menu Element
Settings Panel
Close Settings is the Done button’s. Control Menu, with its Operation on Hide, closes the panel, as Close New Game does.
11.6.3 Use The Last Look
Takes the Board Finish and the Piece Set kept on this device (the Persistent Values "boardFinish" and "pieceSet") into Board State, when the game has them (a row of the Board Finishes table, a row of the Piece Sets table; otherwise Board State's own value stays), and tells everyone "lookChanged", so the Board, its labels, and every Piece show them. gameRules: the Object that carries Board State.
boardStateData StructureBoard State- Action Name
savedFinish- Key
"boardFinish"- Default
gameRules.boardState.boardFinish
- Action Name
savedPieceSet- Key
"pieceSet"- Default
gameRules.boardState.pieceSet
- Action Name
isKnownFinish- Table
Board Finishes- Row
savedFinish.value
isKnownFinish.value == true- Component Value
gameRules.boardState.boardFinish- Value
savedFinish.value
- Action Name
isKnownPieceSet- Table
Piece Sets- Row
savedPieceSet.value
isKnownPieceSet.value == true- Component Value
gameRules.boardState.pieceSet- Value
savedPieceSet.value
Use The Last Look works like Use The Last Strength, twice. It loads the Board Finish and the Piece Set kept on this computer. A kept word the game has no row for is passed over, and Board State keeps its own. That’s the very first time, too, when nothing is kept yet.
Its Description says lookChanged goes out, but none of its steps sends it. Settings does.
11.6.4 Settings
Keeps the Player's settings: the Strength, the Board Finish, and the Piece Set. When the game begins (Game Start, and again after Restart Game), and when a saved game has been brought back ("savedGameLoaded"), it takes (Use The Last Look) the Board Finish and the Piece Set saved on this device (Persistent Values), when the game has them (the Board Finishes and Piece Sets tables; otherwise, and the first time, Board State's own boardFinish and pieceSet), and tells everyone "lookChanged", so the Board, its labels, and every Piece show them. (New Game takes the Strength saved, with the Side.) When the Player presses Settings ("settingsPressed"), it opens the Settings panel, a menu that holds the game while it is open. Each option pressed is used at once and kept on this device: a Strength ("strengthSettingChosen") is the Computer's from its next Move; a Board Finish ("boardFinishChosen") or a Piece Set ("pieceSetChosen") is shown at once ("lookChanged"). The pressed button is its Other, and every option then shows whether it is chosen ("choiceChanged").
Any Objectself.boardState- gameRules
self
- Custom Event
lookChanged
- Component Value
self.boardState.chosenStrength- Value
self.boardState.strength
- Menu Element
Settings Panel
- Menu Element
Settings Panel
- Custom Event
choiceChanged
- Component Value
self.boardState.strength- Value
other.panelOption.option
- Component Value
self.boardState.chosenStrength- Value
other.panelOption.option
- Action Name
persistentWriteResult- Key
"strength"- Value
self.boardState.strength
- Custom Event
choiceChanged
- Component Value
self.boardState.boardFinish- Value
other.panelOption.option
- Action Name
persistentWriteResult2- Key
"boardFinish"- Value
self.boardState.boardFinish
- Custom Event
lookChanged
- Custom Event
choiceChanged
- Component Value
self.boardState.pieceSet- Value
other.panelOption.option
- Action Name
persistentWriteResult3- Key
"pieceSet"- Value
self.boardState.pieceSet
- Custom Event
lookChanged
- Custom Event
choiceChanged
- gameRules
self
- Custom Event
lookChanged
Settings keeps your three choices. It has seven Events, and most are short.
When the Game Starts calls Use The Last Look. It runs before anything else, before the Board is even laid out. So it can’t show the look yet. There’s nothing to show it on. On Board Laid Out comes once every Square has its Objects, and that’s when lookChanged goes out.
On Settings Pressed opens the panel, as New Game opens its own. The panel’s chosenStrength takes the Strength being played, so that one is green. Control Menu opens every piece in the UI Group Settings, and Set Menu Pause State holds the game still behind it. Then choiceChanged, and the green goes on the right buttons.
Each of the next three uses a choice at once, and keeps it. The pressed button’s option goes into Board State. Save Persistent Value keeps it under its own Key, in quote marks. A Board Finish or a Piece Set sends lookChanged, and everything wears it. Last, choiceChanged moves the green.
A Strength changes nothing on the screen. The Computer plays at it from its next Move. And New Game keeps the Strength under the same Key, 'strength', so both panels share one choice.
On Saved Game Loaded waits, as New Game’s does.
11.6.5 Game Rules
Board StateSelect and MoveSelect and Move DataChoose PromotionChoose Promotion DataMake Computer MoveMake Computer Move DataUndoUndo DataGive A HintGive A Hint DataDraw Threat LinesDraw Threat Lines DataAttach Settings to the Game Rules now. Save The Game and Continue The Game come in the last part.
11.6.6 Settings Panel and Option Picture
Window / UICenterUIX 600, Y 620TextSettings30Square Highlight#EFE6CFX 600, Y 620Settings26Semibold#503A2BCenterX 0, Y 270Window / UICenterUIX 64, Y 64TextSettings32Square HighlightX 64, Y 64The Settings Panel is the cream card, like New Game’s, 600 pixels wide and 620 high, with Settings near its top. Nobody presses it, so its Element Type is Text.
An Option Picture is a small picture that sits on an Option Button. Its Element Type is Text too. It takes no clicks, so a click on the picture lands on the button under it.
Read down the panel, and you meet it in the Set up card’s order. Its own word, Settings, is at the top. Under it, the first Panel Label has no Text line, so it keeps its Object’s word, Strength. Five Strength buttons sit under that. Then Board Finish over four buttons, Piece Set over three, and Done at the bottom. Behind it all, the Panel Backdrop dims the game.
The buttons use variants, as in Chapter 6. Make each variant below before placing it: Settings Button from Tool Button, Close Settings Button from Panel Button, and the three Options from Option Button. Each adds the Logic with its job.
Now go to the Set up card, and place everything under PLACE THESE, hidden but the Settings button. Your hands know most of it from Chapter 6, if you built it there. Every piece of the panel goes in the UI Group Settings, so the whole panel opens and closes together.
The Board Finish and Piece Set buttons are taller than the Strengths, to leave room for a picture. Each of them sets its size twice, its Bounding Box and its Sprite, both 104 by 96. Its word moves down, to a Y of -32, in a Font Size of 13.
Then the Option Pictures, fourteen of them. Each Board Finish gets two in the same spot. The first is a dark square, the frame, in the Board’s own label color. That’s its with Sprite > Tint line. The second sits in front, at a Z-Index of 33, slightly smaller: the Board itself, 58 by 58. Each Piece Set gets two Knights side by side, a White one on Frame 3 and a Black one on Frame 9.
Most colors come from the Objects you already made. Settings Panel keeps its cream, #EFE6CF. The button variants keep the tan, #E2D5B4, from their bases. Those colors are already there.
Only the four frames change Tint, as the Set up card shows. Select each Option Picture placement, open its Sprite part, and type that frame’s code in Tint’s HEX box. A placement’s own values shows where it is. The pictures stay white.
Run the game, and press Settings.
The game dims, and the cream panel opens over the Board. Casual is green, or the Strength you chose last. Walnut and Carved walnut are green. Press Slate. The Board turns to blue-gray stone under the panel, and the labels change their color with it. Press Ivory & gold, and every Piece changes at once, each on its own Square. Press Done.
Close the game window, and press Play again. Slate and Ivory & gold are still there, waiting on the Board.
11.7 A Game That Remembers
Your choices outlast the game window now. The game itself doesn’t. Play three Moves, close the window, and open it again. The Board starts over.
11.7.1 Use The Last Threats Choice
Takes Threats mode as it is kept on this device (the Persistent Value "threats") into Board State's isShowingThreats: on when the kept value is true, off otherwise (the first time there is none, and Board State's own value stays). gameRules: the Object that carries Board State.
boardStateData StructureBoard State- Action Name
savedThreats- Key
"threats"- Default
gameRules.boardState.isShowingThreats
savedThreats.value == true- Component Value
gameRules.boardState.isShowingThreats- Value
true
- Component Value
gameRules.boardState.isShowingThreats- Value
false
Use The Last Threats Choice is Show Threats’ old steps, from Chapter 9, in a Function of their own, with one difference. It has an Else. A saved game may have Threats on, and you may have turned it off since. Then the Else turns it off.
11.7.2 Show Threats
Turns Threats mode on and off, and keeps its marks up to date. When the game begins (Game Start), and when a saved game has been brought back ("savedGameLoaded"), it takes the choice kept on this device (the Persistent Value "threats", through Use The Last Threats Choice); after a saved game it also fills the Threats table and shows the legend, or empties it, ends any inspecting, and hides the legend, and shows the legend (the UI Group "Threats Legend") when Threats mode is on. At the start of every Turn ("turnStarted") it fills the Threats table again (Find Threats) while Threats mode is on. When the Threats button is pressed ("threatsPressed") on the Player's Turn or after the game has ended, it turns Threats mode over (Board State's isShowingThreats) and ends any inspecting, fills or empties the Threats table, shows or hides the legend, keeps the choice on this device, and every Square redraws ("positionChanged").
Any Objectself.boardState- Action Name
savedThreats- Key
"threats"- Default
self.boardState.isShowingThreats
savedThreats.value == true- Component Value
self.boardState.isShowingThreats- Value
true
- gameRules
self
self.boardState.isShowingThreats == true- UI Group
"Threats Legend"
self.boardState.isShowingThreats == true- gameRules
self
- Action Name
threatsLook
threatsLook.value == true- Component Value
self.boardState.inspected- Value
''
self.boardState.isShowingThreats == true- Component Value
self.boardState.isShowingThreats- Value
false
- UI Group
"Threats Legend"- Visible
No
- Component Value
self.boardState.isShowingThreats- Value
true
- gameRules
self
- UI Group
"Threats Legend"
self.boardState.selected != ""- from
self.boardState.selected- enPassantSquare
self.boardState.enPassantSquare
- Action Name
persistentWriteResult- Key
"threats"- Value
self.boardState.isShowingThreats
- Custom Event
positionChanged
- gameRules
self
self.boardState.isShowingThreats == true- gameRules
self
- UI Group
"Threats Legend"
- Component Value
self.boardState.inspected- Value
''
- UI Group
"Threats Legend"- Visible
No
When the Game Starts calls the new Function, in place of its steps 2 to 4. Delete them, and add the call after Clear Threats.
On Saved Game Loaded is new. After a saved game lands, Threats mode is whatever you chose last. If it’s on, Find Threats runs and the legend shows. If it’s off, the table is cleared, nothing is inspected, and the legend hides.
11.7.3 Give A Hint
Gives the Player a Hint. When the Hint button is pressed ("hintPressed") on the Player's Turn (Game State Playing), the selection is cleared, the Board waits (Game State Finding A Hint), the status line says a Hint is being found, and the Stockfish Bridge asks Stockfish for a Move at the Hint Strength (the Hint Strength table). When Stockfish answers ("hintChosen"; the Move is in the Chosen Moves table, row Hint) and the Move is a Legal Move, the game is played again, the status line shows the Turn again (Look For Check), everyone hears "hintFound" (the Hint message shows it), and the Squares redraw ("positionChanged") with the Hint marked. When the Move is not legal, is empty, or has not come within the Hint's thinking time and spareSeconds more (the hintWait timer), no Hint is shown (Mark Hint Not Found). spareSeconds: how much longer than its thinking time Stockfish may take to answer, starting Stockfish included. When a saved game is brought back while a Hint was being found ("savedGameLoaded"), the Player's Turn goes on without it.
Any Objectself.boardStateself.giveAHintData- Action Name
isPlaying- Game State
Playing
isPlaying.value == true- Component Value
self.boardState.selected- Value
''
- Game State
Finding A Hint
- Variable
$status- Value
data('Words', 'Finding Hint', 'text')
- Java Script
lib.ext.stockfishBridge.chooseHint(api, ctx.self);
- Component Value
self.giveAHintData.waitSeconds- Value
data('Hint Strength', 'Hint', 'thinkingSeconds')
- Component Value
self.giveAHintData.waitSeconds- Operator
+=- Value
@self.spareSeconds
- Timer Name
"hintWait"- Repeat Every (s)
self.giveAHintData.waitSeconds- Ring Once
Yes
- Custom Event
positionChanged
- Action Name
isFinding- Game State
Finding A Hint
isFinding.value == true- Timer Name
"hintWait"
- Component Value
self.giveAHintData.fromSquare- Value
data('Chosen Moves', 'Hint', 'from')
- Component Value
self.giveAHintData.toSquare- Value
data('Chosen Moves', 'Hint', 'to')
- from
self.giveAHintData.fromSquare- enPassantSquare
self.boardState.enPassantSquare
data('Targets', self.giveAHintData.toSquare, 'reachable') == true- Game State
Playing
- gameRules
self
- Custom Event
hintFound
- Custom Event
positionChanged
- gameRules
self
- Action Name
isStillFinding- Game State
Finding A Hint
isStillFinding.value == true- gameRules
self
- Action Name
wasFindingHint- Game State
Finding A Hint
wasFindingHint.value == true- Game State
Playing
- gameRules
self
Here’s a small thing a saved game can carry. A game saved while a Hint was being found still says Finding A Hint. But the question to the Computer isn’t in the photograph. So Give A Hint listens for savedGameLoaded. If the game was finding a Hint, it goes back to Playing, and Look For Check puts your Turn’s words back under the Board.
11.7.4 Flip Board
Turns the Board over for viewing when the Player presses Flip ("flipPressed"), or back again; once everyone has heard "boardFlipped", it tells its own Object "flipFinished", so Save The Game saves the game the new way round. Only the way the Board is drawn changes (Board State's isFlipped): the position, the Turn, and the Player's Side stay as they are. Then it tells everyone "boardFlipped", so every Piece, Square, Move Mark, and label shows itself the new way round. When the Board has been laid out ("boardLaidOut"), it tells everyone "boardFlipped" too, so everything is shown the way round the Board is drawn.
Any Objectself.boardStateself.boardState.isFlipped == true- Component Value
self.boardState.isFlipped- Value
false
- Component Value
self.boardState.isFlipped- Value
true
- Custom Event
boardFlipped
- Custom Event
flipFinished
- Custom Event
boardFlipped
Flip Board gets one last step. Once everyone has heard boardFlipped, Trigger Custom Event on Object tells its own Object flipFinished. Its Applies to stays on Self. Save The Game, on the same Game Rules, is the one listening.
11.7.5 Save The Game
Saves the game in the save slot "auto" whenever it changes: at the start of every Turn of a game that goes on ("gameContinues", which Look For Game End sends after deciding the game is not over, and Continue The Game after a saved game has been brought back), when the game ends (the Game State becomes Game Over), and when the Player has turned the Board over or back ("flipFinished", from Flip Board). The save holds the Board, the Move History, Board State, the Game State, and every Object as it is; Continue The Game brings it back when the game is opened again.
Any Object- Action Name
gameSave- Slot
"auto"
- Action Name
isGameOver- Game State
Game Over
isGameOver.value == true- Action Name
finishedGameSave- Slot
"auto"
- Action Name
flippedGameSave- Slot
"auto"
Save The Game takes the photograph. It has three Events, and each makes one Save Game. Each Save Game’s Slot starts on "default", so type "auto" into every one. Give each save its own Action Name, as the card shows, though nothing reads those names. Nothing reads them.
On Game Continues saves at the start of every Turn of a game that goes on. Why not on turnStarted? Look For Game End sends gameContinues only after it has decided the game isn’t over. On turnStarted, the save would come before or after that decision, depending on the order of the Logic on the Game Rules. gameContinues comes after, always.
On Game State Changed saves once more when the game ends, so a finished game opens finished. Game State Changed asks nothing more in the Add Event window. Check Game State does the asking.
On Flip Finished saves when you turn the Board, so the game opens the way round you left it.
11.7.6 Continue The Game
Brings back the saved game (the save slot "auto") when the game is opened. On Game Start, when the slot holds a game, it turns Save The Game off and loads it: the Scene's own new game still begins first, and must not be saved over the game on its way. When the engine has brought the saved game back ("load:succeeded"), it tells its own Object "savedGameLoaded" (Settings, New Game, and Show Threats use the choices kept on this device again), every Square redraws ("positionChanged"), and, when the game is not over, it says "gameContinues", so the Computer moves when it is its Move and the game is saved again. Save The Game comes back on with the saved game; when a load fails ("load:failed") it is turned on again here.
Any Objectself.boardState- Action Name
hasSavedGame- Slot
"auto"
hasSavedGame.value == true- Logic Component
Save The Game- State
Disable
- Action Name
gameLoad- Slot
"auto"
- Custom Event
savedGameLoaded
- Custom Event
positionChanged
- Action Name
isGameOver- Game State
Game Over
isGameOver.value != true- Custom Event
gameContinues
- Logic Component
Save The Game
Continue The Game brings the photograph back. Take it slowly. This is the chapter’s hard idea, beat by beat.
The game opens, and When the Game Starts asks Check Save Exists whether the slot auto holds a game. Type "auto" into its Slot, as before. If there’s none, nothing else happens, and the new game on the Board is the game.
If there is one, two things happen. Set Logic State turns Save The Game off. Pick Save The Game from its Logic Component list, which names the Logic in your project. Its State is a list. It starts on Enable, so choose Disable. Last, Load Game asks for the game in the slot auto, and the waiting begins.
Why turn the saving off? Because the load takes a moment. Meanwhile the Scene starts its own new game, and its first Turn sends gameContinues. With Save The Game on, that new game would be photographed over the one on its way.
A moment later, the saved game lands. Myth sends a message of its own, load:succeeded. Add it as a Custom Event, typed exactly so, colon and all.
A load runs no Create and no Scene Start, so nothing in the game knows yet that anything changed. Continue The Game says so, in a set order. First, savedGameLoaded, to its own Object. Settings, New Game and Show Threats each use your kept choices again. A choice you made after the last save wins over the copy in the photograph. Then positionChanged, and every Square shows the Board that came back. Last, if the game isn’t over, gameContinues. The Computer moves, if it’s its Move, and Save The Game saves again.
By then, Save The Game is on again, and nothing in Continue The Game turned it on. The photograph did. It holds Save The Game as it was when it was taken, and it was on.
And when a load fails? Myth sends load:failed, and shows a notice of its own that says why. The new game goes on. Set Logic State turns Save The Game back on, with its State left on Enable. The game is saved again from its next Turn.
Now go back to the Game Rules’ card, in the part before this one, The Settings panel. Attach Save The Game and Continue The Game.
Run the game. Play e2 to e4, and wait for the reply. Then close the game window.
Press Play again. Your game is back: your Pawn on e4, the Computer’s reply, the last Move’s two Squares in soft yellow, and Your move under the Board.
Your game keeps its place now, and the looks you gave it.
Watch for each of these.
- Press Settings. The game dims, and a cream panel opens with three rows. Your Strength is green, and so are Walnut and Carved walnut. Each Board Finish button shows its Board in a thin frame, and each Piece Set button a White and a Black Knight.
- Press Tournament, then Slate, then Marble. The Board under the panel changes each time, and so does the color of its Rank and File labels.
- Press Sculpted marble. Every Piece on the Board changes, and nothing moves from its Square.
- Press Expert, then Done. Press New Game. Expert is green there too, because both panels share one Strength. Press Cancel.
- Play e2 to e4, and wait for the reply. Close the game window, and press Play. The same position comes back, in the same looks, with Your move under the Board.
- Press Threats, then Flip. Close the window and press Play. The Board is still turned, Threats is still pale green, and the legend shows.
- Press New Game, then Play. Close the window and press Play again, and the new game comes back, not the old one.
Settings keeps three choices on this computer, and sends lookChanged. The Board, its labels, the Pieces, the Promotion buttons and the Moving Pieces each dress themselves in the new look. Save The Game takes a photograph of the whole game at the start of every Turn, at the end, and after a Flip. Continue The Game sets the table back from it when the game opens.
Underneath are two kinds of memory. Your choices belong to you, so they’re kept apart from any one game. The game belongs to the Board, so the whole of it is kept together. When the two disagree, your latest choice wins.
11.8 If It Didn’t Work
This chapter has a lot of placing, and a game that remembers can remember a mistake too. Close the window and open it again often, and watch what comes back.
The panel opens white, or a button stays white? A Tint is missing. Select that placement, open its Sprite part, and give Tint the cream #EFE6CF for the panel, or the tan #E2D5B4 for a button.
A block drawn in red, with a number on the Errors bar? Open the bar. It names the block and what it misses. Compare that block with its card. Perhaps a newGameOption is left in a Value that now needs panelOption.
Settings opens, but pressing Slate changes nothing? Look at that button’s placement. It should pick Board Finish Option, whose Logic includes Report Board Finish. Is Show Board Finish attached to the Board?
The Board changes, but the Pieces don’t? Open On Look Changed. Its first call needs other.boardState.pieceSet, from the Game Rules that sent the message.
The labels are hard to read on Slate? Show Label’s On Look Changed may be missing, or the old coloring steps still sit in On Board Laid Out.
A choice comes back wrong after you close the window? Compare the two Keys. Save Persistent Value’s 'boardFinish' and Load Persistent Value’s must be the same word, letter for letter.
The game always opens at the start, though you played on? Look at every Save Game, Load Game and Check Save Exists. Each Slot must say "auto", not "default". Then look at Continue The Game’s step 3. Left on Enable, it lets the new game’s first Turn be saved over yours.
The Computer never answers after the game comes back? Perhaps On Load:succeeded is missing its last If, and its gameContinues.
New Game’s Play brings back your old game? On Play Pressed needs its Delete Save, with auto, before Restart Game.
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 this chapter’s game, finished, if you’d like to compare.
Adding a Board Finish is adding a row to Board Finishes, with its picture and two label colors, and one more button on the panel. What would yours look like? And what else should your game remember? Perhaps how many games you’ve won against each Strength. A Persistent Value can keep a count as well as a word. The rest is up to you.
Still stuck? Search the manual, or ask in the Myth Engine Discord (opens a new tab).
Your game keeps its looks and its place now. In Chapter 12, the last one, it gets a Game menu with every Move written down, and a way to play a whole game from the keyboard.
