DarwinBotsManual
Open the app EspañolES

Variables with def

How to give a name to a memory address or to a number with def, how those names are resolved, and what traps they have.

Writing 50 inc to count something works, but by the third loose address you no longer know what each one was. The def line gives a name to a number: afterwards, anywhere in the bot, you write .name and the loader swaps it for that number.

' A counter in a variable of your own
def pasos 50

cond
start
.pasos inc
stop

This bot adds 1 to cell 50 every cycle: .pasos inc is exactly the same as 50 inc. We checked it by running it: after five cycles cell 50 holds 5.

How it's written

The form is def name number, alone on its line:

Put the def lines at the very top, before the first gene, for a reason that follows.

How names are resolved

The loader reads the file from top to bottom and translates each word at the moment it reads it. When it finds .something:

  1. It looks for a sysvar called something, ignoring case: .UP, .Up and .up are the same one.
  2. Then it looks among your def lines already read, this time respecting case: .Pasos is not .pasos.
  3. If there's a match on both sides, your def wins. If two def lines have the same name, the last one wins.
  4. If it finds nothing, .something is 0. There's no error or warning on load.

Two practical consequences:

A name “not existing” and being 0 seems harmless, but it isn't: in a store, address 0 does nothing, and in a read *0 reads cell 1000. A typo in a name leaves the gene writing nowhere (see Numbers and addresses).

A def is just a number

The loader doesn't know whether your def is an address or a constant: it stores a number and nothing more. That lets you use def for both. This bot uses reloj as an address and giro as a constant:

' Turns a quarter turn every 20 cycles
def reloj 60
def giro 314

cond
start
.reloj inc
stop

cond
*.reloj 20 >=
start
0 .reloj store
.giro .aimdx store
stop

.reloj inc increments cell 60; *.reloj reads its value; .giro pushes the number 314 directly, which is what gets stored in .aimdx. When you run it, cell 60 counts from 0 to 19, goes back to 0, and in that cycle the bot's .aim drops by 314 (a quarter turn to the right, because the full turn is 1256).

LoveBot F2, by Moonfisher, uses the same trick to leave a “key” for its children in genetic memory:

' Fragment of LoveBot F2 (Moonfisher, 2008)
def original 971
def origkey 1234

cond
*.robage 2 <
start
.origkey .original store
stop

original is an address (971, which is inherited: see Free and epigenetic memory) and origkey is a constant. The gene stores 1234 in 971 during the first two cycles of life.

Since it's just a number, a def can be anything within ±32767. If you use it as an address and it falls outside 1..1000, it gets adjusted just like any address (see Numbers and addresses): def x 1050 and def x -50 both end up at cell 50. The value has to be a number written with digits: def x .up doesn't copy the address of .up, it's 0. The The DNA editor flags it.

A def that overrides a sysvar

If you give your variable the same name as a sysvar, your def wins throughout the bot. Here .up stops being the move-forward command:

def up 50

cond
start
10 .up store
stop

The bot doesn't move: the 10 goes to cell 50, not to cell 1 (.up). And since def names are case-sensitive and sysvars are not, in that same bot .UP is still the real sysvar. It's a sure source of confusion; the app's lint flags it as a warning. Pick names that don't clash with the list of sysvars.

Names with no checking

The loader doesn't check the name. Dots, digits or symbols are all fine: def a.b 70 or def 5 70 define legal variables, used as .a.b and .5. Watch out for the last one: .5 without a def is not address 5, it's 0 (addresses go without a dot).

Stranger still: any line that starts with the letters def is taken as a def. The loader discards the first four characters and reads what's left. A line like this one:

defensa 50

defines a variable called nsa with the value 50. If a bot ever does inexplicable things, check that no line starts with “def” by accident. The editor also warns you when a line is read as a def.

use: a dead word

Some old bots from the forum include lines like use NewMove. In this version use does nothing special: each word is read as ordinary DNA and, since they're neither commands nor numbers, they're 0. If the line comes before the first gene, those zeros never run; if it's inside a gene, they push zeros. You can delete them without worry.

Limits and errors that prevent loading

CaseWhat happens
def x without a number, or just defThe whole bot fails to load.
Number outside −32768…32767 (def x 40000)The whole bot fails to load.
More than 1000 def linesThe whole bot fails to load.
Two def lines with the same nameLoads; the last one wins.
def with a number outside 1..1000Loads; used as an address, it's adjusted to 1..1000.

Quirks inherited from 2.48.32

Two curious behaviors of the original, one preserved and one fixed:

Finally, the names exist only in your file. When the simulation saves a bot as .txt (see The .txt format), the DNA comes out already translated: every .pasos appears as 50, with no def lines.