How the brains work

==Before Reading==
Before we get into the major kode concepts, make sure you've played the tutorial level on Project Spark. This guided tour to Project Spark is necessary to help you learn the controls and the components of the game.
==What is "Kode"?==
In Project Spark, all logic happens inside the "brain" of every prop. By "brain", we mean a place where all the WHEN and DOs are stored. All these lines of WHEN and DO are called kode. The reason it is called kode instead of code, is because Project Spark is based on the evolution of Kodu, a simplified programming language published by Microsoft FUSE Labs in 2009.
thumb|left|523px
==When/Do==
The whole concept of WHEN/DO is something that spans in all programming languages. Ever since the evolution of coding languages in the 1950’s, the concept of “when” and “do” became integral concepts of code. They’ve been called and combined with other words like “if”, “while”, “until”, “else” and “for”, to name a few.
These commands in code are used to tell a computer system what to do, how to do it, and when to do it. Project Spark simplifies all these different buzzwords into just two: WHEN this happens, DO that thing
The WHENs and DOs contain a succession of Kode tiles, which must execute a specific action when read. Each tile has got a description detailing what it does and how to use it in kode.
==Execution Order==
A brain is run from beginning to end every frame, and every time a brain is run it is called a tick. The brain runs lines in sequence, and will always run line 1 before it runs line 2 if it runs both lines.
For each line that the brain goes to run, first the When side is evaluated, then, if the conditions on the when side are met, the do side is executed. When a rule is evaluated, if the conditions are not met, then the brain will not execute the Do side or any subrules, nor will it evaluate any subrules for that tick.
==Pages==
Each brain contains one or more pages. When it is time to run a brain in Spark, first the brain must know which page to run. Each brain keeps track of its [current page] which defines the page that the brain starts running from each tick. This can be changed through execution to change your object's behavior, or a page can be called while running another page for more complex behaviors.
==Object Ordering==
Project Spark does not currently guarantee the order that the object's brain will run in a tick. The brains will run one at a time, and in a single play session the brain's are guaranteed to continually execute in the same order but if you restart the game, the order of your next play session may be different. This can be important if you have different brains reliant on each other. Your kode should be able to handle the order of execution being different.
==Default Behaviors And Modifiers==
A single tile in Spark can perform surprisingly complex tasks. For example, the shoot tile will cause your character to shoot a fireball directly ahead which will destroy itself on contact or after a preset amount of time. In order for shoot to be easy to use, all that information is set by having smart defaults for the shoot tile, but the default isn't always what you want in your game.
After the shoot tile, check out the modifiers folder in the tile picker to really customize how your shooting works. Most modifiers require a specific piece of information to function. For example, the [in direction] modifier requires a vector, whereas the [at speed] modifier needs a number. However, some modifiers, like the [disallow friendly fire] modifier, when specified, immediately causes your shots to be unable to harm your allies.
Not all modifiers are found under the modifiers folder. For example, the shoot tile allows you to select an object to shoot. There is no modifier for [shoot this object], it simply knows that if you specify an object after the shoot tile, that is what you want to shoot. Make sure you check out each tile's description to get a better understanding of what you can make it do.
==Read Next==
Variables
==Further reading==
Timing and Logic
Brain Tiles
< Back to Koding Curriculum 
fr:Comment_les_cerveaux_fonctionnent
Category:Brains