Build a GameCreating Chess

11  Settings and Saving

Goal

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.

Chess, Chapter 11 finishedZip of a Myth project · 11.9 MB

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.

Set upWhat this chapter adds
ART (sprites, from the starter kit)
Tournament Board
Slate Board
Marble Board
Ivory And Gold Pieces
Sculpted Marble Pieces
GAME SETTINGS > Display > Rendering
Sprite BatchingOff (One Mesh per Sprite)
SCENE Chess Table: PLACE THESE
ObjectXY
Settings Button26040
with Text / UI > Text = Settings (Edit Object Locally)
Panel Backdrop00hidden
with Z-Index = 29
with UI Interaction > Group = Settings (Edit Object Locally)
Settings Panel070hidden
Panel Label0295hidden
with Z-Index = 32
with UI Interaction > Group = Settings (Edit Object Locally)
Strength Setting Option-232255hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Gentle
Strength Setting Option-116255hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Casual
Strength Setting Option0255hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Club
Strength Setting Option116255hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Strong
Strength Setting Option232255hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Expert
Panel Label0195hidden
with Z-Index = 32
with UI Interaction > Group = Settings (Edit Object Locally)
with Text / UI > Text = Board Finish (Edit Object Locally)
Board Finish Option-174129hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Walnut
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
Board Finish Option-58129hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Tournament
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
Board Finish Option58129hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Slate
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
Board Finish Option174129hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Marble
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
Panel Label045hidden
with Z-Index = 32
with UI Interaction > Group = Settings (Edit Object Locally)
with Text / UI > Text = Piece Set (Edit Object Locally)
Piece Set Option-116-21hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Carved walnut
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
Piece Set Option0-21hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Ivory & gold
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
Piece Set Option116-21hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Data Structure > Values > Option = Sculpted marble
with Text / UI > Font Size = 13 (Edit Object Locally)
with Text / UI > Transform > Position = X 0, Y -32 (Edit Object Locally)
with Transform > Bounding Box > Size = X 104, Y 96 (Edit Object Locally)
with Sprite > Transform > Size = X 104, Y 96 (Edit Object Locally)
Close Settings Button0-155hidden
with UI Interaction > Group = Settings (Edit Object Locally)
with Text / UI > Text = Done (Edit Object Locally)
Option Picture-174139hidden
with Sprite > Tint = #503A2B (Edit Object Locally)
Option Picture-174139hidden
with Z-Index = 33
with Transform > Bounding Box > Size = X 58, Y 58 (Edit Object Locally)
with Sprite > Sprite = Walnut Board (Edit Object Locally)
with Sprite > Transform > Size = X 58, Y 58 (Edit Object Locally)
Option Picture-58139hidden
with Sprite > Tint = #314B3E (Edit Object Locally)
Option Picture-58139hidden
with Z-Index = 33
with Transform > Bounding Box > Size = X 58, Y 58 (Edit Object Locally)
with Sprite > Sprite = Tournament Board (Edit Object Locally)
with Sprite > Transform > Size = X 58, Y 58 (Edit Object Locally)
Option Picture58139hidden
with Sprite > Tint = #344756 (Edit Object Locally)
Option Picture58139hidden
with Z-Index = 33
with Transform > Bounding Box > Size = X 58, Y 58 (Edit Object Locally)
with Sprite > Sprite = Slate Board (Edit Object Locally)
with Sprite > Transform > Size = X 58, Y 58 (Edit Object Locally)
Option Picture174139hidden
with Sprite > Tint = #424840 (Edit Object Locally)
Option Picture174139hidden
with Z-Index = 33
with Transform > Bounding Box > Size = X 58, Y 58 (Edit Object Locally)
with Sprite > Sprite = Marble Board (Edit Object Locally)
with Sprite > Transform > Size = X 58, Y 58 (Edit Object Locally)
Option Picture-136-11hidden
with Transform > Bounding Box > Size = X 57.48, Y 57.48 (Edit Object Locally)
with Sprite > Sprite = Carved Walnut Pieces (Edit Object Locally)
with Sprite > Frame = 3 (Edit Object Locally)
with Sprite > Transform > Size = X 57.48, Y 57.48 (Edit Object Locally)
Option Picture-96-11hidden
with Transform > Bounding Box > Size = X 57.48, Y 57.48 (Edit Object Locally)
with Sprite > Sprite = Carved Walnut Pieces (Edit Object Locally)
with Sprite > Frame = 9 (Edit Object Locally)
with Sprite > Transform > Size = X 57.48, Y 57.48 (Edit Object Locally)
Option Picture-20-11hidden
with Transform > Bounding Box > Size = X 55.13, Y 55.13 (Edit Object Locally)
with Sprite > Sprite = Ivory And Gold Pieces (Edit Object Locally)
with Sprite > Frame = 3 (Edit Object Locally)
with Sprite > Transform > Size = X 55.13, Y 55.13 (Edit Object Locally)
Option Picture20-11hidden
with Transform > Bounding Box > Size = X 55.13, Y 55.13 (Edit Object Locally)
with Sprite > Sprite = Ivory And Gold Pieces (Edit Object Locally)
with Sprite > Frame = 9 (Edit Object Locally)
with Sprite > Transform > Size = X 55.13, Y 55.13 (Edit Object Locally)
Option Picture96-11hidden
with Transform > Bounding Box > Size = X 54.81, Y 54.81 (Edit Object Locally)
with Sprite > Sprite = Sculpted Marble Pieces (Edit Object Locally)
with Sprite > Frame = 3 (Edit Object Locally)
with Sprite > Transform > Size = X 54.81, Y 54.81 (Edit Object Locally)
Option Picture136-11hidden
with Transform > Bounding Box > Size = X 54.81, Y 54.81 (Edit Object Locally)
with Sprite > Sprite = Sculpted Marble Pieces (Edit Object Locally)
with Sprite > Frame = 9 (Edit Object Locally)
with Sprite > Transform > Size = X 54.81, Y 54.81 (Edit Object Locally)
REMOVED
Data Structure: New Game Option

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

