API UnitGUID

From Wowpedia
Jump to: navigation, search

Returns the GUID of the specified unit.

guid = UnitGUID("unit")

Arguments

unit 
String (unitId) - unit to look up the GUID of.

Returns

guid 
String (GUID) - a string containing the hexadecimal representation of the unit's GUID, e.g. "Creature-0-1133-870-141-71953-0000432FBD" (Xuen, on Timeless Isle), or nil if the unit does not exist.

Example

The following snippet attempts to figure out what you're targeting based on its GUID:

local guid, name = UnitGUID("target"), UnitName("target")
local type, zero, server_id, instance_id, zone_uid, npc_id, spawn_uid = strsplit("-",guid);
print(name .. " is a " .. type);
if type == "Creature" then
 print(name .. "'s NPC id is " .. npc_id)
elseif type == "Vignette" then
 print(name .. " is a Vignette and should have its npc_id be zero (" .. npc_id .. ").")
elseif type == "Player" then
 print(name .. " is a player.")
end

Notes

This function will not return any values before the PLAYER_LOGIN event on a fresh login. If you need to hold the player's GUID in a variable, you must delay populating that variable until the PLAYER_LOGIN event fires. If you populate your variable by calling UnitGUID in the main chunk, your addon will not work on a fresh login, only after reloading the UI.

See also

Patch changes

  • Warlords of Draenor Patch 6.0.2 (2014-10-14): New format:
    • For players: Player-[server ID]-[player UID] (Example: "Player-976-0002FD64")
    • For creatures, pets, objects, and vehicles: [Unit type]-0-[server ID]-[instance ID]-[zone UID]-[ID]-[Spawn UID] (Example: "Creature-0-976-0-11-31146-000136DF91")
    • Unit Type Names: "Creature", "Pet", "GameObject", and "Vehicle"
    • For vignettes: Vignette-0-[server ID]-[instance ID]-[zone UID]-0-[spawn UID] (Example: "Vignette-0-970-1116-7-0-0017CAE465" for rare mob Sulfurious)
  • Mists of Pandaria Patch 5.1.0 (2012-11-27): Bits shifted. NPCID is now characters 6-9, not 7-10 (counting from 1).
  • Cataclysm Patch 4.0.1 (2010-10-12): Bits shifted. NPCID is now characters 5-8, not 7-10 (counting from 1).
  • Wrath of the Lich King Patch 3.3.0 (2009-12-08): Bits shifted. NPCID is now characters 7-10, not 6-9 (counting from 1).
  • The Burning Crusade Patch 2.4.0 (2008-03-25): Added.