6 Replies Latest reply on Sep 7, 2006 7:30 AM by kyleg

    way to shorten this?

    kyleg Level 1
      I am wanting to change several list properties with one double conditional statement. It seems I have to write a conditional statement for every change when what I want to do is the change in one line.

      see like below.

      -- if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then glistRollR.RR50cntrstart=0, glistRollR.RR99cntrstart=0

      If I could, I would change the whole list each time, but that is not possible with what I am doing.

      is the only way to do what I want like the example below?

      if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then glistRollR.RR50cntrstart=0
      if sprite(6).within(8) and glistRollR.RR99cntrstart=1 then glistRollR.RR99cntrstart=0

      It is really going to start adding lines and end ifs by the time I am done.

      ..looking for an easier way


      thanks
        • 1. Re: way to shorten this?
          Level 7
          kyleg wrote:
          > I am wanting to change several list properties with one double conditional
          > statement. It seems I have to write a conditional statement for every change
          > when what I want to do is the change in one line.
          >
          > see like below.
          >
          > -- if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then
          > glistRollR.RR50cntrstart=0, glistRollR.RR99cntrstart=0
          >
          > If I could, I would change the whole list each time, but that is not possible
          > with what I am doing.
          >
          > is the only way to do what I want like the example below?
          >
          > if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then
          > glistRollR.RR50cntrstart=0
          > if sprite(6).within(8) and glistRollR.RR99cntrstart=1 then
          > glistRollR.RR99cntrstart=0
          >
          > It is really going to start adding lines and end ifs by the time I am done.
          >
          > ..looking for an easier way
          >
          >
          > thanks
          >

          Don't know if it's what you need in this instance, but you can avoid
          multiple "if" statements by using a single "case" statement.
          • 2. Re: way to shorten this?
            Level 7
            On Tue, 05 Sep 2006 15:48:56 GMT, kyleg posted in
            macromedia.director.lingo:

            > -- if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then
            > glistRollR.RR50cntrstart=0, glistRollR.RR99cntrstart=0
            >

            You can't combine commands/variable settings on a single line like that.
            Separate them into different lines:

            if sprite(6).within(8) and glistRollR.RR50cntrstart=1 then
            glistRollR.RR50cntrstart=0
            glistRollR.RR99cntrstart=0
            end if

            But then your second example seems quite different. How about this?

            if sprite(6).within(8) then
            if glistRollR.RR50cntrstart=1 then
            glistRollR.RR50cntrstart=0
            else if glistRollR.RR99cntrstart=0 then
            glistRollR.RR99cntrstart=1
            end if
            end if

            I'm sure I don't understand exactly what you're trying to do here. I don't
            know if this helps in your project, but check this out for toggling
            booleans to true/false and back:

            glistRollR.RR50cntrstart = NOT glistRollR.RR50cntrstart


            --
            Mark A. Boyd
            Keep-On-Learnin' :)
            • 3. Re: way to shorten this?
              Lukewig Level 1
              Hi,

              It depends on your logic... if you are doing a series of tests and you want to check a sequence of tests, then you could do something like this:

              case (true) of
              (sprite(6).within(8) and glistRollR.RR50cntrstart=1):
              glistRollR.RR50cntrstart=0
              glistRollR.RR99cntrstart=0
              (SomeOthertest):
              -- some lingo here
              end case

              this works if you want to find the first 'true' case only. If you want to test multiple 'true' cases, then an if-end if block might be better.

              Alternatively, you can do some mathematical trickery. For example, say you have 5 tests that can be evaluated to true (1) or false (0). you can represent these 5 tests like this:

              00110 (first two are false, the next two are true, the last one is false).

              You could treat this as a binary number (which, in this case, has the integer value of 6). Doing this means you can use Lingo's bit operators (bitAnd/BitOr etc) to check multiple test results at once. For example, say you want to know if tests 3 and 4 were true (and you don't care about the others), you could use bitAnd to produce a new binary number who has 1s in the position where both test result and desired result have 1s.

              TESTS: 00111
              WANT : 00110
              -------------
              BITAND: 00110 <- this should be the same as what we want


              So, in lingo you would do something like this

              TestResults = ConvertToDecimal("10101")
              DesiredResult = ConvertToDecimal("00110")
              ConditionsMet = (DesiredResult = bitAnd(TestResults, DesiredResult))



              (for a script to convert decimal numberes to binary numbers and back, see
              http://www.lingoworkshop.com/Codelib/xlib.php -- look for the "NumberLib" script)

              -- Luke


              • 4. Re: way to shorten this?
                kyleg Level 1
                thanks for all who has replyed,

                What I am trying to do is track the position of a sprite within hotspot areas by adding the condition as a 1 in a list
                It get trickier in that I have to track previous areas and wether a counter has already been started in the current area (if it has it needs to not restart as it would on a looping frame where the sprite is still in the area).

                The binary way sounds interesting.

                Luke,

                am I to understand that I can have a list as binary?

                can it be this long?

                glist = 00011100101010

                So with a 5 property example would it look like this?

                on startmovie
                glist="00000"
                end

                on exitframe
                --condition #1
                TR=ConvertToDecimal(glist)--makes glist binary (glist can be changing)TR stands for testresult
                DR= ConvertToDecimal("00110")--DR stands for DesiredResult performed if condition below is true
                if sprite(6)within(8)=(DR = bitAnd(TR, DR))--change the glist to DR


                --condition #2 (use these 2 lines below for each new condition)
                DR= ConvertToDecimal("01111")--DR stands for DesiredResult performed if condition below is true
                if sprite(6)within(8)=(DR = bitAnd(TR, DR))--change the glist to DR
                end

                binary seems very interesting.
                no long if statements...and long property list
                sounds to godd to be true!

                It seems I might be able to work with case since I can have multi conditions and multi events (did not know this!!!)
                I have added comments to the example to make sure I understand it right, but I think I do.
                case (true) of
                (sprite(6).within(8) and glistRollR.RR50cntrstart=1):
                glistRollR.RR50cntrstart=0 --set only if condition is true
                glistRollR.RR99cntrstart=0 --set only if condition is true
                (SomeOthertest):
                -- some lingo here
                end case

                let me know on the binary thing...if that makes sense ....if it would work...or is there more to it than that

                Kyle
                • 5. Re: way to shorten this?
                  Lukewig Level 1
                  Hi,

                  Your code makes sense. To speed things up, you could try and work in decimal numbers as often as possible. For example, instead of writing

                  gListAsDec = binaryToDec("00011100101110")

                  you would simply write

                  gListAsDec = 1838

                  You might also have a function for changing gList. For example, it might be easiest to keep gList as a lingo list. When ever you change gList, you set a corresponding decimal value in another variable

                  on GList_SetAt (position, value)
                  if gList[pos] <> value then
                  gList[pos] = value
                  gListAsString=implode("", gList) -- covert to string (see http://www.lingoworkshop.com/code/classes/scripts/ListLib.ls)
                  gListAsDec = binaryToDec(gListAsString)
                  end

                  Then in your exitframe, you do something like this:

                  on exitframe
                  --condition #1
                  DR= 6 -- precalculate ConvertToDecimal("00110")
                  if sprite(6)within(8)=(DR = bitAnd(gListAsDec, DR)) then
                  -- etc

                  end


                  -- Luke
                  • 6. way to shorten this?
                    kyleg Level 1
                    Luke,

                    thanks for the reply

                    I might stay away from precalculating the binary as I can see the 1's and 0's visually in the code better for troubleshooting. I think and do not have to use windows calculator or similar.

                    I think I am going to have 9 properties at the most with 4 list for the project so it's not to long (at least not on this one). I can see where if you get up to 20-30 items it would be good.

                    How would I make a change to just a property and not the whole list in binary. Is the example below only changing the property at the point that I have set in DR as true (see in bold where I modified my code or comments)

                    on startmovie
                    glist= binaryToDec("00000")--dont just make this glist = "00000"
                    end

                    on exitframe
                    --condition #1
                    TR=ConvertToDecimal(glist)--makes glist binary (glist can be changing)TR stands for testresult
                    DR= ConvertToDecimal("00110")--DR stands for DesiredResult performed if condition below is true
                    if sprite(6)within(8)=(DR = bitAnd(TR, DR)) --change the glist property to true only at DR positions that have a 1
                    end

                    so if the glist is set at "00001" and I change to DR="00110" the glist is now "00111" ?.....as I am not changing that last digit at the end?

                    If this is right then I am understanding how to change just certain properties and leave the others alone.

                    The only other thing with binary is using a multi condition. I assume it just looks like this?(condition in bold)


                    if sprite(6)within(8) and glist=binaryToDec("00001")=true=(DR = bitAnd(TR, DR))
                    --compares the last digit only to see if it is true and only execute the (DR=bitAnd(TR,DR)) if that is the case


                    I also think I am going to need a DR1, DR2 etc. for each condition like so:
                    --------------------------

                    TR=ConvertToDecimal(glist)--makes glist binary so U can make multiple changes in one command and to only certain digits - TR stands for testresult
                    --condition #1
                    DR1= ConvertToDecimal("00110")--DR1 stands for Desired Result #1 and is performed if condition below is true
                    if sprite(6)within(8)=(DR = bitAnd(TR, DR))-- change glist property to 1 only at DR positions that have a 1
                    end if

                    --condition #2
                    DR2= ConvertToDecimal("01111")
                    if sprite(6)within(9)=(DR = bitAnd(TR, DR))
                    end if

                    I think my finished conditions are going to look like above.

                    ...think I am getting close to getting the methodology down

                    let me know
                    thanks for your help...it is really appreciated!!!!!
                    Text