Adding a delay to the execution of a loop
Hi,
I use the following code to sell all items in a given bag, which is triggered when I click a Sell button for that bag. Code:
function SellAllInBag(bag) What I am looking for is to add some sort of a delay (e.g. 0.1 seconds) to the loop, so that it will sell all items with one click without getting stuck. Something like: Code:
function SellAllInBag(bag) Thanks. |
I use C_Timer.After for that.
https://github.com/zorker/rothui/blo.../core.lua#L118 |
I have post a new framework named Scorpio, it'd provide a code style like :
Lua Code:
But you couldn't use a framework only for one feature, so there is another way : Lua Code:
Since the code is simple, you can use zork's solution, but it's better to use the coroutine when it's too hard to track the process states. |
Here's my take on using C_Timer.After for this:
Lua Code:
I don't know how expensive this is, but it will schedule several timers with increasing delays all at once. The first action will be 0.1 seconds after the function is called, next will be 0.2 seconds, and so on. |
I know that, 1:1, it is less efficient/more expensive than C_Timer.After, but would C_Timer.NewTicker be fine for this? Or would it still be less efficient than scheduling dozens of C_Timer.After?
Though it looks like NewTicker doesn't let you pass arguments to the function being called, so you'd have to do something silly like using external variables instead. PoC: lua Code:
Works as expected if I run DoThing([bag number]). |
Lua Code:
Here's a robust solution for you. Using C_Timer.After is bad for multiple reasons, one of which is asynchronous execution. Unlike the others, this takes into account if you're actually interacting with an NPC and it is safe to use even if you have massive lag. It's a bit over-engineered, but I think this is your best bet. You can also run this for multiple bags and it'll queue them up and finish them off in order. The way it works is it attempts to sell as many items as possible at once, counting each item that goes through, but when the server throttles your request, it'll queue a new attempt to sell as many items as possible from the items you have remaining in your bag. It will look like chunks of items are disappearing out of your bags. Usage: Lua Code:
|
Thank you all for your inputs.
I tried the following, based on zork's sample code: Code:
function SellAllInBag(bag) I tried Kanegasi's example, it sold only the first item in the bag and gave this error: Code:
Message: Interface\AddOns\SellAll\SellAll.lua:3: Usage: C_Timer.After(seconds, func) Code:
Message: attempt to yield across metamethod/C-call boundary MunkDev, thank you for such a detailed solution. I always try to keep things as simple as possible, and in a way that I can understand and modify when needed. Can't tell if Ammako's solution is more or less efficient but it seems more preferrable to me due to its simplicity. Just one thing: Will Ammako's timer(s) continue to tick in the background after all the items are sold in the bag? That wouldn't be ideal. |
It's a terrible solution for the reasons I mentioned. It can also break in the exact same way you wanted to work around if you're having a massive lag spike.
You need more than this to be able to handle server throttling, not causing errors due to attempting to use inventory items from code and handling when you step away from the NPC. It's not more complicated than it actually needs to be for the feature you want, especially if you're going to release it as an addon for others to use. My solution is more efficient because it doesn't use C_Timer and actually reacts to the event that tells you when your request is throttled.
|
Your first attempt freezes the game because you recursively keep calling the function from within itself. Infinite loop.
Go with MunkDev's, they're way more experienced and know what they're talking about. :p |
I realized the problem with my code. I forgot C_Timer.After needs a direct function and cannot "call" one, as in it needs an anonymous function() end or the name of a function with no parenthesis. We're passing a function in as an argument to the After() function. Try this:
Lua Code:
I am sort of confused as to how the original even sold that first item. If this works, as in it sells a bunch of items, but it doesn't sell everything, just adjust the slot/10. Even though the slot variable changes, the delay between each action is equal to the first delay, which is 1/10. If you wanted a delay of 0.5 sec, you would use slot/2. |
Here is my approach: https://github.com/zorker/rothui/blo.../rSellPoor.lua
|
All times are GMT -6. The time now is 06:43 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI