January 18, 2003   Tip of the Week Mailing List
The Tip

One nice new feature in Authorware 6.5 is that you can write your own functions. This means that you can have one copy of your code and call it over and over throughout your Authorware file. We have always had subroutines, but this takes it a step further. You can call a function without navigating anywhere. What's more, you can store functions in an external file or database, and have a single copy that you use throughout a number of Authorware files.

For instance, imagine that you often have to convert a list to a return-delimited string. You might create a function icon that is called ListtoString. Creating a function icon is as simple as checking "Contains function" in a calculation's Icon Properties.

The function itself might contain something like: repeat with element in args@"ListtoString"

if Output = "" then Output := Output^Return
OutPut := Output^element
end repeat

result@"ListtoString" := Output

You'll notice when you do this that Authorware will ask you to define args@"ListtoString" and result@"ListtoString."

To call the function, all you need to do is include the line CallScriptIcon@"ListtoString", mylist) in any calculation, anywhere in the Authorware piece.

This should work great for internal functions, but will fail if you use it with a script string or script file. The reason for this is that there is no longer an icon called "ListtoString." For starters, you would need to change all references to that icon to @IconID. That won't fix the problem, though, because now the IconID referred to is whichever calculation happened to call the script icon. Args and result must exist for every calc that could call an external script function.

Because of this, my default calculation icon in Authorware 6.5 already has the args and result variables set up. You can do this easily by clicking on a calculation icon on the flow line and opening the variables window. Scroll down to the name of the calculation in the variables window, and create new variables named "args" and "result" by pressing the "New" button.

In addition, you need to make sure the variable "Output" exists in the calling piece.

Did you know?

You can ensure that a variable contained in a function exists in a calling piece by using the NewVariable() function. Just include script like this within your function:

EvalAssign("MyVariable:=""")
if EvalStatus=22 then

Newvariable("MyVariable", """")
end if

This assumes that you call the function at least once during authoring. Note that you cannot create new variables after a piece has been packaged.

Addendum

Last week, I spoke about appending parameters to the end of FileName. I meant to make a note at the end of it pointing out that this will only work when the file is accessed via http protocol.

This means that if you double-click on the HTML page to launch it or open it from your browser's File menu, this method will not work. If you see file: at the beginning of the url, rather than http:, the parameters will not be passed.

What this means is that the computer that you are running the course on must be configured as a server, and your course must be called from that server. For instance, if you had the course on the same computer that you were launching the browser from, you might locate it in C:inetpubswwwroot mycourse. In your browser's address bar, you'd type in http://localhost/ mycourse/mypage.htm.

Naturally, if the file is hosted on another machine, you would use the URL or IP for that machine.