Data StructureBoard StateAddDelete

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).

sideToMoveTextdefault White
selectedTextdefault (empty)
lastFromTextdefault (empty)
lastToTextdefault (empty)
checkSquareTextdefault (empty)
enPassantSquareTextdefault (empty)
promotionSquareTextdefault (empty)
playerSideTextdefault White
fiftyMoveCountNumberdefault 0
isFlippedBooleandefault false
strengthTextdefault Casual
chosenSideTextdefault (empty)
chosenStrengthTextdefault (empty)
isShowingThreatsBooleandefault false
inspectedTextdefault (empty)
boardFinishTextdefault Walnut
pieceSetTextdefault Carved walnut

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

Data StructureBoard Finishes Entry

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.

boardThing Spritedefault None
labelOnLightColordefault #000000
labelOnDarkColordefault #000000
Data TableBoard FinishesData Structure: Board Finishes Entry
Row 0Walnut
boardWalnut Board
labelOnLight#503a2b
labelOnDark#dfc69b
Row 1Tournament
boardTournament Board
labelOnLight#314b3e
labelOnDark#e8ead5
Row 2Slate
boardSlate Board
labelOnLight#344756
labelOnDark#dce4e8
Row 3Marble
boardMarble Board
labelOnLight#424840
labelOnDark#eae5db

Board 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

Data StructurePiece Sets Entry

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.

piecesThing Spritedefault None
sizeNumberdefault 1
Data TablePiece SetsData Structure: Piece Sets Entry
Row 0Carved walnut
piecesCarved Walnut Pieces
size1
Row 1Ivory & gold
piecesIvory And Gold Pieces
size0.9591
Row 2Sculpted marble
piecesSculpted Marble Pieces
size0.9535

Piece 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

FunctionShow 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.

PARAMETERS
pieceObject
pieceSetText
ACTIONS
1Set Sprite
Applies to
piece
Sprite
data('Piece Sets', pieceSet, 'pieces')
2Set Scale
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

Logic ComponentShow Board FinishAttach to: Board

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).

Attach toAny Object
On Look Changed
WhenlookChangedCustom Event
1Set Sprite
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

ObjectBoardAddDelete
Transform
Bounding Box > SizeX 30, Y 30
Sprite
SpriteWalnut Board
Transform > SizeX 30, Y 30
Data Structure
From LogicLay Out The Board
Data StructureLay Out The Board Data
Logic
Lay Out The Board
Show Board Finish
Shared Variables
pieceObjectPiece
rankLabelObjectRank Label
fileLabelObjectFile Label
squareObjectSquare
moveMarkObjectMove Mark
threatMarkObjectThreat Mark
pinMarkObjectPin Mark

