Author Topic: Multiple For loops  (Read 2369 times)

0 Members and 1 Guest are viewing this topic.

Offline HitechsTopic starter

  • Full Member
  • ***
  • Posts: 116
  • Activity:
    0%
  • Reputation Power: 2
  • Hitechs has no influence.
  • Respect: +28
  • Referrals: 1
    • View Profile
Multiple For loops
« on: November 27, 2021, 04:07:54 PM »
0

Was just wondering how you pros deal with for loop inside a for loop that runs off #findindex.
I have been just changing my logic so its not needed, and was wondering if that is my only options
so finditem creates findindex for first loop
but then inside that you need a 2nd findindex for 2nd for loop

Not possible? bad code? dont do that?

Example:

Code: [Select]
finditem %items g_ , %distance
for #findindex 1 #findcnt
     {
      if #findtype = %doDad
          {
            finditem %stuff c_ , #backpackid
            for #findindex 1 #findcnt
                     {
                       exevent drag #findid
                       exevent dropc %secureBox
                       wait 15
                     }
           }
      if #findtype = %thingaMajig
          {
            exevent drag #findid
            exevent dropc #backpackid
            wait 15
           }
     }

Offline Gaderian

  • Elite
  • *
  • *
  • Posts: 486
  • Activity:
    0%
  • Reputation Power: 10
  • Gaderian barely matters.Gaderian barely matters.
  • Respect: +50
  • Referrals: 3
    • View Profile
Re: Multiple For loops
« Reply #1 on: November 27, 2021, 04:42:53 PM »
+2
To have 2 nested finditem based loops you have to keep track of your own version of #findindex and #findcnt.
There can be in issue if during the loop an item in the container (the ground can be a 'container' - so maybe it is better to think of it as "search confinement") disappears (is moved out of the 'container').

So it is definitely possible. I only do this when needed, not because it is so much slower (modern CPU and Easyuo 1.5 void the old thinking that 'finditem' is slow).
If you doubt the correctness of my comment on finditem isn't as slow as it is advertised, try it out for yourself. I did a test at a populated bank where I searched among 100 items in game in .005 seconds (5 ms).

Code: easyuo
  1. finditem %items g_ , %distance
  2. if #findcnt > 0
  3.    {
  4.    set %ItemsCount #findcnt ; keep the value for the %items #findcnt value
  5.    for %ItemsIndex 1 %ItemsCount
  6.       {
  7.       finditem %items %ItemsIndex g_ , %distance ; This has 4 parameters!
  8.       if #findcnt = 0
  9.          break ; this means an item was moved while we are processing the whole routine after the outer finditem above!
  10.       if #findtype = %doDad
  11.          {
  12.          finditem %stuff c_ , #backpackid
  13.          if #findcnt > 0
  14.             {
  15.             for #findindex 1 #findcnt
  16.                {
  17.                exevent drag #findid
  18.                exevent dropc %secureBox
  19.                wait 15
  20.                }
  21.             }
  22.          }
  23.       if #findtype = %thingaMajig
  24.          {
  25.          exevent drag #findid
  26.          exevent dropc #backpackid
  27.          wait 15
  28.          }
  29.       }
  30.    }
  31.  

What has been added above:
1) check #findcnt > 0 after the finditem before doing the for loop, because if #findcnt is 0, then you do a loop when you have no data from values 1 counting down to 0.
2) keep track of the outer loop #FINDCNT
3) use your own variable - like how we would use #findindex...
4) reissue a finditem in the loop where we give 4 parameters which includes the index of the loop we want.

If you have moved out of the area an item in the ground search (or someone else has) then the logic breaks down. I wouldn't do this kind of logic for general population items in my script. Snicker7 did this in the original BodFiller script and that and a conversation with another scripter is how I learned about it. It is the way to keep track of more than 1 finditem in a loop.

There is a way where you could save all the data from the outer finditem loop and then try to process it all again in a second pass. That would allow you to skip over items that have been moved on you. This is only a valid issue if you have a list of 20 things you initially found and an earlier one in the list is moved (not by you) so that the list index changes. The above logic takes care of any future found items in the loop iteration being moved, because it bails if it gets to the end of the list too early.

"Go ahead ask me: 'Should I use hard waits or timers?'"
You say:"Should I"
Gaderian:"Timers!"
You Say:"use hard waits or timers?"

The serious side of timer use is illustrated here: http://www.scriptuo.com/index.php?topic=12094.msg101926#msg101926

However, every time I go back and look at this [AutoLooter] script, I realize I had wrote it in my zen state of EUO scripting - so it makes my brain hurt.

Offline HitechsTopic starter

  • Full Member
  • ***
  • Posts: 116
  • Activity:
    0%
  • Reputation Power: 2
  • Hitechs has no influence.
  • Respect: +28
  • Referrals: 1
    • View Profile
Re: Multiple For loops
« Reply #2 on: November 27, 2021, 05:02:09 PM »
0
ah, nice nice , and an excellent explanation

thank you

Tags: