Timing and Logic

The tiles in the Timing and Logic folder in your tile picker are designed to set up interesting conditions on the When side of your kode. There are some few tiles you can use on the Do side as well, but this set of tiles is clearly focused on conditions. Quite a few of these tiles provide functionality that is either unique to Project Spark, or at least quite different from the standard conditional, so let's take a look at them.

=General Logic Tiles=

Ignore line


The [ignore line] tile can only be placed as the first tile on the When side of a line. When used, this line and all child rules will be ignored. This is mostly useful if you want to disable a behavior without losing your kode.

Not


The [not] tile provides the opposite of the result of what comes after it. For example, [not] [true] would be equal to [false] and [not] [on ground] would be false when your object is on the ground, and otherwise true. This also works for non boolean checks. For example, [not] [Object Var] will be true if your object variable is set to nothing, and otherwise will be false.

Else


Another Kode concept is the “else” tile that you can use only for child lines (found on page 2 of the timing & logic tiles). Else is important because it’s another major language piece of Kode.

This is an example of the Else tile:

We have that same line 1 we had when we were talking about the parent/child concept. But now we have a new child line and it starts with an else. That means that except  When detecting the player, the prop will dance.

For each of


The [for each of] tile is one of the more interesting tiles in the timing and logic section. It can be followed by either a number, or an object set.

With an object set, this will cause the do side, and all child lines, to be executed once for every object in the set. During this execution, the current object in the set can be referenced with the [it] tile. The [current index] keeps track of how many times you go around the loop, starting at 1 rather than 0.

If you use a number, the loop will execute the number of times specified, but rounding number down to the nearest whole. The [current index] tile works here as well, but the [it] tile will not be set.

Current Index


The [current index] tile is related to the [for each of] loop. See above.

Once


The once tile can only be placed as the first tile on the When side of a line. When used, it lets Spark know that this line is only ever to be executed once. After this line has successfully executed once, it will never run again in the current brain. Note that different objects have their own brains, so even if two objects use the same brain, they will each have their own copy of it, and so the [once] tile can execute once for each of the objects.

Started​ To


The [started to] tile checks for when a condition starts to be verified. The frame that the condition specified becomes true, the [started to] tile will allow its do side and children lines to execute.

Please note: The condition on the [started to] does NOT need to be false for one frame before the tile can activate. So long as the [started to] tile didn't execute the previous frame, and its condition is true, the tile will activate.

For example: When: [started to] [on ground] Do: [jump]

This will cause your character to jump continually, even if they start on the ground.

No Longer


The [no longer] tile checks for when a condition is no longer met. The frame that the condition specified has changed from true to false, the [no longer] tile will allow its do side and children lines to execute.

Actually, [WHEN] "no longer", "condition" is exactly equivalent to [WHEN] "started to", "not", "condition"

Please note: The condition on the [no longer] tile needs to be true for at least one frame before the [no longer] tile can activate.

For example: When: [no longer] [falling] Do: [Jump]

This won't do anything at first if your character begins the stage standing on the ground. However, if you jump, then this will cause your character to endlessly jump as they land.

Page Entered
The [page entered] tile returns true if the page has not been run at previous frame but runs now (WARNING! it is not true if the brain itself has not run at previous frame but runs now). That means that you can use it if you want to run a code each time you enter a page either after switching to that page, or only the first time you call the page in a continuous call (one call every frame)

Example: Idle Villager default brain You only need to call an emote once (and not continuously during the whole animation length), it will stop after it's finished.

Until


The [until] tile activates so long as its condition has never been true. For example, When [until] [bump] [Tree] Do [Play sound] [bell] will cause a sound to be played until your object bumps a tree, at which point it will never play a sound again.

It is important to note that the Until tile can be reset if it isn't evaluated during a brain tick. For example:

When [A] When [until] [B] Do [Jump]

When you hold A, your character will jump continually until you hit the B button. If you let go of A though, and hit it again, then your character will start jumping again. This is because the [until] line lost its state when it wasn't being run, as so restarted its check when A was pressed again.

After


The [after] tile is the opposite of the [until] tile. The [after] tile activates after its condition is true once. For example, When [after] [hit by attack] Do [Play Sound] [grunt] will cause your character to grunt endlessly once they have been hit by an attack.

The [after] tile can also be reset if not run during an iteration.

=Timers=

General
Timers are some of the more surprisingly complicated tiles in Project Spark. They seem easy, but there are nuances to their use that can easily trip you up.

A timer has three states: Uninitialized, Timing and Time Finished, and this is how they work.

Before a timer has been evaluated the first time, it is uninitialized. This state is only useful so the timer knows when it should begin timing. When an uninitialized timer is evaluated, it switches to its Timing state, and marks its start time as the current time. The timer's state changes again when the time specified has passed since its start time.

Countdown Timer


The [countdown timer] tile evaluates to false (and therefore does not execute its rules) when it is in its Timing state, but evaluates to true when in its Time Finished state. Or, in plain english, the [countdown timer] tile executes its rules after the specified time is complete. Find more on countdown timer here.

Duration Timer


The [duration timer] tile evaluates to true (and executes its rules) when it is in its Timing state, but evaluates to false when in its Time Finished state. In other words, the [duration timer] executes its rules for the duration specified and then stops.

The reason the states needed to be mentioned is that a timer becomes uninitialized if it is not evaluated in a brain tick. This means if you go through the brain one iteration without running your timer line, the timer will start over from the beginning. This type of error is very common, so I'll provide an example.

When [A] When [countdown timer] [3] Do [Jump]

If you hold down the A button for two seconds, then let go, then hold it for two more seconds, your object will not jump. This is because as soon as you let go of the button, your timer was reset, going back to its uninitialized state. A very common error is to put a timer underneath a condition that can only execute for one frame, like [pressed] or [started to]. This will guarantee that your timer can never finish, so if your timer never seems to work, that is the first thing to check for.

The timers actually have quite a few modifiers that are of interest. You can find more information about using the countdown timer *here. The modifiers are the same between the two timers, so the information for the countdown timer should be applicable to the duration timer as well.

=Music Logic= Logic tiles that work when a music track is playing (using "set music" tile).

On music beat


The [on music beat] tile evaluates to true every music beat of the global song when there is no beat number specified. When a number is specified after [on music beat], like [on music beat][1], the action on the DO side will only execute on the next music beat.

On music measure


The [on music measure] tile evaluates to true every measure of the global song when there is no measure number specified. When a number is specified after [on music measure], like [on music measure][1], the action on the DO side will only execute on the next music measure.

=Logic Gates=

Tiles that connect two or more conditions in WHEN side.

Or


The [or] tile evaluates to true if either of its conditions are true. For example, When [A] [pressed] [or] [B] [pressed] Do [jump] will cause your character to jump when either the A or B buttons are pressed.

And


The [and] tile evaluates to true only if both of its conditions are true. For example, When [A] [and] [B] Do [jump] will cause you character to jump if you hold both A and B buttons.