Script timing for getting data from API

Viewed 35

I am trying to write a macro that combines /use on a trinket then calls the WoW API and outputs some data.

/use 14
/run local d=string.format("%.2f",GetDodgeChance()); print("Dodge Trinket used, dodge at:",d);

This works fine apart from there seems to be a timing problem with using the trinket and then getting the updated dodge chance from the API. When I click once, the trinket activates it shows the dodge chance without the trinket buff. If I click immediately again it then shows the correct value, including the buff.

Is there some timing issue, such as the first /use command not firing until the macro ends? How do I ensure the GetDodgeChance() call includes the trinket buff?

1 Answers

Your game client needs to send the trinket use to the server, and get a confirmation back that you received the buff which changes your stats.

However, execution of code can be delayed with C_Timer.After

Your example would become:

/use 14
/run C_Timer.After( 0.5, function() local d=string.format("%.2f",GetDodgeChance()); print("Dodge Trinket used, dodge at:",d) end)

with half a second delay, which should both be long enough for the Dodge function to return the correct (=anticipated) value, and appear as "almost instant" to human perception.
You can of course tweak that value, try shorter delays - e.g. 0.1 seconds should also be quite long enough for the calculation to be correct, but as this involves communicating with the server, it might work fine 99% of the time for a very short value of delay, and then there's a random minor lag spike, not even something you notice in gameplay, but the info about the buff and changed dodge chance reaches your client a few milliseconds too late.

Related