4 Replies Latest reply on Apr 20, 2006 2:23 AM by James Newton, ACP

    Property List Help plz

    M4rkeh
      Hi can anyone have a look at a bit of lingo i have for showing high scores in a game, I'm pretty sure I'm using get pref and set pref incorrectly and the global variable gname which stores the players name appears blank when added to the property list, although the variable for score appears correctly.

      on begin movie --

      ghighscoreslist = value(getPref("topscores.txt"))

      if voidp(ghighscoreslist) then
      set ghighscoreslist = ["player 1":1, "player 2":2, "player 3":3, "player 4":4, "player 5":5, "player 6":6, "player 7":7, "player 8":8, "player 9":9, "player 10":10]

      end if
      end

      on frame which displays scores--

      on beginsprite me

      addprop (ghighscoreslist, the value of gname, the value of gscore)

      sort ghighscoreslist

      set lcountscores = count(ghighscoreslist)

      if lcountscores >= 11 then
      deleteat(ghighscoreslist,11)
      end if

      setPref("topscores.txt", string(ghighscoreslist))

      --scores are displayed this way--

      set lvalue1 = getat(ghighscoreslist,1)
      put lvalue1 into field "value1"
      set lname1 = getpropat(ghighscoreslist,1)
      put lname1 into field "name1"
      etc....

      The lingo above should load the scores at the start of the game, if there are no scores stored then preset scores should be displayed.
      Then add the current score/name to the list, sort the list, then remove the lowest score/name.
      The list should then be displayed and saved to the text file.

      I'm new to director so please keep any help as simple as possible,
      thanks
      Mark
        • 1. Re: Property List Help plz
          Level 7
          At first glance I see 2 possible issues



          1) There's no begin movie eventm instead use startMovie

          2) Every handler referencing a global must desclare it at the beginining
          of the handler (or at the very top of a script member so all handlers
          use the global)

          on startMovie
          global ghighscoreslist --
          ghighscoreslist = value(getPref("topscores.txt"))

          if voidp(ghighscoreslist) then
          set ghighscoreslist = [] -- initialize empty list

          end if
          end




          Strincly speaking, the .txt extension is not needed for pref files.


          For a quick set/get pref test, ry entering the following in the message
          window ( my director is too faar away for me to try it )


          setPref "prefTest", string( [2, 4, 6] )


          put getPref("prefTest") -- raw string

          put value(getPref("prefTest")) -- list form
          • 2. Property List Help plz
            M4rkeh Level 1
            Sorry I am using start movie, that was just a typo i didnt copy it exactly from my script and I have all the globals declared. What is the correct lingo for sorting and adding to a property list, I think thats where my probles may lie.

            **edit
            Actually it seems to be working, thanks that test helped me get my head around the whole string/getpref/setpref thing. But the name doesn't seem to be added to the list with the score and also the list is being sorted by the property, not the value, if that makes sense?
            • 3. Re: Property List Help plz
              Level 7
              Normally one appends to a prop list using

              adProp theList, newPropName, newPropValue


              Sorting property list has limited value, since it's expected that one
              accesses properties by name. Prop lists seem to sort by value if each
              prop's first value and not by prop name.
              • 4. Re: Property List Help plz
                James Newton, ACP Level 3
                You can use findPosNear() on a sorted list to get the index number of the next closest entry to the value you are looking for:

                myList = ["Bart", "Homer", "Lisa", "Maggie", "Marge"]
                myList.sort()
                myIndex = myList.findPosNear("Margaret")
                put myList[myIndex]
                -- "Marge"

                This can be useful in address books and type-ahead input fields and similar schemes. This works with both linear lists and property lists.