Attach Show Board Finish to the Board.

11.4.7 Show Label

Logic ComponentShow LabelAttach to: Rank Label, File 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).

Attach toAny Object
DATA STRUCTURES
Square Infoself.squareInfo
COMPONENTS
self.textText / UI
SHARED VARIABLES
NameTypeKindStarting Value
showsTextConstantrank
edgeTextConstantfile
On Board Laid Out
WhenboardLaidOutCustom Event
1Change Component Value
Component Value
self.text.text
Value
data('Position', self.squareInfo.name, @self.shows)
On Board Flipped
WhenboardFlippedCustom Event
1Declare Variable
Name
edgeLine
Value
0
2Change Variable
Variable
edgeLine
Value
data('Position', self.squareInfo.name, @self.edge)
3Ifother.boardState.isFlipped == true
Then
4Change Variable
Variable
edgeLine
Value
9 - edgeLine
5IfedgeLine == 1
Then
6Set Visible
Visible
Yes
7Otherwise
8Set Visible
Visible
No
On Look Changed
WhenlookChangedCustom Event
1Declare Variable
Type
Text
Name
finish
2Declare Variable
Name
squareShade
Value
0
3Declare Variable
Name
rank
Value
0
4Declare Variable
Name
file
Value
0
5Change Variable
Variable
finish
Value
other.boardState.boardFinish
6Change Variable
Variable
file
Value
data('Position', self.squareInfo.name, 'file')
7Change Variable
Variable
rank
Value
data('Position', self.squareInfo.name, 'rank')
8Change Variable
Variable
squareShade
Value
(file + rank) % 2
9IfsquareShade == 1
Then
10Change Component Value
Component Value
self.text.color
Value
data('Board Finishes', finish, 'labelOnLight')
11Otherwise
12Change Component Value
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

Logic ComponentShow PieceAttach to: PieceAddDelete

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").

Attach toAny Object
DATA STRUCTURES
Square Infoself.squareInfo
On Board Laid Out
WhenboardLaidOutCustom Event
1Show Piece On SquareFunction
piece
self
On Position Changed
WhenpositionChangedCustom Event
1Show Piece On SquareFunction
piece
self
On Slide Started
WhenslideStartedCustom Event
1IfdataFind('Slides', 'from', self.squareInfo.name) != (-1)
Then
2Set Alpha
Alpha
0
On Look Changed
WhenlookChangedCustom Event
1Show Piece SetFunction
piece
self
pieceSet
other.boardState.pieceSet
2Show Piece On SquareFunction
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

Logic ComponentShow Promotion ChoiceAttach to: Promotion ButtonAddDelete

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").

Attach toAny Object
DATA STRUCTURES
Promotion Choiceself.promotionChoice
On Position Changed
WhenpositionChangedCustom Event
1Declare Variable
Type
Text
Name
choiceKind
2Declare Variable
Type
Text
Name
side
3Change Variable
Variable
side
Value
other.boardState.sideToMove
4Change Variable
Variable
choiceKind
Value
self.promotionChoice.kind
5Set Frame
Frame
data('Kinds', choiceKind, side)
On Look Changed
WhenlookChangedCustom Event
1Show Piece SetFunction
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

Logic ComponentSlide PieceAttach to: Moving PieceAddDelete

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).

