Director: Compressing Lists from storage in text members

Something I do a lot in my Macromedia Director projects is save a proplist or a linear list to a text member in order to recall it later. It has some advantages over using external text files. I use it a lot for game levels where the levels are saved to text members during authoring and read back during playback.

While working on my next game I thought of a simple technique to compress these lists a bit while still making them human readable and easy/fast to convert back into lists when I need them. My compression rouinte is getting my game levels down to about 75% of their original size. First off, if you’re not sure how to save a list to a text member, here’s how. To save a list to a text member (or field member), do this:
ilist = [#a: 10, #b: 20, #c: 30]
member("savedlist").text = string(ilist)
To recall it from the text member, do this:
inewlist = value(member("savedlist").text)
Watch out though because Director’s value() command will choke on certain things, like the void token, and strings with returns in them. This is pretty easy to avoid if you design your program correctly, though.

My compression trick


My compression trick is somewhat obvious, but a trick none-the-less. After I covert the list to a string, I run a routine that removes space characters, while preserving spaces that are inside of quotes. That’s it. That’s the whole trick.

Director and the Shockwave plugin can still convert these compressed lists back into real proplists and linear lists just fine. In fact with my time trials Director was just a bit faster at converting the compressed text into arrays than it was with the untouched text; so there is a slight performance gain when reading.

Pros and Cons


I’ve just started messing with this technique so I don’t know all there is to know about it yet, but let me scratch some info down: It’s slower to save lists this way (using the routine I wrote, anway) but saving to a member is really only useful during authoring, so this slight slowdown only pertains to me, and not my end-users. Also, since the lists are still readable, it’s nice for debugging. And finally, in my levels, I’m getting member sizes around 70% to 75% their original size. In a game with over 100 levels like Rocknor’s Donut Factory, that could save me upwards of 300k. Not bad. Of course results will vary depending on the type of data saved.

on compressList ilist
  -- CompressList by Hanford Lemoore
  --http://blog.hanfordlemoore.com
  -- this is designed to convert lists to strings, then remove the space characters from them to make
  -- them smaller to save in a text member. It preserves spaces that are inside of quotes. This works
  -- with linear lists as well as property lists.
  -- to use it, simply do the following:

  -- smallerlist= compresslist(normallist)

  -- the end result it a text string with all the extra spaces removed. Pretty simple.

  inew =""
  ilist  = string(ilist)
  icnt = ilist.char.count
  iquote= 0 -- Quote checker
  ilast = 1
  repeat with i = 1 to icnt

    if ilist.char[i] = " " then
      if iquote = 0 then
        ilist= ilist.char[1..i-1] & ilist.char[i+1..icnt]
        i = i -1
        icnt = icnt - 1
      end if

    else if ilist.char[i] = quote then
      iquote = 1 - iquote-- Toggle the Quote checker
    end if

  end repeat
  return ilist
end

2 Responses to “Director: Compressing Lists from storage in text members”

  1. rich Says:

    >>>>
    Watch out though because Director’s value() command will choke on certain things, like the void token, and strings with returns in them. This is pretty easy to avoid if you design your program correctly, though.
    >>>>

    That’s a rather brash claim, since voids don’t imply an “incorrect” design. On the contrary, I’d say that writing your code under the assumtion you will never have a void value is at best error-prone, at worst lazy!

    I do a similar thing to your space-saving, when saving lists like this and simply search and replace “” with “void”

  2. Hanford Says:

    I didn’t mean Voids themselves are incorrect.

    Just that you need to plan for them; ignoring the fact that they can break the value() command will get you in trouble; so using this technique does indeed need you to design for them.

Leave a Reply

For security, enter the word TURING below:
Comments RSS feed