View Single Post
04/25/24, 03:14 PM   #10
Vilkasmanga
AddOn Author - Click to view addons
Join Date: Dec 2023
Posts: 8
Originally Posted by Baertram View Post
Yes, having a timer run every 15s as in your case is fine.
Just make sure the timer unregisters properly once your bounty is gone, and does not register with a new name (reuse the same name so it "overwrites" old exisitng ones!) once next bounty comes up.
Great, thanks. I think I coded this correctly (thanks to your and others' help, ofc)

Code:
function FarmersToolkit.BountyCheck()

        local CurBounty=GetFullBountyPayoffAmount();
        local identifier="FarmersToolkit-BountyCheck";

        -- In case this is the first time we've been called this session...
        if ( type(FarmersToolkit.BountyValue) == "nil" ) then
                FarmersToolkit.BountyValue=0;
        end

        if ( CurBounty  == 0 ) then  -- We have no bounty, whew!
                if ( FarmersToolkit.BountyValue > 0 ) then -- We may have a outstanding timer event tho
                        EVENT_MANAGER:UnregisterForUpdate(identifier)
                end

                FarmersToolkit.BountyValue = 0; -- Zero out internal counter / tracker
                FarmersToolkit.UpdateActivities(); -- Update the screen
                dft_debug("BountyCheck indicates you are safe as of " .. os.time() .. "!"); -- Debug
                dft("Bounty appears cleared, cancelling timers - happy hunting."); -- Announce
        else
                if ( ( FarmersToolkit.BountyValue == 0 ) and ( CurBounty > 0 ) ) then -- This is the onset of a bounty
                        dft("Bounty detected (" .. CurBounty .. "), updates posted to screen until cleared.");
                end
                FarmersToolkit.UpdateActivities();
                FarmersToolkit.BountyValue=CurBounty;
                dft_debug("BountyCheck = " .. CurBounty .. ", timer set for 15 seconds as of " .. os.time());
                EVENT_MANAGER:UnregisterForUpdate(identifier)  -- Just in case
                EVENT_MANAGER:RegisterForUpdate(identifier, 15000, FarmersToolkit.BountyCheck)
        end

end -- FarmersToolkit.BountyCheck
  Reply With Quote