• Hail, we're RedGuides. We want you to play six (yes, 6!) EverQuest characters at once. We have scripts and a friendly community to help, please join our playstyle and say hello! 👋

Question - Issue with nested for loop

Soandso2

Well-known member
To skill up languages, I wrote the following Lua, but it acts up in a strange way.
Lua:
mq = require('mq')

for i=1,4,1 do
    mq.cmd("/language "..i)
    print("Setting language to idx "..i)
    mq.delay(1000)
    for l=1,100,1 do
        mq.cmd("/g "..l.." I am teaching you language "..i)
        mq.delay(1000)
    end
end

I added those delays, thinking the server-client would not handle the learning process if running at full speed. However, both delays only work during the first iteration of i.
Once the inner loop has finished and i becomes 2, the Lua executes at full speed, with no delays. There are no errors, Everything works as intended, except for the speed at which it runs. Any ideas why?

edit: Actually, the initial problem is not entirely accurate. This is part of the log: Look at the timestamps.

Code:
[Sat Jan 27 09:47:51 2024] You tell your party, '51 I am teaching you language 1'
[Sat Jan 27 09:47:52 2024] You tell your party, '52 I am teaching you language 1'
[Sat Jan 27 09:47:53 2024] You have become better at Elder Elvish! (51)
[Sat Jan 27 09:47:53 2024] You tell your party, '53 I am teaching you language 1'
[Sat Jan 27 09:47:54 2024] You tell your party, '54 I am teaching you language 1'
[Sat Jan 27 09:47:55 2024] You tell your party, '55 I am teaching you language 1'
[Sat Jan 27 09:47:56 2024] You have become better at Elder Elvish! (52)
[Sat Jan 27 09:47:56 2024] You tell your party, '56 I am teaching you language 1'
[Sat Jan 27 09:47:57 2024] You tell your party, '57 I am teaching you language 1'
[Sat Jan 27 09:47:58 2024] You tell your party, '58 I am teaching you language 1'
[Sat Jan 27 09:47:59 2024] You tell your party, '59 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '60 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '61 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '62 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '63 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '64 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '65 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '66 I am teaching you language 1'
[Sat Jan 27 09:48:00 2024] You tell your party, '67 I am teaching you language 1'
 
Lua:
mq = require('mq')

