View Single Post
04/26/15, 06:29 AM   #6
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Phinix View Post
You could do something like what I did with my Fix Weapon Swap Effects mod and set up a "spam catcher" for event spam:

Code:
local ActiveWeapon = 0
local catchspam = 0

local function DoStuff(WeaponPair)
	Stuff done here that I only want to do if a real weapon swap happened...
	ActiveWeapon = WeaponPair
end

local function CheckStatus(WeaponPair)
	catchspam = catchspam + 1
	if catchspam == 3 then
		if WeaponPair ~= ActiveWeapon then
			DoStuff(WeaponPair)
		end
		catchspam = 0
	end
end

local function OnWeaponSwap(eventCode, WeaponPair, locked)
	CheckStatus(WeaponPair)
end

EVENT_MANAGER:RegisterForEvent(SomeAddon.Name, EVENT_ACTIVE_WEAPON_PAIR_CHANGED, OnWeaponSwap)
I find this works well for my purposes as it has the lowest possible overhead, which is important when firing something on weapon swap. Basically the three events registered go:

1) Ignored.
2) Ignored.
3) Do stuff.

I pass WeaponPair to the CheckStatus event so that I can check a global I store its value in to see if it has changed. This was important for me because Sorcerer ultimate Overcharge is considered a weapon swap for this event but keeps the current internal weapon pair ID, so I can tell not to refresh graphics which cause a Sorcerer to pull out a weapon for their ultimate which looks broken.

EDIT:

EVENT_ACTION_SLOTS_FULL_UPDATE is good if you don't need to know what weapon set the person equipped or if it changed. I used it before, and it is a simple Boolean if the bar change was triggered by a weapon swap.

The thing is, you will still want a spam filter. That event fires 5 times instead of just 3!
In some cases your catchspam doesn't work. I didn't really check if it's because event EVENT_ACTIVE_WEAPON_PAIR_CHANGED is fired more or less times then expected, but I have changed it back to EVENT_ACTION_SLOTS_FULL_UPDATE and it seems to be working better for me.

Lua Code:
  1. local activeWeapon = ACTIVE_WEAPON_PAIR_NONE
  2.  
  3. local function SwapEffects()
  4.     local helmStatus = tonumber(GetSetting(SETTING_TYPE_IN_WORLD, IN_WORLD_UI_SETTING_HIDE_HELM))
  5.     SetSetting(SETTING_TYPE_IN_WORLD, IN_WORLD_UI_SETTING_HIDE_HELM, 1 - helmStatus)
  6.     SetSetting(SETTING_TYPE_IN_WORLD, IN_WORLD_UI_SETTING_HIDE_HELM, helmStatus)
  7. end
  8.  
  9. local function OnWeaponSwap(eventCode, isHotbarSwap)
  10.     if isHotbarSwap and not IsBlockActive() then
  11.         local weapon = GetActiveWeaponPairInfo()
  12.         if activeWeapon ~= weapon then
  13.             SwapEffects()
  14.             activeWeapon = weapon
  15.         end
  16.     end
  17. end
  18.  
  19. local function Init(eventCode)
  20.     activeWeapon = GetActiveWeaponPairInfo()
  21. end
  22.  
  23.  
  24. EVENT_MANAGER:RegisterForEvent(SomeAddon.Name, EVENT_ACTION_SLOTS_FULL_UPDATE, OnWeaponSwap)
  25. EVENT_MANAGER:RegisterForEvent(SomeAddon.Name, EVENT_PLAYER_ACTIVATED, Init)
  Reply With Quote