Using Classes in your Scripts
A class is made from a Lua table using a special protocol (see Creating Classes). This topic gives an overview of the syntax you use to work with the class in Pro Script. The details are given in the Creating Classes topic.
To begin using a class, you must instantiate it—that is, you must create an instance of the class. An instance of a class is an object that exists in memory and is independent of other instances of the same class and all other classes. There is a common interface for the creation, use, and destruction of classes. The table below shows how to manage am arbitrary class named CMyClass. In an actual script, CMyClass might be CImage, CRect, CTextView, another Mira Pro x64 script class, or a class that you create.
To use a class in Lua, there is a standard progression of operations:
Create an instance of the class. For example, the statement Im = new_image() creates a new instance Im, which is a class object of the CImage class.
Use the class methods (functions) and properties (values).
Delete the class object. Every class object defined in the Mira Pro x64 Script Module is automatically deleted when the script ends.
These steps apply to every class defined in the Mira Pro x64 Script Module (as described in this User's Guide). If you create your own class, then you must first define the class before creating an instance of it.
Finally, note that you can create multiple instances of any class, with each instance being independent of the others. For example, you might define 3 instances of the CImage class with one of the instances initially being a copy of the first instance, as in Im1 = new_image(), Im2 = new_image(Im1), Im3 = new_image(). In this example, Im2 becomes independent of Im1 immediately after copying Im1.
Required Steps for Working with a Class
Creating an Instance using a Global Function: (the Constructor) |
C = new_class() where class is the name of the target class. For example, C = new_image() creates a new instance of the CImage class. C is returned as a reference to a new instance of the class CMyClass. The () may contain optional parameters. |
Creating an Instance using a Class Method: (the Constructor) |
C = CMyClass:new() where CMyClass is the name of the target class. For example, C = CImage:new() creates a new instance of the CImage class. C is returned as a reference to a new instance of the class CMyClass. The () may contain optional parameters. This is a global function alternative to C = new_class() |
Destroying an Instance: (the Destructor) |
C = CMyClass:delete() where CMyClass is the name of an existing reference to an instance of the class CMyClass. The () never contains parameters. |
Accessing class methods or data: |
value = C:Method() (returns a value) C:Method() (returns nothing) value = C.Data (Data is a value) Method is a "function" inside CMyClass. Data is a data member (variable or value) inside CMyClass. |
|
A Class contains both Methods ("functions") and Properties (data, or values). Notice in the table above that we use both . (dot) and : (colon) operators to access the class members. This is described in Creating Classes.
To view a simple example using the methods described above, see Writing a Simple Script.
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics,
Inc. All Rights Reserved.