for i=1,4 do
    mq.cmd("/language "..i)
    print("Setting language to idx "..i)
    for l=1,100 do
        local pause = 5 -- Half a second. Adjust as needed
        mq.cmdf('/timed %d /g %d: I am teaching you language %d, pause, l, i)
        pause = pause + 5 -- Increment during each loop
    end
    print('\nLanguage learning complete!')
end

Can't hop into EQ to test, but I think this may help. Implemented a 'pause' variable to insert a '/timed' delay. This should run 100 times per language, and then finish with the 'Learning complete' message. Could be that the mq.delay wasn't delaying the portions that you were expecting.
 
Lua:
mq = require('mq')

local run = true

local function doHelp()
    print('To run: \ao/langstudy \atNumber_of_languages Number_of_iterations')
    print('Ex: /langstudy 2 25')
    print('\n/langstudy help \ao-- Display help')
    print('/langstudy stop \ao-- Close the program')

local function doLang(langNum, langIter)

    if langNum == 'stop' then
        run = false
    elseif langNum == 'help'
        doHelp()
    else
        for i=1, langNum do
            mq.cmd("/language "..i)
            print("Setting language to idx "..i)
            for l=1, langIter do
                local pause = 5 -- Half a second. Adjust as needed
                mq.cmdf('/timed %d /g %d: I am teaching you language %d, pause, l, i)
                pause = pause + 5 -- Increment during each loop
            end
            print('\nLanguage learning complete!')
        end
    end
end

mq.bind('/langstudy', doLang)

while run do
    mq.delay(1000)
end

Try this. It should keep the program running and allow you to enter how many languages you know and how many iterations you want.
 
/timed is not the answer, that’s essentially using the command engine to run a delayed command. You never need /timed in a script.

This looks like client lag to me. Are you running it on multiple characters? What are you using for logging? Are you running it in the background?

Other note if your step is 1 you don’t need to enter it. 1 is the default.
 
Lua:
mq = require('mq')

for i=1,4 do
    mq.cmd("/language "..i)
    print("Setting language to idx "..i)
    for l=1,100 do
        local pause = 5 -- Half a second. Adjust as needed
        mq.cmdf('/timed %d /g %d: I am teaching you language %d, pause, l, i)
        pause = pause + 5 -- Increment during each loop
    end
    print('\nLanguage learning complete!')
end

Can't hop into EQ to test, but I think this may help. Implemented a 'pause' variable to insert a '/timed' delay. This should run 100 times per language, and then finish with the 'Learning complete' message. Could be that the mq.delay wasn't delaying the portions that you were expecting.
That also run without any delays and finishes in less than 2 seconds. Also, it seems to skip iterations: Most unusual. :O

Code:
[Sat Jan 27 10:13:11 2024] You tell your party, '36: I am teaching you language 19'
[Sat Jan 27 10:13:11 2024] You tell your party, '35: I am teaching you language 19'
[Sat Jan 27 10:13:11 2024] You tell your party, '34: I am teaching you language 19'
[Sat Jan 27 10:13:11 2024] You tell your party, '92: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '91: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '90: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '89: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '88: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '87: I am teaching you language 22'
[Sat Jan 27 10:13:11 2024] You tell your party, '86: I am teaching you language 22'
 
/timed is not the answer, that’s essentially using the command engine to run a delayed command. You never need /timed in a script.

This looks like client lag to me. Are you running it on multiple characters? What are you using for logging? Are you running it in the background?

Other note if your step is 1 you don’t need to enter it. 1 is the default.
Just running on one toon. No unusual lag. Log is EQs own character log. Dont know how to run anything "in thr background"
 
BTW, this also misbehaves:

Lua:
mq = require('mq')
for i=1,4,1 do
    print("loop 1 "..i)
    mq.delay(1000)
    for l=1,100,1 do
        print("loop 2 "..l)
        mq.delay(300)
    end
end

as does:

Lua:
mq = require('mq')
for i=1,400,1 do
    print("loop 1 "..i)
    mq.delay(100)
end

I am a bit stumped. Maybe after a while mq.delay does not longer update to the correct timestamp?
 
I reduced the script to just this, and it still screws up on the delay... It runs as expected for the first 100 or so iterations,and then it just ZOOM done ready, in less than a few moments.

Lua:
mq = require('mq')

for l=1,1000 do
    mq.cmd("/g "..l.." I am teaching you languages, lucky sod!")
    mq.delay(250)
end
 
Oh yes, this seems like a bug. I did the same thing and it works as expected for a few rotations then moves on past. I eliminated the cmd calls, and got the same result.


Lua:
local mq = require('mq')
for i = 1, 1000 do
    print(i)
    mq.delay(250)
end
I would put an issue up so the devs can track this.
 
Doesn't help your exercise, but if you'd like to see how some other tools did it you can check here
Yeah, Rasham has taught my first toons and referred to this macro. It worked when he used it on me. But when I run it myself, it does not work as expected. Also downloaded it and ran it on another toon, who has all languages at max skill (so I actually ran it between two separate toons) and it did not work either. I wonder if he updated this macro at a later time to fix whatever does not work now.

Edit: talked to Rasham and he sent me his latest version of the macro. My toon has max skill in all learnable languages now, but that doesnt change that we found a bug. :) Kinda cool, my first time. LOL
 
Last edited:
I've had this happen before. I never figured out what caused it, but after reinstalling windows it has never happened again. Everyone told me I was crazy until I posted a video of it doing it with a longer delay so it was obvious when it started skipping it.

Only excuse I could ever come up with for it was another AMD processor bug like the one that let people run super speed (was bug in eq client) back during bulldozer days.

I guess I should note in my case it was skipping all delays and/or running them out of order. Like it might be /say hi /delay 1000 and it would run /delay 1000 then /say hi
 
its the luajit runtime compiler. After 60 iterations, its optimizing the inner oop which is causing the delay to fail.

You can fix it by putting the delay before the message. I won't be able to fix this right now as the problem is a bit more involved than I'd like it to be.
 
To illustrate what Brianiac is saying:

Code that fails to delay after 60 iterations
Code:
local mq = require('mq')
for i = 1, 1000 do
    print(i)
    mq.delay(250)
end


Code that works correctly
Code:
local mq = require('mq')
for i = 1, 1000 do
    mq.delay(250)
    print(i)
end
The key here is not to have the delay as the last execution in the loop.
Other alternatives are using a while loop or a repeat until loop
 
May have been fixed, but wouldn't this whole thing be better dealt with by a callback in mq.delay? Then just set the delay higher so that you increase the chance the callback ends it early?
 
So I don't want to say this bug isn't fixed, cause I'm not that confident in my coding. But when I run this script.. it skips delays no matter how they are formatted. But when I just copy and paste some random print message command after the delays, it starts working 100%. Here is the code, sorry if I have no idea what I'm talking about.

---Does not Work--- delays end sections.

Doesnt Work
Lua:
    -- Loop through the inventory slots (0-32) to find items to combine (skip gear slots 0-22)
    for i = 0, 32 do
        local item = mq.TLO.Me.Inventory(i)
        if item() and item.Name() == itemName and item.Stack() < maxStack then  -- Check Stack() for the current stack count
            local stackCount = item.Stack()
            PRINTMETHOD("Found partial stack in slot %d with %d items", i, stackCount)

            -- Look for another stack to combine into this one
            for j = i + 1, 32 do
                local otherItem = mq.TLO.Me.Inventory(j)
                if otherItem() and otherItem.Name() == itemName and otherItem.Stack() < maxStack then
                    local otherStackCount = otherItem.Stack()  -- Get number of items in this stack
                    local transferAmount = math.min(otherStackCount, maxStack - stackCount)

                    PRINTMETHOD("Found another stack in slot %d with %d items", j, otherStackCount)

                    -- Pick up the other stack
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', j - 22, otherItem.ItemSlot2() + 1)

                    -- Handle Quantity Window or check if item is on the cursor
                    local itemPickedUp = false

                    -- Add a small delay to allow the Quantity Window to open
                    mq.delay(500)  -- Delay for 500ms to give the Quantity Window time to open

                    -- If the Quantity Window opens, accept the quantity
                    if mq.TLO.Window("QuantityWnd").Open() then
                        PRINTMETHOD("Quantity window opened, confirming %d items", transferAmount)
                        mq.cmd('/notify QuantityWnd QTYW_Accept_Button leftmouseup')
                       
                        -- Delay until the Quantity Window closes, with a timeout to avoid infinite waiting
                        mq.delay(5000, function() return not mq.TLO.Window("QuantityWnd").Open() end)  -- Wait for the window to close

                        -- Check if an item is on the cursor after the window closes
                        if mq.TLO.Cursor.ID() ~= 0 then
                            itemPickedUp = true
                            PRINTMETHOD("Item found on the cursor after quantity confirmation.")
                        end
                    else
                        -- If no Quantity Window opens, check if an item is on the cursor directly
                        mq.delay(500, function() return mq.TLO.Cursor.ID() ~= 0 end)  -- Small delay to wait for item to appear on the cursor
                        if mq.TLO.Cursor.ID() ~= 0 then
                            itemPickedUp = true
                            PRINTMETHOD("Item found on the cursor after picking up the stack.")
                        end
                    end

                    -- If no item was picked up, print an error and exit
                    if not itemPickedUp then
                        PRINTMETHOD("Error: No item found on the cursor after attempting to pick up the stack.")
                        return  -- Exit if no item is picked up
                    end

                    -- Drop the item onto the target stack to combine them
                    PRINTMETHOD("Attempting to combine with stack in slot %d", i)
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', i - 22, item.ItemSlot2() + 1)

                    -- Wait until the cursor is clear, with a timeout to avoid infinite waiting
                    mq.delay(5000, function() return mq.TLO.Cursor.ID() == 0 end)  -- Wait for the cursor to clear
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Error: Cursor still not clear after 5 seconds. Exiting function.")
                        return  -- Exit the function if the cursor is not clear after timeout
                    end

                    -- Ensure that the cursor is cleared after stacking
                    local attempts = 0
                    while mq.TLO.Cursor.ID() ~= 0 and attempts < 5 do
                        PRINTMETHOD("Item still on cursor, returning to inventory (attempt %d)", attempts + 1)
                        mq.cmd("/autoinv")  -- Return the item to inventory
                        mq.delay(WaitTime1000)  -- Wait for the item to return to inventory
                       
                        -- Check again after delay
                        if mq.TLO.Cursor.ID() == 0 then
                            PRINTMETHOD("Cursor successfully cleared after %d attempts.", attempts + 1)
                            break  -- Exit the loop if the cursor is clear
                        end
                       
                        attempts = 0
                    end

                    -- If the cursor is still not clear after 5 attempts, throw an error and exit the function
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Error: Cursor cannot be cleared after 5 attempts. Exiting function.")
                        return  -- Exit the function
                    end

                    -- Update stack counts
                    stackCount = stackCount + transferAmount
                    PRINTMETHOD("Successfully stacked %d items from slot %d to slot %d", transferAmount, j, i)

                    -- If the current stack is full, continue stacking remaining non-max stacks
                    if stackCount >= maxStack then
                        PRINTMETHOD("Stack in slot %d is now full with %d items", i, stackCount)
                        break  -- Break out and find other non-max stacks to combine
                    end
                end
            end
        end
    end

    PRINTMETHOD("Finished stacking items for: %s", itemName)
end


---Does Work--- copy and pasted in a random print message after every delay

works
Lua:
    -- Loop through the inventory slots to find items to combine
    for i = 1, 32 do  -- 32 is the number of general inventory slots
        local item = mq.TLO.Me.Inventory(i)
        if item() and item.Name() == itemName and item.Stack() < maxStack then  -- Check Stack() for the current stack count
            local stackCount = item.Stack()
            PRINTMETHOD("Found partial stack in slot %d with %d items", i, stackCount)

            -- Look for another stack to combine into this one
            for j = i + 1, 32 do
                local otherItem = mq.TLO.Me.Inventory(j)
                if otherItem() and otherItem.Name() == itemName then
                    local otherStackCount = otherItem.Stack()  -- Use Stack() to get the number of items in this stack
                    local transferAmount = math.min(otherStackCount, maxStack - stackCount)

                    PRINTMETHOD("Found another stack in slot %d with %d items", j, otherStackCount)

                    -- Pick up the other stack

                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', j - 22, 0)  -- Pick up the other stack
                    coroutine.yield(mq.delay(100))  -- Wait for the item to be picked up
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Wait for the Quantity Window to open (if necessary)
                    coroutine.yield(mq.delay(500, function() return mq.TLO.Window("QuantityWnd").Open() end))
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Simulate clicking Accept on the Quantity Window if it opens
                    if mq.TLO.Window("QuantityWnd").Open() then
                        PRINTMETHOD("Quantity window opened, confirming %d items", transferAmount)
                        mq.cmd('/notify QuantityWnd QTYW_Accept_Button leftmouseup')
                        coroutine.yield(mq.delay(100))  -- Wait for the action to complete
                        PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                        -- Wait for the quantity window to close before proceeding
                        coroutine.yield(mq.delay(500, function() return not mq.TLO.Window("QuantityWnd").Open() end))
                    else
                        PRINTMETHOD("Quantity window did not open. Assuming full stack pickup.")
                    end

                    -- Combine with the current stack
                    PRINTMETHOD("Attempting to combine with stack in slot %d", i)
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', i - 22, 0)  -- Drop it on the destination stack
                    coroutine.yield(mq.delay(100))  -- Wait for the action to complete
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Ensure that the cursor is cleared after stacking
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Item still on cursor, returning to inventory")
                        mq.cmd("/autoinv")  -- If anything is left on the cursor, return it to inventory
                        coroutine.yield(mq.delay(100))
                        PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    end

                    -- Update stack counts
                    stackCount = stackCount + transferAmount
                    PRINTMETHOD("Successfully stacked %d items from slot %d to slot %d", transferAmount, j, i)

                    -- If the current stack is full, stop combining
                    if stackCount >= maxStack then
                        PRINTMETHOD("Stack in slot %d is now full with %d items", i, stackCount)
                        break
                    end
                else
                    PRINTMETHOD("No more matching stacks found.")
                end
            end
        end
    end

    PRINTMETHOD("Finished stacking items for: %s", itemName)
end
 
Usually when I am waiting for something, I create a while do loop with the delay inside.

Code:
while (something) do

mq.delay(500)

end

This is my delay in a LEM when I am navigating to some target:

Code:
  while mq.TLO.Navigation.Active() do

    mq.delay(10)

  end

This is my delay in the same LEM:

Code:
  while mq.TLO.Target.CleanName() ==  'a hidden barrel'  do

    mq.delay(10)

  end

/attack on was triggered prior to the delay loop. So once the target is dead, it is no longer a target (name changes to corpse).

Taken from your working example (i do not know if it is proper syntax):

Code:
while not mq.TLO.Window("QuantityWnd").Open() do

    mq.delay(10)

end

You trigger the window opening, go into the loop, and it loops delay until the window opens, then continues on.
 
So I don't want to say this bug isn't fixed, cause I'm not that confident in my coding. But when I run this script.. it skips delays no matter how they are formatted. But when I just copy and paste some random print message command after the delays, it starts working 100%. Here is the code, sorry if I have no idea what I'm talking about.

---Does not Work--- delays end sections.

Doesnt Work
Lua:
    -- Loop through the inventory slots (0-32) to find items to combine (skip gear slots 0-22)
    for i = 0, 32 do
        local item = mq.TLO.Me.Inventory(i)
        if item() and item.Name() == itemName and item.Stack() < maxStack then  -- Check Stack() for the current stack count
            local stackCount = item.Stack()
            PRINTMETHOD("Found partial stack in slot %d with %d items", i, stackCount)

            -- Look for another stack to combine into this one
            for j = i + 1, 32 do
                local otherItem = mq.TLO.Me.Inventory(j)
                if otherItem() and otherItem.Name() == itemName and otherItem.Stack() < maxStack then
                    local otherStackCount = otherItem.Stack()  -- Get number of items in this stack
                    local transferAmount = math.min(otherStackCount, maxStack - stackCount)

                    PRINTMETHOD("Found another stack in slot %d with %d items", j, otherStackCount)

                    -- Pick up the other stack
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', j - 22, otherItem.ItemSlot2() + 1)

                    -- Handle Quantity Window or check if item is on the cursor
                    local itemPickedUp = false

                    -- Add a small delay to allow the Quantity Window to open
                    mq.delay(500)  -- Delay for 500ms to give the Quantity Window time to open

                    -- If the Quantity Window opens, accept the quantity
                    if mq.TLO.Window("QuantityWnd").Open() then
                        PRINTMETHOD("Quantity window opened, confirming %d items", transferAmount)
                        mq.cmd('/notify QuantityWnd QTYW_Accept_Button leftmouseup')
                      
                        -- Delay until the Quantity Window closes, with a timeout to avoid infinite waiting
                        mq.delay(5000, function() return not mq.TLO.Window("QuantityWnd").Open() end)  -- Wait for the window to close

                        -- Check if an item is on the cursor after the window closes
                        if mq.TLO.Cursor.ID() ~= 0 then
                            itemPickedUp = true
                            PRINTMETHOD("Item found on the cursor after quantity confirmation.")
                        end
                    else
                        -- If no Quantity Window opens, check if an item is on the cursor directly
                        mq.delay(500, function() return mq.TLO.Cursor.ID() ~= 0 end)  -- Small delay to wait for item to appear on the cursor
                        if mq.TLO.Cursor.ID() ~= 0 then
                            itemPickedUp = true
                            PRINTMETHOD("Item found on the cursor after picking up the stack.")
                        end
                    end

                    -- If no item was picked up, print an error and exit
                    if not itemPickedUp then
                        PRINTMETHOD("Error: No item found on the cursor after attempting to pick up the stack.")
                        return  -- Exit if no item is picked up
                    end

                    -- Drop the item onto the target stack to combine them
                    PRINTMETHOD("Attempting to combine with stack in slot %d", i)
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', i - 22, item.ItemSlot2() + 1)

                    -- Wait until the cursor is clear, with a timeout to avoid infinite waiting
                    mq.delay(5000, function() return mq.TLO.Cursor.ID() == 0 end)  -- Wait for the cursor to clear
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Error: Cursor still not clear after 5 seconds. Exiting function.")
                        return  -- Exit the function if the cursor is not clear after timeout
                    end

                    -- Ensure that the cursor is cleared after stacking
                    local attempts = 0
                    while mq.TLO.Cursor.ID() ~= 0 and attempts < 5 do
                        PRINTMETHOD("Item still on cursor, returning to inventory (attempt %d)", attempts + 1)
                        mq.cmd("/autoinv")  -- Return the item to inventory
                        mq.delay(WaitTime1000)  -- Wait for the item to return to inventory
                      
                        -- Check again after delay
                        if mq.TLO.Cursor.ID() == 0 then
                            PRINTMETHOD("Cursor successfully cleared after %d attempts.", attempts + 1)
                            break  -- Exit the loop if the cursor is clear
                        end
                      
                        attempts = 0
                    end

                    -- If the cursor is still not clear after 5 attempts, throw an error and exit the function
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Error: Cursor cannot be cleared after 5 attempts. Exiting function.")
                        return  -- Exit the function
                    end

                    -- Update stack counts
                    stackCount = stackCount + transferAmount
                    PRINTMETHOD("Successfully stacked %d items from slot %d to slot %d", transferAmount, j, i)

                    -- If the current stack is full, continue stacking remaining non-max stacks
                    if stackCount >= maxStack then
                        PRINTMETHOD("Stack in slot %d is now full with %d items", i, stackCount)
                        break  -- Break out and find other non-max stacks to combine
                    end
                end
            end
        end
    end

    PRINTMETHOD("Finished stacking items for: %s", itemName)
end


---Does Work--- copy and pasted in a random print message after every delay

works
Lua:
    -- Loop through the inventory slots to find items to combine
    for i = 1, 32 do  -- 32 is the number of general inventory slots
        local item = mq.TLO.Me.Inventory(i)
        if item() and item.Name() == itemName and item.Stack() < maxStack then  -- Check Stack() for the current stack count
            local stackCount = item.Stack()
            PRINTMETHOD("Found partial stack in slot %d with %d items", i, stackCount)

            -- Look for another stack to combine into this one
            for j = i + 1, 32 do
                local otherItem = mq.TLO.Me.Inventory(j)
                if otherItem() and otherItem.Name() == itemName then
                    local otherStackCount = otherItem.Stack()  -- Use Stack() to get the number of items in this stack
                    local transferAmount = math.min(otherStackCount, maxStack - stackCount)

                    PRINTMETHOD("Found another stack in slot %d with %d items", j, otherStackCount)

                    -- Pick up the other stack

                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', j - 22, 0)  -- Pick up the other stack
                    coroutine.yield(mq.delay(100))  -- Wait for the item to be picked up
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Wait for the Quantity Window to open (if necessary)
                    coroutine.yield(mq.delay(500, function() return mq.TLO.Window("QuantityWnd").Open() end))
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Simulate clicking Accept on the Quantity Window if it opens
                    if mq.TLO.Window("QuantityWnd").Open() then
                        PRINTMETHOD("Quantity window opened, confirming %d items", transferAmount)
                        mq.cmd('/notify QuantityWnd QTYW_Accept_Button leftmouseup')
                        coroutine.yield(mq.delay(100))  -- Wait for the action to complete
                        PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                        -- Wait for the quantity window to close before proceeding
                        coroutine.yield(mq.delay(500, function() return not mq.TLO.Window("QuantityWnd").Open() end))
                    else
                        PRINTMETHOD("Quantity window did not open. Assuming full stack pickup.")
                    end

                    -- Combine with the current stack
                    PRINTMETHOD("Attempting to combine with stack in slot %d", i)
                    mq.cmdf('/itemnotify in pack%d %d leftmouseup', i - 22, 0)  -- Drop it on the destination stack
                    coroutine.yield(mq.delay(100))  -- Wait for the action to complete
                    PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    -- Ensure that the cursor is cleared after stacking
                    if mq.TLO.Cursor.ID() ~= 0 then
                        PRINTMETHOD("Item still on cursor, returning to inventory")
                        mq.cmd("/autoinv")  -- If anything is left on the cursor, return it to inventory
                        coroutine.yield(mq.delay(100))
                        PRINTMETHOD("Attempting to pick up stack from slot %d", j)
                    end

                    -- Update stack counts
                    stackCount = stackCount + transferAmount
                    PRINTMETHOD("Successfully stacked %d items from slot %d to slot %d", transferAmount, j, i)

                    -- If the current stack is full, stop combining
                    if stackCount >= maxStack then
                        PRINTMETHOD("Stack in slot %d is now full with %d items", i, stackCount)
                        break
                    end
                else
                    PRINTMETHOD("No more matching stacks found.")
                end
            end
        end
    end

    PRINTMETHOD("Finished stacking items for: %s", itemName)
end
try replacing the prints you added with an extra mq.delay(1) and see if your delays start working.
 
Question - Issue with nested for loop

Users who are viewing this thread

Back
Top
Cart