Quantcast WoWInterface - View Single Post - Summoning Companions Via script
View Single Post
08-17-12, 07:12 PM   #5
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,056
Good grief. Alright, well as far as I can tell "C_PetJournal.SummonPetByID" is the only function currently working that will actually summon a companion, and the ID it uses appears to only be obtainable through the pet journal functions.

However.. the pet journal itself isn't loaded until the frame is opened in-game (shift+p) and calling some of the pet journal functions before it loads has managed to fatally crash my client several times.

Apparently calling C_PetJournal.SetSearchFilter in a way that returns no results twice in a row like
Lua Code:
  1. /run C_PetJournal.SetSearchFilter('xx');C_PetJournal.SetSearchFilter('xx')
will just immediately crash the game, but only if you haven't loaded the pet journal yet.

Calling C_PetJournal.ClearSearchFilter() in between seems to prevent this from happening, and even though setting the search filter isn't strictly necessary, it does leave fewer things to iterate over.

Here's my second crack at the function, and I haven't managed to break this one but you never know.
Lua Code:
  1. function CallPetByName(petName)
  2.     C_PetJournal.SetFlagFilter(LE_PET_JOURNAL_FLAG_COLLECTED, true) -- displays pets we do have
  3.     C_PetJournal.SetFlagFilter(LE_PET_JOURNAL_FLAG_NOT_COLLECTED, false) -- ignores pets we don't have
  4.     C_PetJournal.ClearSearchFilter() -- prevents the game from crashing
  5.     C_PetJournal.SetSearchFilter(petName) -- filter by name
  6.     local numPets = C_PetJournal.GetNumPets(false)
  7.     for i=1,numPets do
  8.         local petID, _, _, _, _, _, _, name = C_PetJournal.GetPetInfoByIndex(i, false)
  9.         if name and name:lower() == petName:lower() and C_PetJournal.PetIsSummonable(petID) and petID ~= C_PetJournal.GetSummonedPetID() then
  10.             return C_PetJournal.SummonPetByID(petID)
  11.         end
  12.     end
  13.     print('Failed to summon '..petName)
  14. end
I'm not really sure why you would need to summon a pet based on its name though, it seems like an odd request.