Reusable code (again)

Sarah Reichelt sarahr at genesearch.com.au
Mon Nov 29 23:12:54 EST 2004


> Suppose I have a stack file with some reusable
> functions and constants in it - e.g. for doing math
>
> How do I store the constants in my reusable stack and
> then make them available to the rest of the app when I
> load the stack?
>
> If I have some functions, how do I do the same kind of
> thing with them?
>
> I am still not clear on the use of 'insert script' or
> custom properties. If somebody could show me how this
> works with the following constant and function, I
> would be very grateful
>
> e.g. my library stack contains ...
>
> a constant called 'HalfCircle' with a value of 180.0
>
> a function called 'degreestoradians' defined as
>
> function degreestoradians degrees
>    return (degrees/180.0) * 3.1415926
> end function
>
> How do I store these in my library stack?
> How do make them available to the rest of my app when
> the library stack is loaded?
>
You can't store re-usable constants :-(
Constants have to be declared in every script that uses them.
The easiest way to to make them functions rather than constants.

Make your stack with the stack script containing all your math 
functions e.g.
function halfCircle
   return 180
end halfCircle

function degreestoradians degrees
    return (degrees/180.0) * 3.1415926
end function

When you need to access these functions in another stack file, go to 
the Inspector for that client stack and set the "Stack files" to 
include your Math stack. This will make sure that the file containing 
your Math stack is included in any build. In the startup routines i.e. 
preOpenStack or openStack, include a line saying:
	start using stack "Math"

Now you can call your halfCircle or degreestoradians functions from 
anywhere in the client stack or it's sub-stacks.

There are other ways to achieve the same result, but this is the one I 
use most of the time.

Cheers,
Sarah



More information about the use-livecode mailing list