Articles Archive
Articles Search
Director Wiki
 

Me, my code and I

April 9, 2000
by Pat McClellan

Dear Multimedia Handyman

I am trying to study about Lingo and I am confused about "me". Could you explain to me what the purpose of that parameter is please?

Wittamon

Dear Wittamon,

That's a good question, considering that you see it in scripts all the time and almost nobody is very clear on why to use it. Part of its use is very practical while part of it is simply conventional.

Let's start with the word itself. There is nothing special or unique about the use of the word "me". There is no predefined Lingo meaning. You can safely substitute any other valid variable name such as "myName" or "foo" or "whatever". It is not a property of a sprite or an object, but rather just an easy way to refer to something. But a reference to what?

The use of "me" started long before there were behaviors in Director. More advanced Director users were using object oriented programming to create "instances" of "parent scripts." These instances are called objects, and you can think of them as a behavior that doesn't need to be attached to a sprite or frame. Each instance of an object (and now, each instance of a behavior) occupies a specific space in your computer's memory while it remains in existence. This "address" is kind of like the name for the object. For example, if you had a behavior named "meTester", its address looks something like this:

<offspring "meTester" 4 ba02e44>

And if you had a second sprite with the same behavior, its address might look like this:

<offspring "meTester" 4 9fee110>

You can see that if you wanted to communicate with one of those instances of the behavior (or object), it could get a bit wordy. Wouldn't it be much easier if you could just assign that long address to a variable? Sure. Let's call it "me". And let's create something for the behavior to do:

on mouseUp me
  put "me =" && me
  put "me.spriteNum =" && me.spriteNum
end

-- "me = <offspring "meTester" 4 9fee1b0>"
-- "me.spriteNum = 2"

And now just to prove that the variable name "me" was not imbued with special powers, let's change the behavior a bit, substituting "myName" for "me". It works just the same.

on mouseUp myName
  put "myName =" && myName
  put "myName.spriteNum =" && myName.spriteNum
end

-- "myName = " -- "myName.spriteNum = 2"

Now let's move onto another issue: do you need to use anything at all, me or otherwise? The answer is... sometimes. Press the "Beeper" button in the demo above. It beeps. Here's the behavior that's attached to that button sprite:

on mouseUp me
  put "beep"
  beep
end

Notice that there's no reference to "me" in there. In this case, there's really no reason to use it (but no particular benefit to not including it either.) So when would we really need to use it? Perhaps you need to do something to that sprite... move it, change the member of it, etc. In this case, you'll need for the behavior to be able to determine its spriteNum. We already did that in the demo above, where we said:

me.spriteNum

That can be read literally as "the spriteNum of me"... and more verbosely as "the spriteNum of the sprite to which this particular instance () of the behavior script is attached." Pretty quickly, you start to see the advantage of the shorthand.

If you happened to use the variable "me" in a handler where it was not declared at the top, it would be just like using any other variable that you had not assigned a value. You'd get the typical Lingo syntax error message:

I hope that this discussion about me -- I mean, about "me" -- has been helpful in your Lingo-learning process. Although you don't have to use the specific word "me", it is often helpful to be conventional. For example, many Lingo users (including myself) name all global variables starting with a small "g", and properties starting with a small "p". Again, you don't have to, but it sure makes reading the code a bit easier for you to read -- and easier for others to understand. And that's something we should all strive for.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.