Attach toAny Object
DATA STRUCTURES
Slide Piece Dataself.slidePieceData
SHARED VARIABLES
NameTypeKindStarting Value
slideSecondsNumberConstant0.36
squareMetersNumberConstant3.75
boardLeftNumberConstant0
boardBottomNumberConstant4.375
PER INSTANCE VARIABLES
NameTypeStarting Value
slideTextPiece
saysWhenLandedBooleanYes
On Slide Started
WhenslideStartedCustom Event
1Declare Variable
Type
Text
Name
toSquare
2Declare Variable
Type
Text
Name
fromSquare
3Change Variable
Variable
fromSquare
Value
data('Slides', self.slide, 'from')
4Change Variable
Variable
toSquare
Value
data('Slides', self.slide, 'to')
5IffromSquare != ""
Then
6Change Component Value
Component Value
self.slidePieceData.isFlipped
Value
other.boardState.isFlipped
7Square PositionFunction
Action Name
startSquare
square
fromSquare
isFlipped
self.slidePieceData.isFlipped
squareMeters
@self.squareMeters
boardLeft
@self.boardLeft
boardBottom
@self.boardBottom
8Square PositionFunction
Action Name
endSquare
square
toSquare
isFlipped
self.slidePieceData.isFlipped
squareMeters
@self.squareMeters
boardLeft
@self.boardLeft
boardBottom
@self.boardBottom
9Change Component Value
Component Value
self.slidePieceData.kind
Value
data('Position', fromSquare, 'kind')
10Change Component Value
Component Value
self.slidePieceData.side
Value
data('Position', fromSquare, 'side')
11Show Piece SetFunction
piece
self
pieceSet
other.boardState.pieceSet
12Set Frame
Frame
data('Kinds', self.slidePieceData.kind, self.slidePieceData.side)
13Set Position
X
startSquare.x
Y
startSquare.y
14Set Visible
Visible
Yes
15Switch Mode
Event
On Board Flipped
Mode
Land At Once
16Switch Mode
Event
On Look Changed
Mode
Show The New Piece Set
17Tween
Value
self.transform.position.x
Target
endSquare.x
Duration
@self.slideSeconds
Ease
Sine in Out
18Tween
Value
self.transform.position.y
Target
endSquare.y
Duration
@self.slideSeconds
Ease
Sine in Out
19Otherwise
20Return
21Ifself.saysWhenLanded == true
Then
22Start Recurring Timer
Timer Name
"landing"
Repeat Every (s)
@self.slideSeconds
Ring Once
Yes
On the Landing Timer
WhenRecurring TimerTimer name: landing
1Trigger Custom Event
Custom Event
pieceLanded
On Position Changed
WhenpositionChangedCustom Event
1Set Visible
Visible
No
2Stop Recurring Timer
Timer Name
"landing"
3Switch Mode
Event
On Board Flipped
Mode
Stay Hidden
4Switch Mode
Event
On Look Changed
Mode
Stay Hidden
On Board Flipped
WhenboardFlippedCustom Event
ModeStay Hidden (Starting Mode)
(no Actions)
On Board Flipped
WhenboardFlippedCustom Event
ModeLand At Once
1Ifself.saysWhenLanded == true
Then
2Trigger Custom Event
Custom Event
pieceLanded
On Look Changed
WhenlookChangedCustom Event
ModeStay Hidden (Starting Mode)
(no Actions)
On Look Changed
WhenlookChangedCustom Event
ModeShow The New Piece Set
1Show Piece SetFunction
piece
self
pieceSet
other.boardState.pieceSet
2Set Frame
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

Data StructurePanel 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).

optionTextdefault (empty)

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.2 Option Button

ObjectOption ButtonAddDelete
Transform
Presentation > SpaceWindow / UI
Presentation > Screen AnchorCenter
Presentation > Render LayerUI
Bounding Box > SizeX 104, Y 44
UI Interaction
GroupNew Game
Sprite
Scene Depth Draw Order31
SpriteSquare Highlight
Tint#E2D5B4
Transform > SizeX 104, Y 44
Text / UI
Text{option}
Font Size16
EmphasisSemibold
Color#503A2B
AlignmentCenter
Data Structure
Data StructurePanel Option
Data StructureNew Game Option
Logic
Show Option

The rename has done this one already. Open the Option Button, and its Data Structure says Panel Option.

11.5.3 Use The Last Strength

FunctionUse 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.

PARAMETERS
NameTypeStarts at
gameRulesObjectStructural
Required Components
Slot NameboardState
Component TypeData Structure
Required SchemaBoard State
ACTIONS
1Load Persistent Value
Action Name
savedStrength
Key
"strength"
Default
gameRules.boardState.strength
2Check Data Row Exists
Action Name
isKnownStrength
Table
Strengths
Row
savedStrength.value
3IfisKnownStrength.value == true
Then
4Change Component Value
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

