Lua Scripting for Ecotect

Instructor: Matthew M. Burke
Summer 2008

Lesson 1: Exercises

In the lecture, we examined a script that retrieved information about a model's zones and printed it out to the screen. The source for the script is available here. In this series of exercises, you will add functionality to the script.

  1. Rather than hard-coding the filename of a model into our script, let's allow the user to choose. You will need to make use of getUserFile. You may find that your script occassionally generates an error: dde: Ecotect could not process command. This seems to be a timing issue with the file dialog. If you insert pause(50) after you use getUserFile and before you load the model, the error is resolved.

    Note: you do not need to use getcwd as the file selection dialog provides the complete path to the file (You can verify this by printing out the value returned from the file dialog.).

  2. Now modify the script so that it prints information about the objects in a model, rather than the zones. get('model.objects') returns the number of objects in the model. Objects do not have names and colors, but they do have types and are composed of a certain number of nodes.

    Tip: You might want to switch back to a hard-coded model name while debugging.

  3. Now do nodes. Decide for yourself what properties to display.

    Warning: Models can contain a lot of nodes. While debugging your script, you might want to limit the for loop to the first 4 or 5 nodes.

    Tip: If you want to get fancy, consider using the Lua min function to limit yourself to 5 nodes or the number of nodes in the model, whichever is smaller.

    Challenge: A node's position is a table of three numbers indexed by they keys 'x', 'y' and 'z'. Thus getNodeData returns a table whose value at index 'pos' is a table. Modify your script so that it prints out the positions of (some of) the nodes in a model.

  4. The table-indexing expression table.key is equivalent to table["key"]. One advantage to the latter expression is that we can use a variable in place of the string to specify the index into the table. For example, in the sample script, we could replace zone_data.name with zone_data["name"]. Or even

    index = "name" zone_data[index]

    Now the function getUserInput allows us to retrieve input from the user. Use these two items to modify the sample script so that the user can specify what zone parameter to display, e.g. name or color or activity or ...

  5. We need to tidy up the sample script. The ten-dash underline in the header should be changed so that it matches the length of the line above. You will probably want to use strlen to accomplish this.


Copyright 2008 Matthew M. Burke. All rights reserved.
Modified: 30 June 2008