Creating Classes Going Further with Lua

Creating a Simple Script


A great way to learn how to write scripts is by using the Script Manager to view the sample scripts supplied with your Mira installation. You can also setup a productive learning environment by opening the Script Editor on one side of the screen and a Mira Text Editor on the other side of the screen. Text messages can be sent to the Editor as a result of adding or changing the script, then running it from the Script Editor. In fact, you should do exactly this when you read through the example below. Create and run the script provided below. Then add features, change the code, and re-run your changes.

Example

In the script below, we create a Mira Text Editor window with the window caption "My Text Window". Then we use a few commands to create some values and print them in the window. The script finishes by deleting the class from memory. This simple script uses these elements:

bullet.gif    Class CTextView

bullet.gif    Global dialog function GetNumber

bullet.gif    Lua math package (see the Lua documentation for a description of the math package).

bullet.gif    Class method CTextView:Printf to perform C-style formatted text output.

bullet.gif    --(green) is used for inline comments.

The example below provides a complete script. If you want to run this script, paste the plain text version into the Script Editor.

 

T = new_textview("My Text Window")

-- create an instance of class CTextView

n = math.sqrt( GetNumber(100) )

-- get a number from the user, default to 100

f = n + math.log( n/55.4 )

-- do some math

s = "My String" .. " goes here."

-- concatenate two strings

T:Printf("Val=%.2f\r\nStr= '%s'\r\n", f,s)

-- format f and s to the Text Editor.

      -- (do more things here)

 

T:delete()

-- do when completely finished using T

This script has 4 main parts:

  1. The first statement creates a new instance of the class CTextView which refers to a Mira Text Editor window. The CTextView class provides methods for working with its window. The new method assigns a name to the window and returns a reference to the new CTextView object. The script assigns this reference to T. To work with this object later in the script, use a syntax like T:MyMethod() or T:MyData . You could create a second text window using a syntax like T2 = CTextView:new("My Second Text Window")

  2. Next, the script computes the square root of a number you enter. The math package method math.sqrt() does the calculation on the number entered by the user. This number is obtained using the GetNumber function which opens a dialog to request the value. The square root of the returned value is assigned to n. In the C and C++ languages, we would think of n as being a "variable" which must be declared at or before it is first used. In Lua, it is also a variable but is created by being assigned a value (number, string, etc.). In this casen becomes a number (actually, a double precision floating point value). The next statement, does some computations using n and assigns the result to a new variable,f. The following statement then defines a string by assigning a string value tos.

  3. Next, the script calls the CTextView class method Printf to print the results. This function sends C-style formatted text to a Mira text window. The format string might be very simple or very complex (we have made it somewhere in between). If you have a simple need to send text to a text window, you could forget about using CTextView and, instead, use the global Printf method to accomplish something similar.

  4. Finally, since the script is finished using T, the method T:delete is called to free the CTextView object from memory. If you forget to delete the object, Mira will do it for you. However, it is a good practice to get used to doing it explicitly, as it may be important in more complicated scripts.

Same Script Without the CTextView Class

The simple script above does not require the CTextView class to accomplish the same thing. When you simply want to send results to some Text Editor window, you can simplify things. In this case, we can remove 3 lines from the script and use the global Printf function. The following script will give exactly the same results as above:

n = math.sqrt( GetNumber(100) )

-- get a number from the user, default to 100

f = n + math.log( n/55.4 )

-- do some math

s = "My String" .. " goes here."

-- concatenate two strings

Printf("Val=%.2f\r\nStr= '%s'\r\n", f,s)

-- format f and s to the Text Editor.

In Summary

As you can see, the script syntax is in some ways very much like the C and C++ languages. In fact, Lua has greatly simplified the syntax of C and C++ to deliver a similar result. For example, a Lua script creates a string simply by assigning a string expression to a value. In particular, you might note the lack of the C-style ; character at the end of each statement. In Lua, the ; delimiter is optional and you can use it according to your own programming style. It may be useful as a separator when you place multiple statements on the same line, as in w=I:Cols() ; h=I:Rows(), but even in this case, Lua parses the line as containing 2 statements, and the; separator is merely useful to help make the script easier for you to read.

Related Topics

Script Editor Window, Overview of the Mira Pro Script Module


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