Comparison with C and C++ Script Classes

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:

  1. 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.

  2. Use the class methods (functions) and properties (values).

  3. 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)

Object = new_class()

bullet.gif    where "class" is the name of the target class. For example, MyClass = new_image() creates a new instance of the CImage class named Object.

bullet.gif    Object is returned as a new instance of the class.

bullet.gif    The "()" may contain optional parameters.

Creating an Instance using a Class Method:

(the Constructor)

Object = CMyClass:new()

bullet.gif    where CMyClass is the name of the target class. For example, Object = CImage:new() creates a new instance of the CImage class.

bullet.gif    Object is returned as a reference to a new instance of the class CMyClass.

bullet.gif    The "()" may contain optional parameters.

bullet.gif    This is a global function alternative to Object = new_class()

Destroying an Instance:

(the Destructor)

CMyClass:delete()

bullet.gif    where CMyClass is the name of an existing class object of type CMyClass.

bullet.gif    The "()" never contains parameters.

Accessing class methods and data:

value = MyClass:Method() (returns a value)

MyCLass is an instance of the CMyClass class.

MyClass:Method() (uses the colon operator)

MyClass.Data (uses the dot operator)

bullet.gif    Method is a class method ("function") inside CMyClass.

bullet.gif    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.

Related Topics

Contents

Creating Classes

Script Classes


Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.