Lingo Tutorial: Reading from and Saving into External Files

READING: Director FileIO Extra; Maricopa Tips n' Scripts

File "field.dir"

Sometimes you need to have your program read or write to an external file (to save your notes, to keep the state of your game, etc.) To accomplish this, you need to use some of the extra functionality provided by "plug-ins" written in other languages (such as C, Java, etc) for Director. These plug-ins are called xtra's.

The director file shows how to save text from a field (which may contain user notes, for example) and show it up again at another place. (The second field could be different than the first, in general.) It is always a good idea to initialize your fields:

 

on startMovie
  member("my notes").text = "" -- clear the field
  member("my notes").font = "Times"
  member("my notes").fontSize = 18
end

Remembering the contents of the field is done though the use of a global variable and the script:
 

  global notes
  notes = member("my notes").text -- keep the field contents in a global variable

So, when we reach frame "other" we can remember what the field contained. Next, we will save the content of this field inside a file we created for this purpose.

File "save_in_file.dir"

This program saves the contents of the field "my notes" in a file called "Saved_Notes.txt", in the same directory as the program. Quite useful for saving the status of a game or other information.

 

-- This  saves the notes onto a file upon stopping the movie
on stopMovie
  global notes
  -- WRITE THE DATA INTO A FILE
  scribe = new (xtra "FileIO")
  myFileName = "Saved_Notes.txt"
  -- put myFileName
  scribe.createFile(the moviePath & myFileName)
  scribe.openFile(myFileName, 0)
  scribe.writeString (string (notes)) -- will write on top of any existing file
  scribe.closeFile()
  put "FILE WRITTEN"
  -- END OF FILE GENERATION
end stopMovie 

And this loads the notes from a file upon starting the movie

 

on startMovie
  global notes
  member("my notes").text = "" -- clear the field
  member("my notes").font = "Times"
  member("my notes").fontSize = 18
  
  -- READ THE DATA FROM A FILE INTO A STRING VARIABLE
  scribe = new (xtra "FileIO")
  myFileName = "Saved_Notes.txt" -- we hope the file to exist
  
  scribe.openFile(the moviePath & myFileName, 0) -- open for reading and writing
  if (status(scribe) <> -37) then -- if the file was found
    notes = scribe.readFile()
    put "FILE READ: " & notes
  else
    notes = ""
    put "FILE NOT FOUND"
  end if
  scribe.closeFile()
  -- END OF FILE READING
end startMovie 

NOTE: You can change the file name to whatever you want. But be warned: If the file name you are trying to save in cannot be handled by the OS, (because it is, say, too long) the file will be lost...

Saving the state of a game

One useful application of the FileIO xtra is to use it so that a user can save a game she is playing for later. You can practice by augmenting this version of a 2-level memory game so that it saves the game and it is available to play it next time. Note: it requires that you refresh your memory on String manipulation. (Here is a version that saves the easy-level game. Saving the tough-level game is left as an exercise.)

Finally, let us have the user choose which file she wants to read in.

File "read_from_chosen_file.dir"

This allows the user to read in from any file on the hard disk.

 

-- This loads the notes from a file upon starting the movie
on startMovie
  global notes
  member("my notes").text = "" -- clear the field
  member("my notes").font = "Times"
  member("my notes").fontSize = 18
  
  -- READ THE DATA FROM A FILE INTO A STRING VARIABLE
  scribe = new (xtra "FileIO")
  myFileName = displayOpen(scribe)   -- Display Open Dialog and return the fileName 
  
  if myFileName = "" then -- no file was chosen
    alert("You did not chose a file, so I will try to open the default one")
    myFileName = "Saved_Notes.txt" -- open this by default (still might not exist)
    scribe.openFile(the moviePath & myFileName, 0) -- open for reading and writing
    
    if (status(scribe) <> -37) then -- if the file was found
      notes = scribe.readFile()
      put "FILE READ: " & notes
    else
      notes = ""
      put "FILE NOT FOUND"
    end if
  else -- a file was chosen
    alert("You chose to open file " & myFileName)
    scribe.openFile(myFileName, 0) -- open for reading and writing
    
    notes = scribe.readFile()
    put "FILE READ: " & notes
  end if
  
  scribe.closeFile()
  -- END OF FILE READING
  
  member("my notes").text = notes -- just to show what I read
end startMovie 

Now change the stopMovie script to also save notes into a selected file.

 

 

Maintained By: Takis Metaxas
Last Modified: April 15, 2010