Using a C_Timer.After and how to add a time delay
Hi all
I am the author of a small addon Aardvark, I am self taught with no background in coding at all. I have a couple of issues that I have not been able to solve that I would like some guidance for. Q1. I have a sell function that scans my bags and auto sells grey items and items that a player adds to their sell list when they visit a vendor, (line 1910). When the vendor opens the sell function runs however it does not always sell all the grey and listed items, I get the "that object is busy" message and I have to spam the sell button to clean out all of the remaining items. I have tried adding a C_Timer.After in the sell function wrapped around the actual sell chunk, (line 1942) using: Code:
C_Timer.After(1, function(self) Code:
2x Aardvark\Aardvark-AR 9.0.8.3.lua:1951: attempt to perform arithmetic on local 'itemValue' (a nil value) How do I add a timer delay in the sell function to ensure all the items sell properly? Is there a better way to ensure all the items sell other than using a timer? Q2. I have used a timer in my add item by itemID function, (line 5987), without issue. I have tried to use a time less than 1 sec but it does not seem to reduce the time below 1 sec. Is there a way of reducing the timer to less than 1 sec or is there a alternative way of adding a delay that will allow a sub 1 sec timer? Looking forward to some help and understanding of what I am doing wrong. |
The function called by C_Timer.After can't have parameter so you can't pass self to it. You would have to do it by another mechanism.
If it's not that then the code you posted does not include the part causing the error ie. the parts where local itemValue is initialised and subsequently used at line 1951. |
Hi Fizzlemizz
Thank for your help, I had linked the code instead of the paste, I have now added the correct link to my paste :) I am not sure of what you mean in regards to passing self as I have got it to work in the add item by itemID function, (line 5985), with the exact same syntax. What do you mean as I thought that self used the following code as the parameter? |
You do the check
Code:
if (checkItemOnGlobalProtectList(currentItemID) or checkItemOnCharacterProtectList(currentItemID)) and Code:
if not itemValue or (checkItemOnGlobalProtectList(currentItemID) or checkItemOnCharacterProtectList(currentItemID)) and |
Fizzlemizz means that C_Timer only accepts a function object with no arguments. You're not using self anyways, so just function() will do.
As for your error, what is the item causing the error? Are you sure itemValue is not nil? Some items have no value and there's no guarantee it's zero. Concerning C_Timer delay, the lowest value is determined by your fps. It can only delay something as quick as a single frame. If you are consistently running at 60fps, the lowest value is 1/60 or 0.016. If your computer is average or below and it gets, say, 10fps, the lowest value is 1/10 or 0.1. The reason behind this is that Lua only runs frame-by-frame. Once the next frame is processed, the low value delay has already passed. |
Hi Fizzlemizz and Kanegasi
Thanks for your help, I am going to try a few different fixes using your advice. I will add a reply to this thread when I get it working or when I need more help. Cheers for your replies. |
Hi all
I still have not been able to crack this nut, no matter where I put the timer the sell does not slow. I have also tried moving the timer into a separate function that I call in the sell with no success. Is there any way to implement a delay between each loop in the sell function? |
You have a scope problem. The values of the variables inside the function you pass to C_Timer.After are still being modified by the main for-loop. You need to make local copies of each one used so that they cannot be modified. Basically you need to take a snapshot of their values at that time.
Some notes: 1. You seem to be leaking globals like crazy, but maybe they're declared at the top of the file. 2. I don't understand your use of the twelveCounter variable. It seems to be to limit the number of items/stacks you try and sell at once but you check it inside the function and not outside before creating the function. That seems very counter productive to me. 3. You are assuming you are selling items when that may not be the case. To keep track of each individual sell like you are trying you will almost need to make a list of items you can sell, try to sell them, and then see which items actually sold. Try replacing your avkSell function with this (dry coded): Code:
local avkSell |
You should really just use the event BAG_UPDATE_DELAYED, which fires after you've sated the amount of requests you can send to the server to sell items, then continue trying to sell the rest of the items when that event fires. Keep repeating that process until you've sold all the items in the list. Using a timer is always going to be unreliable because player or server latency can affect the outcome when it's asynchronous.
|
Hi Vrul & MunkDev
Thanks for your help. I decided to go with Vrul's solution; with the event the item totals are still counted even when the item did not actually sell, so the list method worked out better for me. I do have a question about Vrul's solution; I can see the do but how does it work outside of a function? Also the bleeding local's is due to the max limit of 200 local's used, so I am in the process of moving variables into tables to resolve this issue. |
Quote:
In this case the variable profitCounter, the table sellList, and the function avkSellList cannot be seen by the rest of your code and also cannot be modified if you happen to accidentally reuse one of those variable names. Quote:
|
Hi Vrul
Thanks for that explanation, it does raise a question regarding the do, Is this a method that should be utilised for every addon file or just in limited circumstances? Also in the case of splitting an addon into more than one file I have tried to work this out but have yet been successful, (though I am sure I will be able to work this out eventually), why do some addon use one localisation file, whilst others have a file for each localisation? is this the best method or is it just a personal choice? I think that using a separate file for all variables may be useful when tweaking numbers and print strings but I too wonder if this is a good idea or not. Once again thanks for your help. |
All times are GMT -6. The time now is 08:14 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI