Variables, Constants, and Settings in Scripts

There are four basic types of variables you can use in script.

script variables

These are the usual in-script variables. They follow the normal rules and usage of your scripting language.

mapset/module variables

These are variables that are stored in the mapset or module associated with the script.

<pre> fooValue = mapset.getVariables().get(“foo”); mapset.getVariables().put(“foo”, fooValue); </pre>

<pre> fooValue = module.getVariables().get(“foo”); module.getVariables().put(“foo”, fooValue); </pre>

These are good for storing things while the program is open, but doesn’t save them between sessions. They are also useful for sharing objects between scripts or between different runs of the same script.

script constants

Constants are really just a special case of script variable. They are generally used when there is some behavior or value that doesn’t need to be configurable but might have to get modified occasionally during development or troubleshooting - for example, to turn on debugging code or to set the length of a data record.

<pre> // constants PATH_LENGTH = 53; DEBUG = false; </pre>

mapset/module settings

When it’s time to reuse the same code for multiple customers and you need to change something on a per-customer basis, use config variables.

Create a settings.xml file in either: * config/FOO/mapsets/MAPSET/settings.xml, or * config/FOO/modules/MODULE/settings.xml.

They work the same way, just with module or mapset as desired:

<pre> pathLength = mapset.getSettings().get(“pathLength”); </pre>

or

<pre> pathLength = module.getSettings().get(“pathLength”); </pre>

Each time you call getSettings() the platform checks to see if the file has changed and reloads it as needed. Thus, you don’t have to restart the platform every time you modify settings.xml.

Here’s an example of the XML. Note the integer type - important to avoid needing to use ConversionLib. <pre> <?xml version=‘1.0’?> <cog>

<Naming>
<Integer name=’pathLength’>
53

</Integer>

</Naming>

</cog> </pre>

The “Parse” button on the Workbench text editor will check for XML issues.

Table Of Contents

Previous topic

Scripting

Next topic

Operating Systems

This Page