Articles Archive
Articles Search
Director Wiki
 

preLoadNetThing

November 6, 1997
by Alex Zavatone

At first glance, preloadNetThing seems like a great command since it lets you load jpeg, gif and mov files off the net. Unfortunately, it is not clear to many Director users how to set up the lingo to enable preloadNetThing to do it's job most effectively.

The first problem is that once you issue a preloadNetThing, you have to check occasionally to see when it is done. There is no broadcasted message saying "Hey, I'm done!" You have to set up lingo to poll or check to see if the task has been completed. The most obvious way to try to do this is within a repeat loop. Unfortunately, it is also a brick wall since the command that returns "if a network task is completed", netdone(), is an event that is blocked when a repeat loop is being executed.

Another method is to break up your score into sections that have lingo in frame scripts like that below:

on exitFrame
  global gMyNetID
  if netDone(gMyNetID) then
    go the frame + 1
  else
    go the frame
  end if
end

The problem here is that you must disrupt the flow of your movie to accomodate the preloadNetThing. Even though it works, this is not ideal. Now there are facilities that exist within Lingo that will do the checking for you if set up correctly. To do this, you must venture into the realm of objects -- but, don't worry -- in this article, it's already set up for you. You can just copy and paste the code and away you go. As always, the concept is the big deal -- more important than the code in fact.

Here, I wanted to create a script that would do it all for you. The object would first come into existence, then fetch the media, check itself for completion and then delete itself. Another script would watch the first net process and when it was done, it would call on the display handler to put it on the screen. Or the script could fire off the display handler (or whatever handler you want) before it deletes itself. Both will work fine. These approaches are used in the shockwave movie below to fetch media off the net.


Note: shockwave 6.01 is required to display any Quicktime movies. The source movie is also available for download (Mac or PC)

The code that allows this to be able to happen is very simple to call. There are only two handler calls to load and display any of the media. A generic handler for loading a jpeg, gif or mov is displayed below.

on LoadNetMedia
  add the actorlist, (new (script ¬
    "PreloadNetThing", URLtoTheMedia)
  add the actorlist, (new (script ¬
    "Display", mySprite, ¬
    myMember,getAt(the actorlist, ¬
      count( the actorlist))))
end

Above, the only variables you need to include are the URL of the media, the sprite you want it to display in and the member that it loads into. Below is the source that grabs and displays the jpeg

on StartJpeg
  add the actorlist, (new (script "PreloadNetThing",¬
    "http://www.blacktop.com/¬
     zav/download/clanlogo.jpg"))
  add the actorlist, (new (script "Display", 1, 1, ¬
    getAt(the actorlist, count( the actorlist))))
end

The scripts that you need to paste into your movie are the "preloadNetThing", "NetDone" and "Display" scripts and are included below. Don't forget to make sure that they are named with their correct names.

script: PreloadNetThing

property pFilename, Ancestor

on New me, myUrl, myFile
  init me, myUrl, myFile
  return me
end
on Init me, myUrl
  preloadNetThing myUrl
  set pFilename = myUrl
  set the ancestor of me = (new (script "NetDone",¬
   getlatestNetID()))
end

script: NetDone

property pDone, pID
on New me, myId
  init me, myId
  return me
end
on Init me, myId
  set the pDone of me = 0
  set the pId of me = myId
end
on StepFrame me
  if pDone = 1 then Dispose me
  if netdone(pId) then set pDone = 1
end
on Dispose me
  deleteOne(the actorlist, me)
end

script: Display

property pSprite, pMember, pDone, pThreadToWatch
on New me, mySprite, myMember, threadToWatch
  Init me, mySprite, myMember, threadToWatch
  return me
end
on Init me, mySprite, myMember, ThreadToWatch
  set pSprite = mySprite
  set pMember = myMember
  set pDone = 0
  set pThreadToWatch = ThreadToWatch
end
on StepFrame me
  if pDone then Dispose me
  if (the pDone of pThreadToWatch) then
    if netError( the pId of ¬
     pThreadToWatch ) = "OK" then
      set the filename of member pMember = the ¬
      pFilename of pThreadToWatch  
      set the member of sprite pSprite = pMember
    else
      set the media of member pMember = ¬
         the media of member "error"
    end if
    set pDone = 1
  end if
end 
on Dispose me
  deleteOne(the actorlist, me)
end


Once the scripts are pasted in and your movie is running, all that needs to be done is to call your medialoading handler and the media will load and display on it's own. No babysitting required. Your mileage may vary.

Now there is always room for improvement and many differnet ways to get the job done. This all could be done with one command if the display command was issued from within the stepFrame method of the NetDone script. However, this would no longer make netdone "generic" so, remunch the code as you see fit.

A Director user since 1987, Alex (Zav) Zavatone has worked on the engineering teams for both Director and Shockwave, 4, 5, 6 and 7. Recent investigations find him developing foundation classes for Director with asynchronous process management and other life threatening activities. In its early days, he had something to do with this Internet thing known as "DOUG". A noted ne'erdowell, slacker and laggard, Mr. Zavatone is nonetheless is trusted by children, small animals and the elderly. In his spare time, Mr. Zavatone rehabilitates lame desert trout.

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