Counter

From MZXWiki
Revision as of 20:32, 29 January 2009 by Terryn (talk | contribs) (Fixed. The extremities don't have the same absolute value because 0 takes up a space.)
Jump to navigation Jump to search

A counter is essentially Megazeux vernacular for a variable, meaning a piece of data in the program whose value can change. Specifically in the case of MZX, counters are integer numbers, not strings, and not floating-point numbers. In standard programming terms, most counters are signed, 32-bit integers, meaning any number between -2,147,483,648 and 2,147,483,647. In the past, before the MZX port, counters were 16-bit, or between -32768 and 32767. However, some counters with built-in meanings and uses assume different ranges, or don't have actual ranges at all because they indicate special syntax instead of numeric values. For example, the counter "fwrite_open" doesn't correspond to any value itself, and instead provides an access point for opening a file. MZX has also changed over the years in terms of the number of counters it has allowed the user to define. As late as version 2.51, only 50 could be active (i.e. non-zero) at one time. Over time, this number was expanded, and then in the port the limit was effectively removed.

While most counters in a MZX game are user defined, MZX also provides a couple hundred built-in counters and counter-like constructs to access special functions and information. These are detailed by category in the help file, which is a very good resource for any MZX programmer.

Another important type of counter is the local counter. Most counters are global, meaning they can be accessed from anywhere in the game and by any robot. By contrast, local counters are specifically tied to an individual robot, so that the same name referenced or set by one robot will access a different value than another robot would. Robots on the same board can read each others' local counters, but can't write values to them. The most important local counters are named "local#", where # is an optional number between 1 and 32. All of these are technically built-in since their names are fixed, but most have no special function except to be local counters.