Protocol for Using Classes


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 other classes. In Pro Script, a common interface exists for the creation, destruction, and use of classes. In the table below, we show how to manage a hypothetical class named CMyClass. In an actual script, CMyClass might be CImage, CRect, CTextView, another Pro Script class, or a class that you create.

To use a class in Lua, there is a standard progression of operations:

bullet.gif    Include the class definition

bullet.gif    Create an instance

bullet.gif    Use the class

bullet.gif    Delete the class

These operations follow a similar work flow in your scripts, as detailed in the table below.

Protocol for Working with a Class, "CMyClass"

To Create an Instance:

(the Constructor)

C = CMyClass:new()

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

bullet.gif    The () may contain optional parameters.

To Destroy an Instance:

(the Destructor)

C = CMyClass:delete()

bullet.gif    C is an existing reference to an instance of the class CMyClass.

bullet.gif    The () never contains parameters.

To use class methods or data:

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

C:Method() (returns nothing)

value = C.Data (Data is a value)

bullet.gif    Method is a "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 "variables"). 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, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.