Counter

From MZXWiki
Revision as of 16:49, 9 February 2009 by Terryn (talk | contribs) (Adding info about status counters. That should be enough info for this page.)
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 or write to each others' local counters, as long as it is specified which Robot's counters are being accessed (this is done by prefixing the counter name with "r<robot_id>." e.g. reading the first Robot's "local" counter is done by reading "r1.local"). The most important local counters are named "local#", where # is a number from a range of 32 numbers (usually local1-local32 or local0-local31); "local1" can be represented by just "local". All of these are technically built-in since their names are fixed, but most have no special function except to be local counters. (Other local counters exist - loopcount, bullettype, and lava_walk - but they are tied to actual Robot functions and can have other imposing limits, such as a range of only 0-256.)

When MegaZeux executes a swap world, all non-local counters are retained; all local counters are reset.

In older versions of MegaZeux, it was a fairly common practice to designate certain counters as status counters; making a counter a status counter meant that the counter name and value would be shown in the default status menu if the value was not 0. Up to six counters at once can be status counters. With the added ability to remove and replace the default status menu, this fell out of use.