TutorialsPush Lab

2  Place the Carts

Goal

Move either cart along its rail, let go without throwing it, and bring both carts home.

Drag either cart along its rail. Release it, then press Home or the orange Return button.

Push Lab, Chapter 2 finishedMyth project

Open the downloaded file with File › Open Project... in Myth Engine.

2.1 The Idea

The carts have waited on the bench long enough. Put your pointer against Blue Cart and draw it along the upper rail. Copper stays below, with its load above it. Each cart keeps its own lane.

Before the workshop can compare pushes, you need room to arrange the experiment. This chapter lets you arrange it. The dial still waits.

A Logic Component gives an Object behavior through Events and Actions. An Event starts work when its named moment arrives, such as a mouse press. Its Actions do the work.

Holding a cart and leaving it alone need different responses. A Mode chooses the response. Think of the rail’s handle being engaged while you place the cart, then released when you let go. In Holding, your pointer places the cart; in Released, native physics owns its motion.

The change between them matters. Letting go should leave the cart where you placed it, without turning a quick drag into a throw. You are arranging the apparatus. The push comes next chapter.

A Function is a named piece of work that other Logic can call. Release Cart handles letting go. Return Cart Home will use that release before moving a cart back to the same starting place.

2.2 The Plan

Capture Cart Press remembers the grabbed cart and the place you touched. Place Cart on Rail holds it. Release Cart gives that body back to physics as soon as the drag ends.

Home reaches each cart through Return Carts. The orange button takes another route: Return Button Response sends returnCarts, and each cart returns itself. Both routes share a quiet click.

2.3 Set Up

Continue with the prepared workshop you made last chapter, keeping its Scene and Game Settings. The kit already supplies the button picture and sound listed below.

Make Return Carts in Game Settings > Input Manager, with Home as its binding. Build Return Button before placing it. Its setup row tells you where the new Object belongs in the Scene.

Set upWhat this chapter adds
ART (sprites, from the starter kit)
Workshop return-button
SOUNDS (from the starter kit)
Workshop Click
COMMANDS (Game Settings > Input Manager)
Return CartsButton
KeybindingsHome
SCENE Lab: PLACE THESE
ObjectXY
Return Button18.0254.05

This is the first chapter with Logic. Turn on Settings > Advanced Mode so Execute Script is available in the Action list. Keep Making what a card shows beside you for Logic Components, Variables, Events, Modes and Functions. The Library and Script passages explain the supplied input helpers below.

Build the helpers first. Some cards name an attachment you will make later in this chapter, after its resource is ready.

2.4 Remember the Press

2.4.1 Cart Press Input

Grab the cart near its end, and that point should stay under your pointer. The cart should not jump to put its center there. The workshop remembers where your hand began.

Cart Press Input is a supplied Library for that small input job. Download its file from the card. Import that file into the Library resource and match its wiring so the following Scripts can find it.

The Library records selection and the grab offset, leaving every change in motion to your native Logic.

LibraryCart Press Input
SOURCEfrom a file
Import .js...push-lab-cart-press-input.js
Size1.8 KB
ADVANCED WIRING AND LOAD ORDER
Alias (Lib.Ext....)cartPress
Global NameCartPressInput

2.4.2 Capture Cart Press

The cart needs its own listener before you press it. Capture Cart Press connects that cart to the Library, which remembers the exact press.

Make a Script with this name and place the supplied source in Script Code. Keep this source whole. Here, ctx and api connect the supplied helper to its calling Object. Movement stays in Logic.

ScriptCapture Cart Press
Script Code
// Capture the exact point that the hand grabbed.
lib.ext.cartPress.attach(ctx, api);
Download this Script source

2.4.3 Capture Control Press

The orange button needs to recognize its own press too. Capture Control Press uses the same Library without asking for a cart’s grab offset.

Make this as another Script. Its added false tells the helper to skip the drag offset. Both calls keep the same supplied connection names.

ScriptCapture Control Press
Script Code
// Remember whether this control received the exact press.
lib.ext.cartPress.attach(ctx, api, false);
Download this Script source

Nothing moves yet. The workshop can remember which cart you grabbed and where you touched it.

2.5 Let Go and Return

2.5.1 Release Cart

You let go. The cart stays still on the rail, even if your pointer was moving fast. Release Cart makes that handover deliberate.

Its cart Parameter identifies the body being released. The required Transform and Collider give the Function the pieces it needs. A held cart resumes from rest.

A cart already released keeps its existing motion, which will matter when next chapter gives it a push.

FunctionRelease Cart

Returns a held cart to native physics without throwing it. Released carts keep their existing velocity.

