Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Version History

« Previous Version 7 Next »

High Level Constructs


unit

Units are groupings of functions and also variables.

They can be thought of as modules or namespaces. A Mezzanine script must have at least one unit.Unit must be followed with a unique unit name. Units are defined as follows.

 

  1. unit MyUnit;
  2. // Other code follows
  3.  
  4. string myVar;
  5.  
  6. int factorial(int x) {
  7.     if(== 0 || x == 1) {
  8.         return 1;
  9.     } else if (> 1) {
  10.         return factorial(- 1);
  11.     }
  12.  
  13.     return 0;
  14. }

HEADS UP Objects, enums and validators are global, so they can optionally be in a separate source code file without a unit.

 

Mez File Comments

Two types of comments are supported

Single line comments

 

  1. // date tstamp = System.now();
  2. // decimal rand = System.random();

 

And multi-line comments:

 

  1. /* date tstamp = System.now();
  2. decimal rand = System.random(); */

 

 

 

 

 

This unit has one unit variable named myVar. This variable has unit scope and can be accessed by any function in the unit. The unit also has a function named factorial. A unit must have at least one function. To refer to a unit’s member functions or variables from another unit, you have to use the scope operator

 

  1. unit TestUnit;
  2.  
  3. void test() {
  4.     MyUnit:myVar = "New string value!";
  5.     int f = MyUnit:factorial(5);
  6. }

 

Note: Unit variables cannot be in line initialized when they are declared. The following code will not compile

 

  1. unit MyUnit;
  2.  
  3. string myVar = "some value"; // this is not a legal statement

 

 

  • No labels