Code Tab Guide

The Code tab is where the code for the model is stored. Commands you only want to use once can be typed in the Command Center, but collections of commands that you want to save for later use can be stored in the Code tab. Screenshot

Table of Contents

Many Code tab behaviors can be changed by using Code preferences. These preferences can be accessed via the “Preferences” button in the Code tab toolbar, or the Code Preferences tab of the NetLogo Preferences dialog which is opened with the Tools menu “Preferences” item on Windows and Linux, and with the NetLogo menu “Settings” item on MacOS.

Code Tab Toolbar

Details for these items can be found in the corresponding sections.

Procedures menu

This menu includes a list of all the procedures in the Code tab. Clicking on a procedure moves the cursor to that procedure. Typing in the text box at the top will filter the list, only showing procedures whose name contains the typed text. By default procedures are listed alphabetically, but this can be changed to their order of appearance by using the “Sort order of procedures menu” code preference.

Checking for Errors

To determine if the code has any errors, you may press the “Check” button. If there are any syntax errors, the Code tab will turn orange, the code that contains the error will be highlighted, the cursor will move to the error location, and an error message will appear in an orange banner at the top of the code area. If you don’t want the cursor to move when there is a compilation error (for example when there is more code than fits on the screen) uncheck the “Auto-focus text or tab with errors when model is compiled” preference. The error will not be highlighted. If the error occurs in the Interface tab, for example if any button or plot commands result in a compilation error, the Interface tab will turn orange instead.

Screenshot

Switching tabs also causes the code to be checked, so if you just switch tabs, pressing the Check button first isn’t necessary.

Separate Code tab

The Code tab can be opened in a separate window from the main NetLogo window. It can then be moved and resized.

Find & Replace

To find a fragment of code in the Code tab, click on the “Find” button in the Code toolbar and the Find dialog will appear. Screenshot

You may enter a word or phrase to find, and optionally also a new word or phrase to replace it with. The “Ignore case” checkbox controls whether the capitalization must be the same to indicate a match.

If the “Wrap around” checkbox is checked, the entire Code tab will be checked for the phrase, starting at the cursor position. When it reaches the end it will return to the top, otherwise only the area from the cursor position to the end of the Code tab will be searched. The “Next” and “Previous” buttons will move down and up to find another occurrence of the search phrase.

“Replace” changes the currently selected phrase with the replace phrase and “Replace & Find” changes the selected phrase and moves to the next occurrence. “Replace All” will change all instances of the find phrase in the search area with the replace phrase.

Automatic Indentation

When the “Auto indent” Code preference is selected, NetLogo will automatically attempt to align your code in a logically structured format. For example, when you open a set of square brackets “[” (perhaps after an if statement), NetLogo will automatically add spaces so that the following lines of code are two spaces further indented than the bracket. When you close the square brackets the closing bracket will be lined up with the matching open bracket. Screenshot

NetLogo will try to indent the code as you type, but you can also press the tab key anywhere on any line to ask NetLogo to indent the line immediately. Or, you can select a whole region of code and press the tab key to re-indent all of it.

Line Numbers

By default line numbers are shown on the left edge of the code area. They can be removed by unselecting the “Show line numbers” Code preference.

More Editing Options

To find a particular procedure definition in your code, use the “Procedures” popup menu in the Code tab. The menu lists all procedures in the order they appear in the file. To search for a particular procedure, use the search field at the top of the popup. It will filter the list of procedures using fuzzy matching as you type. Fuzzy match will include procedures whose names contain the characters that you type in the order that they appear in the search field, but the characters do not have to be right next to each other. For instance, “fnn” will match"find-nearest-neighbors" and “wolf-down-neighbor” but not “nearest-wolf-neighbor”. You may use the arrow keys or mouse to select a particular procedure and enter or click to jump to it. This can be a very convenient way to navigate your file. To facilitate this, you can use the shortcut ctrl-g (cmd+g on Mac OS) to access the procedures menu.

The “Shift Left”, “Shift Right”, “Comment”, and “Uncomment” items on the Edit menu are used in the Code tab to change the indentation level of your code or add and remove semicolons, which mark comments, from sections of code.

For more information about writing procedures, read Tutorial #3: Procedures and the Programming Guide.

Included Files

Caution: The includes facility is still experimental.

If you have a file with the extension .nls all of its breeds, variables, and procedure definitions are available in the model if you add it by using the __includes keyword to a model. When that keyword is used the “Included Files” appears menu to the right of the procedures menu appears. This menu lists all the NetLogo source files (.nls) included in this model. You can choose a file name from the menu to open a tab for that file, or you can open a new or existing file using the “New Source File” or “Open Source File” items .You can make this menu always visible using the “Always show “Included Files” menu” Code preference.

Once you’ve opened new tabs they become accessible from the Tabs menu, and you can use the keyboard to move from tab to tab (Command + number on Mac, Control + number on other operating systems). If the main Code tab is in a separate window, the tabs for included files will appear there too. When an included (.nls) file tab is selected it becomes blue. An included file tab can be closed by clicking on the “x” on the right side of the tab. Each included file tab has a toolbar indentical to the main Code Tab’s toolbar; the options in this toolbar apply to all Code tabs, not just the one that is currently visible.

Note that you can create or open an .nls file before including it in a model with an __includes statement. In this case, the contents of the file will not be compilable, which is indicated by a warning label below the toolbar and by the Check button being disabled. This does not affect the rest of the model or cause any errors, but the contents of the file will not be accessible from any other code tabs in the model.