Logic ComponentNew GameAttach to: Game RulesAddDelete

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.

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
When the Game Starts
WhenGame Start
1Load Persistent Value
Action Name
savedSide
Key
"playerSide"
Default
self.boardState.playerSide
2Load Persistent Value
Action Name
savedStrength
Key
"strength"
Default
self.boardState.strength
2Check Data Row Exists
Action Name
isKnownSide
Table
Sides
Row
savedSide.value
3IfisKnownSide.value == true
Then
4Change Component Value
Component Value
self.boardState.playerSide
Value
savedSide.value
6Check Data Row Exists
Action Name
isKnownStrength
Table
Strengths
Row
savedStrength.value
7IfisKnownStrength.value == true
Then
8Change Component Value
Component Value
self.boardState.strength
Value
savedStrength.value
5Use The Last StrengthFunction
gameRules
self
6Ifself.boardState.playerSide == "Black"
Then
7Change Component Value
Component Value
self.boardState.isFlipped
Value
true
On New Game Pressed
WhennewGamePressedCustom Event
1Change Component Value
Component Value
self.boardState.chosenSide
Value
self.boardState.playerSide
2Change Component Value
Component Value
self.boardState.chosenStrength
Value
self.boardState.strength
3Control Menu
Menu Element
New Game Panel
4Set Menu Pause State
Menu Element
New Game Panel
5Check Game State
Action Name
isOver
Game State
Game Over
6Ifself.boardState.lastFrom != "" && isOver.value == false
Then
7Set UI Element Visibility
Element
Replace Question
8Otherwise
9Set UI Element Visibility
Element
Replace Question
Visible
No
10Trigger Custom Event
Custom Event
choiceChanged
Custom Event
newGameChoiceChanged
On Side Chosen
WhensideChosenCustom Event
1Change Component Value
Component Value
self.boardState.chosenSide
Value
other.panelOption.option
Value
other.newGameOption.option
2Trigger Custom Event
Custom Event
choiceChanged
Custom Event
newGameChoiceChanged
On Strength Chosen
WhenstrengthChosenCustom Event
1Change Component Value
Component Value
self.boardState.chosenStrength
Value
other.panelOption.option
Value
other.newGameOption.option
2Trigger Custom Event
Custom Event
choiceChanged
Custom Event
newGameChoiceChanged
On Play Pressed
WhenplayPressedCustom Event
1Save Persistent Value
Action Name
persistentWriteResult
Key
"playerSide"
Value
self.boardState.chosenSide
2Save Persistent Value
Action Name
persistentWriteResult2
Key
"strength"
Value
self.boardState.chosenStrength
3Delete Save
Slot
"auto"
4Restart Game
On Saved Game Loaded
WhensavedGameLoadedCustom Event
1Use The Last StrengthFunction
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

Logic ComponentShow OptionAttach to: Option ButtonAddDelete

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.

Attach toAny Object
DATA STRUCTURES
Panel Optionself.panelOption
New Game Optionself.newGameOption
SHARED VARIABLES
NameTypeKindStarting Value
chosenColorColorConstant#a9c79b
plainColorColorConstant#e2d5b4
On Choice Changed
WhenchoiceChangedCustom Event
On New Game Choice Changed
WhennewGameChoiceChangedCustom Event
1Declare Variable
Type
Text
Name
myOption
2Change Variable
Variable
myOption
Value
self.panelOption.option
Value
self.newGameOption.option
3IfmyOption == other.boardState.chosenSide || myOption == other.boardState.chosenStrength
Then
3IfmyOption == other.boardState.chosenSide || myOption == other.boardState.chosenStrength || myOption == other.boardState.boardFinish || myOption == other.boardState.pieceSet
Then
4Change Sprite Tint Property
Value
@self.chosenColor
5Otherwise
6Change Sprite Tint Property
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

Logic ComponentReport Settings

What the Settings button does: it tells whoever listens (the Game Rules) with "settingsPressed".

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
Custom Event
settingsPressed
Logic ComponentReport Strength Setting

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.

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
Custom Event
strengthSettingChosen
Logic ComponentReport Board Finish

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.

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
Custom Event
boardFinishChosen
Logic ComponentReport Piece Set

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.

Attach toAny Object
On Hud Activated
WhenUI Activated
1Trigger Custom Event
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

Logic ComponentClose Settings

What the Done button of the Settings panel does: it closes the panel, and the game goes on with the choices made.

