Behavior: preloadNetThing

Date:  Tue, 25 Nov 1997 10:29:08 -0800
From: Chris Walcott 
Subject: preloadNetThing behavior
I finally got around to updating the preloadNetThing behavior that is included in the 6.0 behavior library. Now, when the preloading is done you can go to a specified marker, take an action based on a global flag or gotoNetPage.

Behavior description:
This behavior can be used to as a general purpose preloadNetThing script. The behavior is assignable to a frame Script. With this behavior you can perform a series of preloadNetThing commands in such a way that there will be no more than 4 open streams at one time. When the preloading is completed, you can choose between three different actions. Go To Marker sends you to a marker specified in the custom property dialog. Autoload Page performs a gotoNetpage on the URL specified in the custom property dialog. Set preloadDone to TRUE sets the global variable "preloadDone" to TRUE and progresses to the next frame. It is then up to the author to catch the flag in a frame script to take further action.

copy this into a score script. Please include the commented section at the top.

- chris walcott
- macromedia director development


-- preloadNetThing Behavior script
-- Chris Walcott
-- version 1.0 2/13/97
-- version 1.1 9/26/97  update and bug fixes.
-- version 1.2 11/24/97 - added Go To Marker and Set preloadDone flag.

-- This behavior can be used to as a general purpose preloadNetThing script.
-- The behavior is assigned to a frame Script.  With this behavior you can
-- perform a series of preloadNetThing commands with no more than 4 open streams
-- at one time.  When the preloading is completed, you can choose between
-- three different actions.
-- Go To Marker sends you to a marker specified in the custom property dialog.
-- Autoload Page performs a gotoNetpage on the URL specified in the custom
-- property dialog.
-- Set preloadDone to TRUE sets the global variable "preloadDone" to TRUE and
-- progresses to the next frame.  It is then up to the author to catch the
-- flag in a frame script to take further action.

-- Required cast member. You will have to create a named field cast member
-- to hold the list of URL's that you want to preload.

-- The custom properties are:
-- "Member Name of URL List" - this is the field that will contain the list
-- of URL's that are to be preloaded. Use the Score channel number of the field.
-- "Output Status" - If TRUE, status lines will print to the message window.
-- "Action" - If set to "Go To Marker", the playback head will advance to th e
-- next marker when preloading is done.  If set to "AutoLoad", a preloadNetThing
-- will be performed on the URL provided in the next property field.
-- if set to "Set preloadDone flag TRUE", a global flag will be set allowing you
-- to take whatever action you wish and the play head will progress to the next
-- frame.  Make sure to reset the flag to FALSE after the action is taken.
-- "Name of Marker" - if Action is set to "Go To Marker", this is the marker the
-- movie goes to.
-- "Target URL" -  if Action is set to "Autoload Page", a gotoNetPage operation
-- will be performed on this URL.


property pListURLs        -- field containing URL's to preload
property pDebug           -- if TRUE status of preload will print to message window
property pAction          -- property determins what action will take place when preload done
property pURL             -- which URL is called if Action = "AutoLoad Page"
property pInitializeDone  -- flag set to TRUE when initializeNetList is done
property pMarker          -- the name of the maker the movie goes to if Action = Go To Marker

property pWaitingList          -- list of URL's waiting to be retrieved
property pDispatchedListURL    -- list of preloaded URL's
property pDispatchedListNetID  -- list of preloaded URL net ID's



on getPropertyDescriptionList
  set description = [:]

  addprop description, #pListURLs, [#default: "", #format: #integer, 
          #comment: "Member name of URL list:"]

  addprop description, #pDebug, [#default: 0, #format:#boolean, 
          #comment: "Output status to message window:"]

  addprop description, #pAction, [#default: "Go To Marker", #format:#symbol, 
          #comment: "Action when PreloadNetThing Done:",  
          #range: ["Go To Marker","Set PreloadDone flag TRUE","AutoLoad Page" ]]

  addprop description, #pMarker, [#default: "", #format: #string,  
          #comment: "Name of Marker for Go To Marker:"]

  addprop description, #pURL, [#default: "http://", #format: #string,  
          #comment: "Target URL for AutoLoad Page:"]

  return description
end


on getBehaviorDescription
  return "PreloadNetThing Behavior.  A NetLingo behavior script that can be assigned to either a sprite or a script channel frame."
end

on beginSprite me
  global preloadDone

  set pInitializeDone = FALSE
  set preloadDone = FALSE
  preLoadMember pListURLs

end


on exitFrame me
  global preloadDone

  if  NOT pInitializeDone = TRUE then
    initializeNetList
    cursor 4
    set pInitializeDone = TRUE
  end if

  if NetDoneList() > 0 then
    go to the frame
  else
    case (pAction) of
      "AutoLoad Page":
        cursor 0
        gotoNetPage pURL
      "Go To Marker":
        go to pMarker
        cursor 0
      "Set PreloadDone flag TRUE":
        set preloadDone = TRUE
        go to the frame + 1
    end case
  end if


on initializeNetList me

  -- initialize the lists
  set pWaitingList = []
  set pDispatchedListURL = []
  set pDispatchedListNetID = []
  repeat with i = 1 to the lineCount of member pListURLs
    set theLine = line i of field pListURLs
    append( pWaitingList, theLine)
  end repeat

  if pDebug = TRUE then
    put "List of assets to preload" & RETURN & pWaitingList & RETURN
  end if


end


on netDoneList me
  -- check the dispatch list for assest that have arrived
  
  repeat with iListAsset = 1 to count( pDispatchedListURL )
    -- if the asset has arrived
    if NetDone( getAt( pDispatchedListNetID, iListAsset ) ) then
      if pDebug = TRUE then
        put "Asset Received:" & getAt( pDispatchedListURL, iListAsset )
      end if
      -- remove if from the dispatch list
      deleteAt pDispatchedListURL, iListAsset
      deleteAt pDispatchedListNetID, iListAsset
    end if
  end repeat

  -- check the dispatch and waiting list: how many things
  -- can we preload?  Current stream limit is set to 4
  set iCountDispatch to min( count( pWaitingList ), 4 - count(pDispatchedListURL ) )
  repeat with iFreeSpot = 1 to iCountdispatch

    -- start the  preloadNetThing operation on the next waiting asset
    set getMe = getAt( pWaitingList, 1 )
    if pDebug = TRUE then
      put "Asset Preloading" getMe
    end if
    preloadNetThing getMe

    -- move the asset to the dispatched list
    append pDispatchedListURL, getAt( pWaitingList, 1 )
    append pDispatchedListNetID, getLatestNetID()
    -- remove the asset from the waiting list
    deleteAt pWaitingList, 1
  end repeat

  -- check the dispatch list for assest that have arrived
  set netDoneListCount = count( pDispatchedListURL ) + count( pWaitingList)
  if pDebug = TRUE then
    put "Assets remaining to process:" netDoneLIstCount
  end if
  return netDoneListCount

end