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:
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()
|
To Destroy an Instance: (the Destructor) |
C = CMyClass:delete()
|
To use class methods or data: |
value = C:Method() (returns a value) C:Method() (returns nothing) value = C.Data (Data is a value)
|
|
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.