Attach toAny Object
On Hud Activated
WhenUI Activated
1Control Menu
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

FunctionUse 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.

PARAMETERS
NameTypeStarts at
gameRulesObjectStructural
Required Components
Slot NameboardState
Component TypeData Structure
Required SchemaBoard State
ACTIONS
1Load Persistent Value
Action Name
savedFinish
Key
"boardFinish"
Default
gameRules.boardState.boardFinish
2Load Persistent Value
Action Name
savedPieceSet
Key
"pieceSet"
Default
gameRules.boardState.pieceSet
3Check Data Row Exists
Action Name
isKnownFinish
Table
Board Finishes
Row
savedFinish.value
4IfisKnownFinish.value == true
Then
5Change Component Value
Component Value
gameRules.boardState.boardFinish
Value
savedFinish.value
6Check Data Row Exists
Action Name
isKnownPieceSet
Table
Piece Sets
Row
savedPieceSet.value
7IfisKnownPieceSet.value == true
Then
8Change Component Value
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

Logic ComponentSettingsAttach to: Game Rules

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").

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
When the Game Starts
WhenGame Start
1Use The Last LookFunction
gameRules
self
On Board Laid Out
WhenboardLaidOutCustom Event
1Trigger Custom Event
Custom Event
lookChanged
On Settings Pressed
WhensettingsPressedCustom Event
1Change Component Value
Component Value
self.boardState.chosenStrength
Value
self.boardState.strength
2Control Menu
Menu Element
Settings Panel
3Set Menu Pause State
Menu Element
Settings Panel
4Trigger Custom Event
Custom Event
choiceChanged
On Strength Setting Chosen
WhenstrengthSettingChosenCustom Event
1Change Component Value
Component Value
self.boardState.strength
Value
other.panelOption.option
2Change Component Value
Component Value
self.boardState.chosenStrength
Value
other.panelOption.option
3Save Persistent Value
Action Name
persistentWriteResult
Key
"strength"
Value
self.boardState.strength
4Trigger Custom Event
Custom Event
choiceChanged
On Board Finish Chosen
WhenboardFinishChosenCustom Event
1Change Component Value
Component Value
self.boardState.boardFinish
Value
other.panelOption.option
2Save Persistent Value
Action Name
persistentWriteResult2
Key
"boardFinish"
Value
self.boardState.boardFinish
3Trigger Custom Event
Custom Event
lookChanged
4Trigger Custom Event
Custom Event
choiceChanged
On Piece Set Chosen
WhenpieceSetChosenCustom Event
1Change Component Value
Component Value
self.boardState.pieceSet
Value
other.panelOption.option
2Save Persistent Value
Action Name
persistentWriteResult3
Key
"pieceSet"
Value
self.boardState.pieceSet
3Trigger Custom Event
Custom Event
lookChanged
4Trigger Custom Event
Custom Event
choiceChanged
On Saved Game Loaded
WhensavedGameLoadedCustom Event
1Use The Last LookFunction
gameRules
self
2Trigger Custom Event
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

ObjectGame RulesAddDelete
Data Structure
Data StructureBoard State
Data Structure
From LogicSelect and Move
Data StructureSelect and Move Data
Data Structure
From LogicChoose Promotion
Data StructureChoose Promotion Data
Data Structure
From LogicMake Computer Move
Data StructureMake Computer Move Data
Data Structure
From LogicUndo
Data StructureUndo Data
Data Structure
From LogicGive A Hint
Data StructureGive A Hint Data
Data Structure
From LogicDraw Threat Lines
Data StructureDraw Threat Lines Data
Logic
Select and Move
Start Turn
Choose Promotion
Look For Game End
Flip Board
New Game
Make Computer Move
Undo
Give A Hint
Show Threats
Draw Threat Lines
Settings
Save The Game
Continue The Game

Attach 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

ObjectSettings Panel
Transform
Presentation > SpaceWindow / UI
Presentation > Screen AnchorCenter
Presentation > Render LayerUI
Bounding Box > SizeX 600, Y 620
UI Interaction
Element TypeText
GroupSettings
Sprite
Scene Depth Draw Order30
SpriteSquare Highlight
Tint#EFE6CF
Transform > SizeX 600, Y 620
Text / UI
TextSettings
Font Size26
EmphasisSemibold
Color#503A2B
AlignmentCenter
Transform > PositionX 0, Y 270
ObjectOption Picture
Transform
Presentation > SpaceWindow / UI
Presentation > Screen AnchorCenter
Presentation > Render LayerUI
Bounding Box > SizeX 64, Y 64
UI Interaction
Element TypeText
GroupSettings
Sprite
Scene Depth Draw Order32
SpriteSquare Highlight
Transform > SizeX 64, Y 64

