Thread Tools Display Modes
01-16-13, 12:38 PM   #1
CrushBug
A Murloc Raider
Join Date: Oct 2006
Posts: 9
Question Pet information availability and login timing

Greetings:

I am working on a small data export mod to dump out all the information on my Battle Pets. For the most part, the code is working just fine, but I think the problem I am having has to do with timing. It seems like the complete Battle Pet info isn't fully ready by the time the Player events are fired. Here is the function that is handling the pet info.

Code:
function CBPetList_GetBattlePets()
	local numPets, numOwned = C_PetJournal.GetNumPets(false)
	CBPetCount = numOwned
	for iLoop = 1, numPets do
		local petID, speciesID, owned, customName, level, favorite, isRevoked, speciesName, icon, petType, companionID, tooltip, description, isWild, canBattle, isTradeable, isUnique = C_PetJournal.GetPetInfoByIndex(iLoop, false)
		local health, maxHealth, power, speed, rarity = C_PetJournal.GetPetStats(petID)
		local isFavorite = C_PetJournal.PetIsFavorite(petID)
		if owned then
			if CBPetListTable[speciesID] == nil then
				CBPetListTable[speciesID] = {}
			end
			CBPetListTable[speciesID]["name"] = speciesName
			CBPetListTable[speciesID]["customName"] = customName
			CBPetListTable[speciesID]["favorite"] = isFavorite
			CBPetListTable[speciesID]["rarity"] = rarity
			CBPetListTable[speciesID]["level"] = level
			CBPetListTable[speciesID]["maxHealth"] = maxHealth
			CBPetListTable[speciesID]["power"] = power
			CBPetListTable[speciesID]["speed"] = speed
		end
	end
end
CBPetCount and CBPetListTable are Saved Variables.

I have a slash command that can manually run this function. I have the following events registered and each of the COMPANION_/PLAYER_ events call the above function.

Code:
CBPetList:RegisterEvent("ADDON_LOADED")
CBPetList:RegisterEvent("COMPANION_LEARNED")
CBPetList:RegisterEvent("PLAYER_ENTERING_WORLD")
CBPetList:RegisterEvent("PLAYER_ALIVE")
If I issue my "/petlist count" command right after getting into the game, it usually prints out 0 (it once said the proper count, but never again). This would be showing the Battle Pet count from the above events.

If I run "/petlist update" it spits out the proper Battle Pet count.

My question is, is there some better event I should be using like (joke) "PLAYER_DATA_FINALLY_LOADED" or is there some way to delay-fire the calling of that function? Or is there some other fundamental mistake I am making?

CB
  Reply With Quote
01-16-13, 04:43 PM   #2
CrushBug
A Murloc Raider
Join Date: Oct 2006
Posts: 9
Interesting results of some debug text. I put prints in the different Events from above, and called the CBPetList_GetBattlePets() function in each one. Here is the printing results:

Addon loaded message.
PLAYER_ENTERING_WORLD message, 0 pets.
PLAYER_ALIVE message, 0 pets.
Manually executed update command, 431 Pets.

So it seems that the player connecting events are firing before the Pet info is loaded to the account. I am going to poke around and see if there is another, later firing event, that I can try.

Again, if I am misunderstanding something, I would love to have someone point it out to me.
  Reply With Quote
01-16-13, 05:43 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
None of the events you're currently registering for have anything to do with the pet journal. You need to use PET_JOURNAL_LIST_UPDATE instead.

Also, if your addon needs to change any pet list filters (eg. un-filter the list so can see all pets) you should be aware that changing a filter triggers another PET_JOURNAL_LIST_UPDATE event, so you need to set a flag when you start responding to the event (before you change any filters), ignore any events that fire while the flag is set, and clear the flag when you're done responding to the event.

Code:
local processing

local frame = CreateFrame("Frame")
frame:RegisterEvent("PET_JOURNAL_LIST_UPDATE")
frame:SetScript("OnEvent", function(self, event, ...)
    -- ignore this event if we're still processing the first one
    if processing then
        return
    end

    -- set the flag
    processing = true

    -- clear any filters (and optionally save their state)

    -- scan the list and do whatever with the data

    -- restore filters (optional, but recommended so you don't screw with the user's display settings)

    -- clear the flag
    processing = nil
end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-16-13, 07:40 PM   #4
CrushBug
A Murloc Raider
Join Date: Oct 2006
Posts: 9
Ah, thanks for that bit of insight. I had made the assumption that the pet information was usually available by the time I got control of the interface, that it was something that was loaded up first thing. I am going to go play with it some more.
  Reply With Quote
01-16-13, 09:54 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You could also just use LibPetJournal-2.0, which will give you a nice normal API for interacting with the pet journal and getting info about pets, instead of the horribly designed and completely nonsensical Blizzard API. I can only imagine how many drugs were involved in that bit of programming...
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-16-13, 10:55 PM   #6
CrushBug
A Murloc Raider
Join Date: Oct 2006
Posts: 9
Thanks, I will look into using that. I did some more debugging and found out some interesting things.

Filtering in the pet journal had no effect on the C_PetJournal.GetNumPets for loop that I was using. I saw my chat print pet count spit out '431' many times while I did filtering and searching, so it turns out I didn't need to handle that situation at all.

PET_JOURNAL_LIST_UPDATE fires about 3 times at first and there are still 0 pets from the C_PetJournal.GetNumPets API call. I added a 'previousCount' variable, to only do a chat print when the number of pets were greater than 0 and different from the last call. This worked out pretty well.

My use of 'speciesID' as a unique pet identifier in the CBPetListTable was a mistake. This actually was unintentionally giving me a count of unique pets because while I had 2 Arcane Eye pets (one Rare, one Common), I was only writing out the Common one. Another good reason to look into that Lib you mentioned, I guess.

Thanks again for your help!
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Pet information availability and login timing


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off