Xtras for Sale

Home (Spotlight) | TOC | Products & Ordering | Technical Info | Contact Zeus


Zeus Tech Note
Object-Oriented Programming in Lingo

Copyright © 1997. Zeus Productions. All Rights Reserved.
Written by Bruce A. Epstein

Procedural vs. Object-Oriented Programming:

Typical Lingo scripting is procedural because you create procedures (or functions) to perform a particular task. A procedure typically performs a single operation. For example, a "multiply" procedure might multiply two numbers:
	on multiply a, b
	  return (a*b)
	end

To implement a "stop watch" using a procedural approach, we could use global variables to communicate between the various functions, as below (Note: this example is heavily simplified and not very robust).
	-- Reset the timer to zero
	on resetTimer
	  global gCurrentTime
	  set gCurrentTime = 0
	end resetTimer


	-- Start the timer running
	on runTimer
	  global gStartTime
	  set gStartTime = the ticks
	end runTimer


	-- Stop the timer (assumes timer has been reset and started)
	on stopTimer
	  global gCurrentTime, gStartTime
	  set gCurrentTime = (the ticks - gStartTime)
	end stopTimer
 

	-- Report the timer's value (assumes timer has been stopped)
	on reportTimer
	  global gCurrentTime
	  return (gCurrentTime/60.0)
	end reportTimer

We could use our timer to check how fast Lingo can perform 1000 iterations of a repeat loop:
	-- Test the timer functions
	on testTimer
	  resetTimer
  
	  runTimer
  
	  repeat with x = 1 to 1000
	    nothing
	  end repeat
  
	  stopTimer
  
	  put reportTimer()
	end testTimer

To implement two timers that operate simultaneously using the procedural approach, we would need to create additional global variables and handlers to avoid conflicts between the two timers.

Object-oriented programming (OOP) is an ideal alternative. (Lingo allows both procedural and object-oriented programming in the same file, so you can mix and match as needed). We can build a timer object, and create multiple instances of it, each of which operates independently. Each instance can maintain it's own properties, which are "global-like" variables that are private to the object. Properties can be used by any handler or method within the object, and persist over time, but they are not generally visible outside of the object (see the property keyword for exceptions), which reduces potential conflicts.

Objects contain on or more methods to perform the desired functions. For example, a "Timer" object might behave like a stop-watch with four buttons, each represented by a method:

To create a child object:
  1. Write the Lingo methods for the object.
  2. Place the Lingo in a Parent Script. (Create a Parent Script by changing the script type in the Script cast member info dialog box to "Parent").
  3. Create an instance (child object) using the "new" command.
  4. Use the instance handle returned by the new method to call the other methods.

Here is an object-oriented version of the previous example. This Lingo should be placed in a Parent Script cast member named "Timer".
	-- Cast Member "Timer"
	property pCurrentTime, pStartTime

	on new me
	  return me
	end


	-- Reset the timer to zero
	on resetTimer me
	  set pCurrentTime = 0
	end resetTimer

	-- Start the timer running
	on runTimer me
	  set pStartTime = the ticks
	end runTimer

	-- Stop the timer (assumes timer has been reset and started)
	on stopTimer me
	  set pCurrentTime = (the ticks - pStartTime)
	end stopTimer
 
	-- Report the timer's value (assumes timer has been stopped)
	on reportTimer me
	  return (pCurrentTime/60.0)
	end reportTimer


Notes about the object-oriented version of our timer:
The testTimerObj handler can be placed in a Movie Script and used to test the Timer object. Note that there are two separate instances of the timer (timer1 and timer2) running at once.
	-- Test the Timer child object
	on testTimerObj
  
	  set timer1 = new (script "Timer")
	  set timer2 = new (script "Timer")
  
	  resetTimer (timer1)
	  runTimer (timer1)
  
	  put "Testing the speed of the repeat loop"
  
	  repeat with x = 1 to 10
	    resetTimer (timer2)
	    runTimer (timer2)
    
	    repeat with y = 1 to 5000
	      nothing
	    end repeat
    
	    stopTimer (timer2)
    
	    put "5000 Iterations took" && reportTimer(timer2) && "seconds"

	  end repeat
  
	  stopTimer (timer1)
  
	  put "10 tests took a total of" && reportTimer(timer1) && "seconds"

	  set timer1 = 0
	  set timer2 = 0
    
	end testTimerObj


Child Objects are generally a good idea when you want to create:

... more to come....




Home (Spotlight) | Table of Contents | Links | Contact Info

Place an Order | Products for Sale | Licensing | Downloads

TechNotes | FAQs | E-Mail Zeus | GuestBook | Glossary

[End of Page]

Copyright © 1996-1997. Zeus Productions. All Rights Reserved.

(This document last revised August 1, 1997)