View Single Post
02/26/23, 01:54 PM   #13
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Originally Posted by sinnereso View Post
everything looks like it should be working but I dont see how you could run complex functions on a /script. Heres everything related as it is at this moment.. ill leave it this way and give up later if i cant get it working. im getting a headache.

Code:
function RidinDirty.PlayerActivated()
        if autoRechargeToggle == "enabled" then
		EVENT_MANAGER:RegisterForEvent("RidinDirty", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, RidinDirty.InventoryUpdate)
	end
end

-- Auto recharge weapons with soulgems
function RidinDirty.InventoryUpdate(eventCode, bagId, slotId, isNewItem, itemSoundCategory, inventoryUpdateReason, stackCountChange)
	if bagId ~= BAG_WORN then return end
	if inventoryUpdateReason == 3 and not IsUnitDead(PLAYER) then
		RidinDirty.GetGems()
	end
end

function RidinDirty.GetGems()
	for slotId = 0, GetBagSize(BAG_BACKPACK) do
		if IsItemSoulGem(SOUL_GEM_TYPE_FILLED, BAG_BACKPACK, slotId) then
			local gemSlot = slotId
			RidinDirty.Recharge()
		end
	end
end

function RidinDirty.Recharge()
    local minimumWeaponCharge = 97
	local weapons = {
        EQUIP_SLOT_MAIN_HAND,
        EQUIP_SLOT_OFF_HAND,
        EQUIP_SLOT_BACKUP_MAIN,
        EQUIP_SLOT_BACKUP_OFF
    }
	for _, weapon in ipairs(weapons) do
		df("found weapon %s", weapon)
		local charge, maxCharge = GetChargeInfoForItem(BAG_WORN, weapon)
		if charge <= minimumWeaponCharge then --and IsItemChargeable(BAG_WORN, weapon) then --and not maxCharge == 0 then
			ChargeItemWithSoulGem(BAG_WORN, weapon, BAG_BACKPACK, gemSlot)
		end
		local charged, maxCharged = GetChargeInfoForItem(BAG_WORN, weapon)
		if charged > charge then
			df("should have been charged")
		end
	end
end

function RidinDirty.RechargeToggle()
	local autoRechargeToggle = RidinDirty.savedVariables.autoRecharge
	if autoRechargeToggle == nil or autoRechargeToggle == "" or autoRechargeToggle == "disabled" then
		autoRechargeToggle = "enabled"
		RidinDirty.savedVariables.autoRecharge = autoRechargeToggle
		EVENT_MANAGER:RegisterForEvent("RidinDirty", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, RidinDirty.InventoryUpdate)
		df("|c9900FF[RidinDirty]|r Auto Recharge: %s", autoRechargeToggle)
	else
		autoRechargeToggle = "disabled"
		RidinDirty.savedVariables.autoRecharge = autoRechargeToggle
		EVENT_MANAGER:UnregisterForEvent("RidinDirty", EVENT_INVENTORY_SINGLE_SLOT_UPDATE)
		df("|c9900FF[RidinDirty]|r Auto Recharge: %s", autoRechargeToggle)
	end
end

SLASH_COMMANDS["/ridindirty_recharge"] = RidinDirty.RechargeToggle

The function ChargeItemWithSoulGem uses the bagId and slotIndex of BAG_WORN that you want to charge, and the bagId and slotIndex of the soulgem in your bags.

Lua Code:
  1. ChargeItemWithSoulGem(bagId,slotIndex,gem.bag,gem.index)

Your code should work fine if the function is called properly and the soulgem bagId and slotIndex are correct!
I'd check the soulgems tuff to be the correct ones, via debug messages!

In your function RidinDirty.Recharge() gemSlot is not known! Ths cannot work.
You specify it in your function RidinDirty.GetGems() as local so it's ONLY known inside that function.


You should do it like this instead:
Pass in the slotId as "gemSlot" to your function RidinDirty.Recharge
or define 1 local gemslot at the top of your lua file and then set it at function RidinDirty.GetGems() and use it at RidinDirty.Recharge

local variables are only available in their closure, means inside the if ... end or inside the function ... end or inside a for ... do end loop etc.
If you want it to be avaialable in all functions of a file you need to define it at the top of the file, outside of all functions etc.
Or use it as RidinDirty.gemSlot, added to your global variable!

Lua Code:
  1. function RidinDirty.GetGems()
  2.     for slotId = 0, GetBagSize(BAG_BACKPACK) do
  3.         if IsItemSoulGem(SOUL_GEM_TYPE_FILLED, BAG_BACKPACK, slotId) then
  4.              
  5.             return RidinDirty.Recharge(slotId)
  6.  
  7.         end
  8.     end
  9. end
  10.  
  11. function RidinDirty.Recharge(gemSlot)
  12.  if gemSlot == nil then return false end
  13.  ...--your other code here
  14.   --optional: if everything went well return true after charging
  15.   return true
  16. end

I personally would NOT combine GetGems and Recharge so they always will be run together. Just run GetGems once before usage, from e.g. function RidinDirty.Recharge, and then recharge the items that you want to.
Not the other way around: Call Recharge from GetGems.

Last edited by Baertram : 02/26/23 at 02:04 PM.
  Reply With Quote