PARAMETERS
NameTypeStarts at
cartObject
Compatibility ModeStructural
Required Components
Slot Nametransform
Component TypeTransform
Slot Namecollider
Component TypeCollider
ACTIONS
1Ifcart.collider.fixed == true
Then
2Set Velocity
Applies to
cart
X Velocity
0
Y Velocity
0
3Set Spin
Applies to
cart
Spin
0
4Change Component Value
Applies to
cart
Component Value
cart.collider.fixed
Value
false

2.5.2 Return Cart Home

Each cart needs a familiar place to begin another experiment. Return Cart Home ends a hold, stops the cart, and puts it back on its rail.

The same Function returns either cart, despite their separate rails. Returning leaves the mass alone.

FunctionReturn Cart Home

Stops one cart and returns it to its chosen lane start without changing mass.

PARAMETERS
NameTypeStarts at
cartObject
Compatibility ModeStructural
Required Components
Slot Nametransform
Component TypeTransform
Slot Namecollider
Component TypeCollider
homeXNumber6.75
laneYNumber10.975
ACTIONS
1Release CartFunction
cart
cart
2Set Velocity
Applies to
cart
X Velocity
0
Y Velocity
0
3Set Position
Applies to
cart
X
homeX
Y
laneY

2.5.3 Play Workshop Click

Home and the orange button should sound like the same apparatus answering. Play Workshop Click gives both that quiet answer. Its silent reset and import cases prepare for later chapters; no extra control is missing here.

FunctionPlay Workshop Click

Plays the quiet workshop click for an accepted input request. Reset, setup import and canceled hands stay silent.

PARAMETERS
NameTypeStarts at
acceptedBooleanYes
ACTIONS
1Ifaccepted == true
Then
2Control Sound
Sound
Workshop Click

The helpers are ready for their callers.

2.6 Give the Carts a Hand

2.6.1 Place Cart on Rail

Blue Cart can now follow your pointer. The press listener remembers which cart you touched and its grab offset, so the body stays beneath your hand.

Place Cart on Rail prepares the listener after the workshop opens, before you begin arranging the carts. Its short timer rings once. A selected press stops the cart, fixes its Collider in place, and chooses Holding for the held Event.

The Shared Variables describe the rail and home position. Per Instance Variables remember this cart’s current drag. Declare both kinds in the Logic editor, as Making what a card shows describes.

The Components row needs the existing cart Collider. Add its reference as collider in the Logic’s Components card. Keep the self. prefix shown on the generated card out of that Name field.

Before filling the first panel, prepare its later target. Create While the Left Mouse Button Is Held with Released and Holding as its Modes. Keep Released as its starting Mode. Their Actions can wait for the next panel. The first panel’s Switch Mode can now choose that Event and Holding.

Logic ComponentPlace Cart on RailAttach to: Blue Cart, Copper Cart

Place one exact cart along its rail with the pointer, release it from rest, or return it home.

Attach toAny Object
COMPONENTS
self.colliderCollider
SHARED VARIABLES
NameTypeKindStarting Value
homeXNumberConstant6.75
laneYNumberConstant10.975
leftLimitNumberConstant5.975
rightLimitNumberConstant20.875
PER INSTANCE VARIABLES
NameTypeStarting Value
grabOffsetNumber0
handXNumber0
pressSelectedBooleanNo
When Created
WhenCreate
1Start Recurring Timer
Timer Name
"cartInputReady"
Repeat Every (s)
0.02
Ring Once
Yes
On the Cart Input Ready Timer
WhenRecurring TimerTimer name: cartInputReady
1Execute Script
Script
Capture Cart Press
When the Left Mouse Button Is Pressed
WhenMouse PressMouse button: left
1Ifself.pressSelected == true
Then
2Set Velocity
X Velocity
0
Y Velocity
0
3Change Component Value
Component Value
self.collider.fixed
Value
true
4Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Holding

Now follow the held Event. It has two Modes, Released and Holding, within one Event. Released starts with no Actions to carry out. Holding subtracts the grab offset before placing the cart, keeping the original contact point under your pointer.

The clamp expression keeps the requested position between the rail’s limits. The lane height stays fixed. Drag diagonally and the cart still travels along its rail.

Enter these expressions as shown, including their prefixes. In the handX expression, valueMember(event.position,“x”) takes the pointer Event’s horizontal coordinate. The subtraction removes this hand’s grabOffset. Here, self reads this cart’s Per Instance value; @self reads its Shared value.

Logic ComponentPlace Cart on Railcontinued
While the Left Mouse Button Is Held
WhenMouse HeldMouse button: left
ModeReleased (Starting Mode)
(no Actions)
While the Left Mouse Button Is Held
WhenMouse HeldMouse button: left
ModeHolding
1Change Variable
Variable
handX
Value
valueMember(event.position,"x") - self.grabOffset
2Set Position
X
clamp(self.handX, @self.leftLimit, @self.rightLimit)
Y
@self.laneY

