LuaSlinger»Change Action Bar Automatically Based On Target Distance

Change Action Bar Automatically Based On Target Distance

Change the action bar automatically based on the target's distance (advanced)

Description: Unfortunately Blizzard provides no event we can catch to know when the target crosses into mele range, so in this case we constantly poll the target's distance. The way we poll is by hooking the PlayerFrame_OnUpdate function, which is a built-in WoW function that gets called every screen refresh. We save the original function and call it every time, but we do our thing only once every .3 seconds. In this example I chose to use page 1 for mele and page 3 for ranged. Put this code in your Library and it will automatically start working. Remember: LuaSlinger gives you rope... don't hang yourself :-).
How to use: Just put this in your Library and Reload, it will automatically start working.

-- Update action bars based on target's distance.  Hook PlayerFrame's OnUpdate to do this.

function stkRangeCheck()
    if (UnitName("target")) then
      local desiredPage
      if (CheckInteractDistance("target", 1)) then
        desiredPage = 1
      else
        desiredPage = 3
      end
      if (CURRENT_ACTIONBAR_PAGE ~= desiredPage) then
        CURRENT_ACTIONBAR_PAGE = desiredPage
        ChangeActionBarPage()
      end
    end
end

function stkRangeCheckOnUpdate(elapsed)
  if (GetTime() >= stkLastRangeCheck + .3) then
    stkLastRangeCheck = GetTime()
    stkRangeCheck()
  end
  stkOriginalPlayerFrameOnUpdate(elapsed)
end

if (stkOriginalPlayerFrameOnUpdate == nil) then
-- only want to do this once
  stkOriginalPlayerFrameOnUpdate = PlayerFrame_OnUpdate
  stkLastRangeCheck = GetTime()
end

-- hook PlayerFrame_OnUpdate
PlayerFrame_OnUpdate = stkRangeCheckOnUpdate