Best Practices
Cubist at aol.com
Cubist at aol.com
Sun Nov 6 15:12:41 EST 2005
sez esmale at welshpiper.com:
>* Functions: From what I read, a function can be called by any other
>handler in a given script. Is it therefore best to define functions at
>the beginning of a script, before any event handlers? If I want to open
>a function up to the whole application, should I define it in its own stack?
The short answer: As far as Rev is concerned, it makes no difference where
a function is in its script. As well, if you want your function to be usable
by anything in a stack, that function should go into the script of the stack
itself.
The longer answer: When you use a function in a script, Rev starts out
looking for the function in that script. If the function isn't there, Rev moves
up the message-passing hierarchy until it finds a script that contains the
function.
As an example, let's say that you've got a button named "Fred", which
lives on a card named "George" in a stack named "Zelda". Let's say that button
"Fred" has a mouseUp handler which uses the function SuperTweak. When you click
on button "Fred", Rev will first check the script of "Fred" to see if the
SuperTweak function is in it. If SuperTweak is there, great -- Rev will happily use
the SuperTweak function in button "Fred". But if SuperTweak *isn't* in the
script of button "Fred", Rev moves up the message-passing heirarchy -- which, in
this case, means it looks in the script of card "George". If Rev finds
SuperTweak in the script of card "Fred", it uses that SuperTweak and all is well; if
not, it's time to move up the message-passing hierarchy once again, which
means looking in the script of stack "Zelda". If Rev finds SuperTweak in the
script of stack "Zelda", great! Rev then uses that SuperTweak. But if SuperTweak
isn;t even *there*, well, that's the end of the line -- there's nowhere else in
the message-passing hierarchy to *go* -- so Rev will put up a dialog box on
the screen which complains that Rev doesn't understand "SuperTweak".
At this point, it is worth noting that there is something called a
"backscript" which can complicate matters. However, backscripts don't exist by
default; unless you've explicitly created one, you can safely ignore backscripts.
More information about the use-livecode
mailing list