View Single Post
05/16/23, 06:41 AM   #7
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Lua Code:
  1. --Get the currently logged in account's characters as table, with the name as key and the characterId as value,
  2. --or the characterId as key and the character name as key (depending in boolean parameter keyIsCharName)
  3. --Function to get all characters of the account, ID and name.
  4. --Returns a table with 2 possible variants, either the character ID is key and the name is the value,
  5. --or vice versa.
  6. --Parameter boolean, keyIsCharName:
  7. -->True: the key of the returned table is the character name
  8. -->False: the key of the returned table is the unique character ID (standard)
  9. local function GetCharactersOfAccount(keyIsCharName)
  10.     keyIsCharName = keyIsCharName or false
  11.     local charactersOfAccount
  12.     --Check all the characters of the account
  13.     for i = 1, GetNumCharacters() do
  14.         local name, _, _, _, _, _, characterId = GetCharacterInfo(i)
  15.         local charName = zo_strf(SI_UNIT_NAME, name)
  16.         if characterId ~= nil and charName ~= "" then
  17.             if charactersOfAccount == nil then charactersOfAccount = {} end
  18.             if keyIsCharName then
  19.                 charactersOfAccount[charName]   = characterId
  20.             else
  21.                 charactersOfAccount[characterId]= charName
  22.             end
  23.         end
  24.     end
  25.     return charactersOfAccount
  26. end


Use table with the charname as choices (shows the names of the char as dropdown entries in the LAM dropdown)
AND IMPORTANT: build your choicesValues table which uses the internal characterId then as key, and not the name as the name can be changed but characterId is unique (stays the same after rename) per server.

So basically call the above function once with false -> returned table will be tab[characterId] = charName
Then loop over tab with
local choices = {}
local choicesValues = {}
local count = 0
for charId, charName in pairs(tab) do
count = count + 1
choices[count] = charName
choicesValues[count] = charId
end

Else you will get into trouble like always when you depend on translatable or changeable strings!

Last edited by Baertram : 05/16/23 at 06:45 AM.
  Reply With Quote