Releasing the button gives the cart back to physics, even when the pointer has moved outside it. Interruptions end the hold too. Losing focus or canceling a touch should leave the bench usable when you return.

These interruptions repeat the same release and Mode change. After attaching this Logic to both carts, you can try the first drag. The load:succeeded Event is preparation for the saved experiments you will build in a later chapter. Myth sends it after restoring an experiment. That Event releases an interrupted hold and starts listening on the restored cart. The cart can respond again.

Logic ComponentPlace Cart on Railcontinued
When the Left Mouse Button Is Released
WhenMouse ReleaseMouse button: left
1Release CartFunction
cart
self
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released
On Focus Lost
WhenFocus Lost
1Release CartFunction
cart
self
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released
On Touch Cancelled
WhenTouch Cancelled
1Release CartFunction
cart
self
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released
On Load:succeeded
Whenload:succeededCustom Event
1Release CartFunction
cart
self
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released
3Execute Script
Script
Capture Cart Press

Finally, the cart listens for the keyboard Command, Return Carts. The returnCarts Custom Event comes from the orange button you will build below.

Both use this cart’s lane values. Both finish in Released, ready for another press.

Logic ComponentPlace Cart on Railcontinued
When Return Carts Is Pressed
WhenCommand PressedCommand: Return Carts
1Return Cart HomeFunction
cart
self
homeX
@self.homeX
laneY
@self.laneY
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released
On Return Carts
WhenreturnCartsCustom Event
1Return Cart HomeFunction
cart
self
homeX
@self.homeX
laneY
@self.laneY
2Switch Mode
Event
While the Left Mouse Button Is Held
Mode
Released

The same Logic serves both carts, with each Object supplying the height of its own lane.

2.7 Connect the Two Lanes

2.7.1 Blue Cart

Attach Place Cart on Rail to Blue Cart. Attaching it supplies the Shared Variable rows on the body you already built. Select this attachment in the Object’s Logic workspace to compare their values.

The lane height belongs to this cart’s center. Keep the limits and home position with the cart too; the Logic reads them when it needs them.

ObjectBlue CartAddDelete
Transform
Bounding Box > SizeX 3.6, Y 0.9
Sprite
Z Index Within Object3
SpriteBlue Cart Art
Transform > SizeX 3.6, Y 0.9
Collider
Shape and Transform > SizeX 3.6, Y 0.9
Logic
Place Cart on Rail
Place Cart on Rail > Components > Collider (Collider)Collider
Shared Variables
homeX > TypeNumber
homeX > KindConstant
homeX > Value6.75
laneY > TypeNumber
laneY > KindConstant
laneY > Value10.975
leftLimit > TypeNumber
leftLimit > KindConstant
leftLimit > Value5.975
rightLimit > TypeNumber
rightLimit > KindConstant
rightLimit > Value20.875

2.7.2 Copper Cart

Copper uses the same behavior on the lower rail. Its laneY differs, so the lower cart stays below Blue instead of climbing into its lane.

Open Copper Cart in its Components workspace, with the Object itself selected. Under Properties > Variables, find laneY in Shared Variables and enter 8.2 in Value, keeping Kind as Constant. Leave Blue Cart’s laneY at 10.975. These are each Object’s values, with no extra Variable to create.

Keep the load Sprites already attached to Copper Cart, so they follow when the Object changes position. The load travels with its cart.

ObjectCopper CartAddDelete
Transform
Bounding Box > SizeX 3.6, Y 0.9
Physics > Mass10
Sprite
Z Index Within Object3
SpriteCopper Cart Art
Transform > SizeX 3.6, Y 0.9
Collider
Shape and Transform > SizeX 3.6, Y 0.9
Logic
Place Cart on Rail
Place Cart on Rail > Components > Collider (Collider)Collider
Sprite(additional component)
NameRear Load
SpriteWorkshop weight-2
Z Index Within Object6.04
Transform > PositionX -1.125, Y 0.825
Transform > SizeX 0.625, Y 0.95
Sprite(additional component)
NameMiddle Load
SpriteWorkshop weight-2
Z Index Within Object6.05
Transform > PositionX -0.375, Y 0.825
Transform > SizeX 0.625, Y 0.95
Sprite(additional component)
NameFront Load
SpriteWorkshop weight-5
Z Index Within Object6.08
Transform > PositionX 0.375, Y 0.9
Transform > SizeX 0.75, Y 1.1
Shared Variables
homeX > TypeNumber
homeX > KindConstant
homeX > Value6.75
laneY > TypeNumber
laneY > KindConstant
laneY > Value8.2
leftLimit > TypeNumber
leftLimit > KindConstant
leftLimit > Value5.975
rightLimit > TypeNumber
rightLimit > KindConstant
rightLimit > Value20.875

Run Lab now. Drag each cart and let go, then use Home to bring them back. The orange button is next.

2.8 Give Return Its Button

