How does my program talk to my bot's parts?
Find your channels
Open Brain code and expand the Machine section under the editor (channels and performance). It lists each part on your machine, its channel, and the exact line that connects to it in your language. The chassis is always channel 0. Mounted parts follow in mount order, skipping empty mounts, so the standard loadout puts the scanner on 1 and the laser on 2.
Connect, then loop
from bot import Chassis, Scanner, Laser, wait
# Connect to each part on its channel, once, at the top.
chassis = Chassis(0)
scanner = Scanner(1)
laser = Laser(2)
while True:
if scanner.any:
chassis.forward = 1 # a command: holds until changed
else:
chassis.forward = 0
if not laser.present:
log("no laser on channel 2")
wait() # commands out, next tick, fresh readings in
Create each part at the top level of the program. Creating one inside a loop or a function is a compile error.
Commands and readings
- Commands tell a part what to do:
chassis.forward = 1,scanner.sense = 90. A command holds until you change it, so you don't need to repeat it every tick. A few are one-off actions instead, such aslaser.fire(b): a shot happens once. - Readings tell you what a part saw:
chassis.heading,chassis.hull,scanner.ping(0). All readings between twowait()calls come from the same moment.
The Reference at the bottom of Brain code lists every part's commands and readings for your language, with examples.
When a part isn't there
If a channel holds nothing, or a different type of part, the part isn't present: it reads 0 and ignores commands. Your program still runs, but that part does nothing. Brain code warns you when this happens, for example "The program connects a RNG-1 on channel 3, but this machine has nothing there". Fix the channel number in your code, or change the design in the Factory. Check part.present if your program should cope with machines that differ.
Under the hood
Each part is a bank of 32-bit registers on the bot's CPU. The compiled code copies fresh readings into the banks, copies your commands out, and sends them once a tick. In assembly you do this yourself with POLL, IN, OUT and SEND. The other languages spell the same calls their own way: in Java, for example, it's chassis.setForward(1) and waitTick().