Articles Archive
Articles Search
Director Wiki
 

Passing string variables from Flash to Director

January 31, 2001
by Ryan Todd

As many of us who work with integrating Flash and Director can attest, there are a few little things that can cause problems with files and make them not work like one would think they should work. This article discusses one of those problems.

Through the use of the Flash Asset Xtra, one can enhance what Director has to offer, including communication between the two movies. The GetURL action allows for communication with Director in a few different ways:

1. Get URL ("someString") - this method sends the string "someString" to Director, in which it can be captured and parsed.

2. Get URL ("lingo:someLingoCommand") - this method calls a particular Lingo command in a Director movie script.

3. Get URL ("event:someHandlerName,someParameter") - this command triggers a Lingo command in the behavior of the Flash sprite, and passes that handler the parameter or parameters in the string.

Now suppose one would like to take the contents of a field or variable in Flash, and send them to Director. There are several ways to do this of course. One could use one of the above calls to trigger Director to read the variable using getVariable, for example. A more convenient way to do this is to simply pass the variable to Director using Get URL.

Unfortunately, this poses a small problem that must be negotiated - in order to send the variable out of Flash with Get URL, you must create an expression representing the string that gets passed.

Here is the code in Director that is catching the calls from Flash. I have inserted one alert to let us know what was passed to Director, and a case statement to further test these values when I discuss passing integers:

on catchCall me, varSent
  alert ("The value received by Director is: " & varSent)

  case varSent of
    "this":
      alert ("The this script passed a value.")
    "that":
      alert ("The that script passed a value.")
    "12":
      alert ("The 12 script passed the string 12.")
    "34":
      alert ("The 34 script passed the string 34.")
    12:
      alert ("The 12 script passed the numerical value 12.")
    34:
      alert ("The 34 script passed the numerical value 34.")
    otherwise
      alert ("This script did not pass a value.")
  end case
end

If we look at the example movie, notice that in the this example, the button in Flash contains the following ActionScript:

On (Release)
  Set Variable: "varToSend" = "this"
  Get URL ("event: catchCall, " & varToSend)
End On

Click the button and observe that in the alert, we are notified that nothing is passed to Director (the variable actually has a VOID value). The problem seems to be that even though the variable called varToSend contains a string, when it is added to the expression above, Flash (or something in the Flash Xtra) does not recognize it as a string value to be passed.

Now click the that button. Notice that now we get a string value - "that" - passed to Director from Flash. This is the ActionScript that resulted in that success:

On (Release)
  Set Variable: "varToSend" = "that"
  Get URL ("event: catchCall," & chr (34) & varToSend & chr (34))
End On

The expression chr (34) is just a coded way of inserting extra quotes into the string that we are building to send using Get URL. By putting these quotes around the value contained in the variable varToSend, we seem to make Flash happy.

Now click the try this with numbers button and we'll take a look at the second part of the example. We now have two buttons that have the exact same code in Flash, save that instead of sending a string of text digits, we are sending a string of numerical digits. For those of us who are used to programming, a string is a string, regardless of whether or not it contains letters or numbers. The Flash user usually distinguishes between the number 12 and the string "12" by either using quotes around the string as in this sentence, or by toggling the abc/123 button in the ActionScript window. The following is the code for the two different buttons.

The top button:

On (Release)
  Set Variable: "varToSend" = "12"
  Get URL ("event: catchCall, " & varToSend)
End On

The bottom button:

On (Release)
  Set Variable: "varToSend" = "34"
  Get URL ("event: catchCall, " & chr (34) & varToSend & chr (34))
End On

This time, the code works exactly the same. My first thought when I found this result was that maybe I was actually getting a number passed as a parameter rather than a string, but that is not the case. Notice in the case statement, I have put in a test to see if the value passed is the number 12 or the string "12". As you see from the second alert that pops up, it passes a string.

Why does this work differently when both the letter and number examples use strings? I'm not sure. I do know that the above solution does work.

Other ways of passing strings from Flash that also work

1. Use Get URL to simply pass the string out. This seems to work without having to insert extra quotes. However, your code in Director will look like this (rather than allowing you to create specific handlers for specific actions). Lets say you passed the string "catchcall, this":

on getURL me, vstring
  set the itemDelimiter = ","
 
case vstring.item[1] of
    "catchCall": alert (vstring.item[2])
  end case
end

2. Use Get URL to trigger a handler in Director that reads a variable called varToSend that resides in the Flash movie. Attach this behavior to the sprite:

on getURL me, vstring
  case vstring of
    "catchCall":
      thisSprite = the spriteNum of me
      sentValue = sprite (thisSprite).getVariable ("varToSend")
      alert (sentValue)
  end case
end getURL

3. Use Get URL with the parameter "lingo: command" and proceed similarly to example 2. However, this time, "command" should be the name of a custom handler that you create such as this one:

on customReceiver me
   thisSprite = the spriteNum of me
   sentValue = sprite (thisSprite).getVariable ("varToSend")
   alert (sentValue)
end customReceiver

Either of these other three ways are just as valid as using the event syntax with the embedded chr (34). They just require a little different configuration and logic in both the Flash and Director movies.

A sample Director 8 movie and Flash 4 source are available for download in Mac or Windows format.

Ryan Todd started working in web and multimedia development as a grad student in 1995 and currently works as a developer at IBM Ebusiness Innovation Services.

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