2.8.1 Listen for Control Press

Pressing empty space should not return the carts. Listen for Control Press remembers whether this button received the press before the pointer moves away.

After creation, its timer calls Capture Control Press to connect the button to its press listener. Load reconnects the restored control for saved experiments later. The result lives in pressSelected, ready for the response to read.

Both Logic cards declare that name because both need it on their holding Object. Build each declaration as shown. Once attached to Return Button, they use the same Per Instance value. The Script writes that value on the button; the response reads it. No wire between attachments is needed.

Logic ComponentListen for Control PressAttach to: Return Button

Remembers the exact control pressed, before the pointer can move. Responses run in native Mouse Press Events.

Attach toAny Object
PER INSTANCE VARIABLES
NameTypeStarting Value
pressSelectedBooleanNo
When Created
WhenCreate
1Start Recurring Timer
Timer Name
"controlInputReady"
Repeat Every (s)
0.02
Ring Once
Yes
On the Control Input Ready Timer
WhenRecurring TimerTimer name: controlInputReady
1Execute Script
Script
Capture Control Press
On Load:succeeded
Whenload:succeededCustom Event
1Execute Script
Script
Capture Control Press

2.8.2 Return Button Response

The orange button asks both carts to come home. Return Button Response checks that the button was pressed, then broadcasts returnCarts and plays the click.

Each cart hears that message and handles its own return. Home already reaches the carts through their Command Events, so the button’s keyboard Event supplies only the sound.

Logic ComponentReturn Button ResponseAttach to: Return Button

The orange button asks the apparatus to return. The button and Home request share one quiet click.

Attach toAny Object
PER INSTANCE VARIABLES
NameTypeStarting Value
pressSelectedBooleanNo
When the Left Mouse Button Is Pressed
WhenMouse PressMouse button: left
1Ifself.pressSelected == true
Then
2Trigger Custom Event
Custom Event
returnCarts
3Play Workshop ClickFunction
accepted
Yes
When Return Carts Is Pressed
WhenCommand PressedCommand: Return Carts
1Play Workshop ClickFunction
accepted
Yes

2.8.3 Return Button

The painted Return control now gets a working Object. Its Sprite supplies the orange face, while its two Logic attachments recognize the press and answer it.

Build Return Button and place it in Lab using the setup row. Both attachments belong on Return Button, the Object you can select separately from the chamber painting.

ObjectReturn Button
Transform
Bounding Box > SizeX 1.5, Y 1.5
Sprite
Z Index Within Object5
SpriteWorkshop return-button
Transform > SizeX 1.5, Y 1.5
Logic
Return Button Response
Listen for Control Press

The bench has a way home.

2.9 Play It!

Predict what will happen when you drag a cart diagonally and release it. Run Lab and compare.

  • Grab Blue Cart away from its center and drag it along the rail. That contact point stays beneath your pointer.
  • Drag up or down as well as sideways. Blue stays on its upper rail; Copper stays on its lower rail.
  • Drag beyond either rail end and watch the cart stop at its allowed limit.
  • Release after a quick drag. The cart rests where you left it, without a throw.
  • Move both carts away from home, then press Home to return them to their starting places.
  • Move them again. Press the orange Return button and both come home with a quiet click.
  • Press empty space. Neither cart returns. The dial and Space still supply no push in this chapter.

2.10 How It Works

The press chooses a cart. Holding places it. Release hands motion back to physics, and Return restores its starting place.

The hand and physics take turns owning movement. They never need to pull against one another. You can use that same handover for arranging a body before another experiment begins.

2.11 If It Didn’t Work

A cart follows without a press? Look for the starting Mode on the held Event in Place Cart on Rail. Released starts empty; Holding carries the placement Actions. Restore that arrangement and the cart waits for your hand again.

Copper jumps onto Blue’s rail? Compare Copper Cart’s laneY with the lower lane value on its Copper Cart Object card. Give Copper its lower lane value. Run again, keeping the shared Logic as it is, and the carts should stay apart.

Neither cart takes a press? Open the Cart Press Input Library and check its Enabled switch is on. Compare Advanced Wiring and Load Order with the Library card. Compare Capture Cart Press next. The Script connects the listener; Place Cart on Rail still owns movement.

Home works but the orange button stays silent? Look for Return Button in Lab, then check both its Logic attachments. Listen for Control Press supplies the selection that Return Button Response needs.

A cart stays held after release? Look at Mouse Release in Place Cart on Rail. It needs Release Cart and the change back to Released. Restore those together and try dragging again, watching for the cart to stop when you let go.

2.12 Challenge

Choose a different home position for one cart, inside its existing limits. Move it away. Return should bring it to the home position you chose on that cart. Keep the other cart’s home unchanged, then restore the card’s value before continuing.

You can arrange both carts now. Next, the workshop gives them a push.