The 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.

Object variantSettings ButtonVariant of: Tool Button
Logic
Report Settings
Object variantStrength Setting OptionVariant of: Option Button
Logic
Report Strength Setting
Object variantBoard Finish OptionVariant of: Option Button
Logic
Report Board Finish
Object variantPiece Set OptionVariant of: Option Button
Logic
Report Piece Set
Object variantClose Settings ButtonVariant of: Panel Button
Logic
Close Settings

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

FunctionUse 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.

PARAMETERS
NameTypeStarts at
gameRulesObjectStructural
Required Components
Slot NameboardState
Component TypeData Structure
Required SchemaBoard State
ACTIONS
1Load Persistent Value
Action Name
savedThreats
Key
"threats"
Default
gameRules.boardState.isShowingThreats
2IfsavedThreats.value == true
Then
3Change Component Value
Component Value
gameRules.boardState.isShowingThreats
Value
true
4Otherwise
5Change Component Value
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

Logic ComponentShow ThreatsAttach to: Game RulesAddDelete

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").

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
When the Game Starts
WhenGame Start
1Clear ThreatsFunction
2Load Persistent Value
Action Name
savedThreats
Key
"threats"
Default
self.boardState.isShowingThreats
3IfsavedThreats.value == true
Then
4Change Component Value
Component Value
self.boardState.isShowingThreats
Value
true
2Use The Last Threats ChoiceFunction
gameRules
self
When Created
WhenCreate
1Ifself.boardState.isShowingThreats == true
Then
2Set UI Group Visibility
UI Group
"Threats Legend"
On Turn Started
WhenturnStartedCustom Event
1Ifself.boardState.isShowingThreats == true
Then
2Find ThreatsFunction
gameRules
self
On Threats Pressed
WhenthreatsPressedCustom Event
1Can Show ThreatsFunction
Action Name
threatsLook
2IfthreatsLook.value == true
Then
3Change Component Value
Component Value
self.boardState.inspected
Value
''
4Ifself.boardState.isShowingThreats == true
Then
5Change Component Value
Component Value
self.boardState.isShowingThreats
Value
false
6Clear ThreatsFunction
7Set UI Group Visibility
UI Group
"Threats Legend"
Visible
No
8Otherwise
9Change Component Value
Component Value
self.boardState.isShowingThreats
Value
true
10Find ThreatsFunction
gameRules
self
11Set UI Group Visibility
UI Group
"Threats Legend"
12Ifself.boardState.selected != ""
Then
13Find Legal MovesFunction
from
self.boardState.selected
enPassantSquare
self.boardState.enPassantSquare
14Save Persistent Value
Action Name
persistentWriteResult
Key
"threats"
Value
self.boardState.isShowingThreats
15Trigger Custom Event
Custom Event
positionChanged
On Saved Game Loaded
WhensavedGameLoadedCustom Event
1Use The Last Threats ChoiceFunction
gameRules
self
2Ifself.boardState.isShowingThreats == true
Then
3Find ThreatsFunction
gameRules
self
4Set UI Group Visibility
UI Group
"Threats Legend"
5Otherwise
6Clear ThreatsFunction
7Change Component Value
Component Value
self.boardState.inspected
Value
''
8Set UI Group Visibility
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

