Static Scripts

From Director Online Wiki
Jump to: navigation, search

The term Static Script is often used to describe an uninstantiated Movie Script that stores values as properties. This is an advanced technique which takes advantage of the fact that script members of all types are objects, and that objects can store properties.

Since a Movie Script member belongs to a particular cast, only movies that use that castLib have access to it. A Static Script can therefore act as a source of variables which are limited to the scope of a particular movie.

For example, you may be building an application where the user can open a number of separate documents in different windows. You may have an undo feature. The undo action will depend on which document (and thus which window) it is applied to. If you use a global variable to store the undo data, it will be shared between all windows. If you use a Static Script to store the undo data, each window can store its own data separately. Note: For this to work the Static Script must be in an in an internal cast (see explanation below).

Sharing Static Scripts between Movies

If your Static Script is in an external cast? which is shared between movies, all those movies can access the data stored in the Static Script.

Example Script


-- STATIC VARIABLES --
-- Movie Script
--

-- This Movie Script object is used to store values which are required
-- by a number of different scripts in this Movie.  The values are
-- maintained so long as this movie remains in RAM space, and are
-- accessible only to movie who share this castLib.
--
-- This technique allows you to create the movie-wide variables,
-- without encroaching on global name-space.  It is particularly
-- useful for Movies In A Window (MIAWs), especially if this script is
-- stored in an internal castLib.
--
-- No instance of the script is created.  The values are available
-- using the syntax...
--
--   GetValue(#propertyName)
--
-- ... and they can be set using the syntax...
--
--   SetValue(#propertyName, propertyValue)
--
-- This script will not function if:
-- - The name of the script does not correspond to the names used in
--   the SetValue() and GetValue() handlers
-- - a lower-numbered cast member has the same name, but no script
-- - a lower-numbered Movie Script member has the same handlers
----------------------------------------------------------------------
on SetValue(aPropertyName, aPropertyValue) ---------------------------
  -- INPUT: <aPropertyName> should be symbol, but other data types
  --         may work
  --        <aPropertyValue> can be any Lingo value
  -- ACTION: Creates an arbitrarily-named property in this Movie
  --         Script object and attributes a value.  The GetValue?()
  --         handler can subsequently be used to retrieve the value.
  --------------------------------------------------------------------
  script("Static Variables")[aPropertyName] = aPropertyValue
end SetValue
on GetValue(aPropertyName) -------------------------------------------
 -- INPUT: <aPropertyName> should be symbol, but other data types
  --         may work
  -- OUTPUT: the value of <propertyName> as stored by this Movie
  --         Script object
  --------------------------------------------------------------------
  return script("Static Variables")[aPropertyName]
end GetValue

Performance

Using the SetValue() and GetValue() is about 10 times slower than using a global variable. Using an uninstantiated script is between 2% and 6% slower than using an instance.

Demonstration

You can download a demonstration of the above script for Macintosh and Windows. Note that the Windows version is set to read-only so that you can open it in one or more MIAWs.