Logic ComponentGive A HintAttach to: Game RulesAddDelete

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.

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
Give A Hint Dataself.giveAHintData
SHARED VARIABLES
NameTypeKindStarting Value
spareSecondsNumberConstant20
On Hint Pressed
WhenhintPressedCustom Event
1Check Game State
Action Name
isPlaying
Game State
Playing
2IfisPlaying.value == true
Then
3Change Component Value
Component Value
self.boardState.selected
Value
''
4Clear TargetsFunction
5Forget The HintFunction
6Set Game State
Game State
Finding A Hint
7Change Variable
Variable
$status
Value
data('Words', 'Finding Hint', 'text')
8Run Code (JS)
Java Script
lib.ext.stockfishBridge.chooseHint(api, ctx.self);
9Change Component Value
Component Value
self.giveAHintData.waitSeconds
Value
data('Hint Strength', 'Hint', 'thinkingSeconds')
10Change Component Value
Component Value
self.giveAHintData.waitSeconds
Operator
+=
Value
@self.spareSeconds
11Start Recurring Timer
Timer Name
"hintWait"
Repeat Every (s)
self.giveAHintData.waitSeconds
Ring Once
Yes
12Trigger Custom Event
Custom Event
positionChanged
On Hint Chosen
WhenhintChosenCustom Event
1Check Game State
Action Name
isFinding
Game State
Finding A Hint
2IfisFinding.value == true
Then
3Stop Recurring Timer
Timer Name
"hintWait"
4Change Component Value
Component Value
self.giveAHintData.fromSquare
Value
data('Chosen Moves', 'Hint', 'from')
5Change Component Value
Component Value
self.giveAHintData.toSquare
Value
data('Chosen Moves', 'Hint', 'to')
6Find Legal MovesFunction
from
self.giveAHintData.fromSquare
enPassantSquare
self.boardState.enPassantSquare
7Ifdata('Targets', self.giveAHintData.toSquare, 'reachable') == true
Then
8Clear TargetsFunction
9Set Game State
Game State
Playing
10Look For CheckFunction
gameRules
self
11Trigger Custom Event
Custom Event
hintFound
12Trigger Custom Event
Custom Event
positionChanged
13Otherwise
14Clear TargetsFunction
15Mark Hint Not FoundFunction
gameRules
self
16Otherwise
17Forget The HintFunction
On the Hint Wait Timer
WhenRecurring TimerTimer name: hintWait
1Check Game State
Action Name
isStillFinding
Game State
Finding A Hint
2IfisStillFinding.value == true
Then
3Mark Hint Not FoundFunction
gameRules
self
On Saved Game Loaded
WhensavedGameLoadedCustom Event
1Check Game State
Action Name
wasFindingHint
Game State
Finding A Hint
2IfwasFindingHint.value == true
Then
3Set Game State
Game State
Playing
4Look For CheckFunction
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

Logic ComponentFlip BoardAttach to: Game RulesAddDelete

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.

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
On Flip Pressed
WhenflipPressedCustom Event
1Ifself.boardState.isFlipped == true
Then
2Change Component Value
Component Value
self.boardState.isFlipped
Value
false
3Otherwise
4Change Component Value
Component Value
self.boardState.isFlipped
Value
true
5Trigger Custom Event
Custom Event
boardFlipped
6Trigger Custom Event on Object
Custom Event
flipFinished
On Board Laid Out
WhenboardLaidOutCustom Event
1Trigger Custom Event
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

Logic ComponentSave The GameAttach to: Game Rules

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.

Attach toAny Object
On Game Continues
WhengameContinuesCustom Event
1Save Game
Action Name
gameSave
Slot
"auto"
On Game State Changed
WhenGame State Changed
1Check Game State
Action Name
isGameOver
Game State
Game Over
2IfisGameOver.value == true
Then
3Save Game
Action Name
finishedGameSave
Slot
"auto"
On Flip Finished
WhenflipFinishedCustom Event
1Save Game
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

Logic ComponentContinue The GameAttach to: Game Rules

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.

Attach toAny Object
DATA STRUCTURES
Board Stateself.boardState
When the Game Starts
WhenGame Start
1Check Save Exists
Action Name
hasSavedGame
Slot
"auto"
2IfhasSavedGame.value == true
Then
3Set Logic State
Logic Component
Save The Game
State
Disable
4Load Game
Action Name
gameLoad
Slot
"auto"
On Load:succeeded
Whenload:succeededCustom Event
1Trigger Custom Event on Object
Custom Event
savedGameLoaded
2Trigger Custom Event
Custom Event
positionChanged
3Check Game State
Action Name
isGameOver
Game State
Game Over
4IfisGameOver.value != true
Then
5Trigger Custom Event on Object
Custom Event
gameContinues
On Load:failed
Whenload:failedCustom Event
1Set Logic State
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.

Play 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.
How it works

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.

Challenge

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.