API UnitGUID

Returns the GUID of the specified unit; works on both players and NPC. guid = UnitGUID("unit")

Arguments

 * unit : String - Unit ID of the unit to look up.

Returns

 * guid : String - a string containing the hexadecimal representation of the unit's GUID, or nil if the unit does not exist.

Example
local name = UnitName("target"); local guid = UnitGUID("target"); ChatFrame1:AddMessage(name.." has the GUID: "..guid);

Result Cyan has the GUID: 0x00000000012729FD

Conversion to decimal?

GUIDs are 64-bit numbers, and are far too long to convert to decimal numbers using lua's libraries as a whole. E.g. running tonumber(guid, 16) will produce erronous results.

Example: Determining unit type
local guid = UnitGUID("target"); local B = tonumber(guid:sub(5,5), 16); local maskedB = B % 8; -- x % 8 has the same effect as x & 0x7 on numbers <= 0xf local knownTypes = {[0]="player", [3]="NPC", [4]="pet", [5]="vehicle"}; print("Your target is a " .. (knownTypes[maskedB] or " unknown entity!"));

Example: Decomposing a GUID
We have a GUID: "0xF530004D2B008852".

First of all, let's find out what kind of unit it is. Take the first three digits in the GUID, ie "F53" (or 0xF53 to show it's an hex value) and apply an AND mask of 0x007 to get 0x003. This GUID is for a normal NPC, neither a pet nor a player.

We can also extract the Unit ID by taking the seventh to tenth digit and converting it to decimal form: "4D2B", or converted to decimal form "19755". A quick visit to wowhead shows that the NPC with that id is "Mo'arg Weaponsmith",. All "Mo'arg Weaponsmiths" will have that id to identify them.

The last six digits, "008852" is the spawn counter. There will never be two "Mo'arg Weaponsmith", possible even never two mobs in the outside world, with the same spawn number. This spawn counter, combined with the rest makes it possible to always refer to exactly this "Mo'arg Weaponsmith", and not the one next to it.

A simple in game script command can be used to get the NPC ID from your target: Target NPC ID: 19755 /run print("Target NPC ID:", tonumber((UnitGUID("target")):sub(-12, -9), 16)) More informative script: NPC ID("Mo'arg Weaponsmith") = 0x4D2B = 19755 /run local a=strsub(UnitGUID("target"),7,10); print("NPC ID(\""..UnitName("target").."\") = 0x"..a.." = "..tonumber(a,16))

Cross-server and GUID "uniqueness"
Player GUIDs are local on a per-server basis, making the "global" scope to be bound to the specific server. Every time a character is created, a new GUID is assigned from a simple +1 counter and then given to that character. It should be noted that the act of transferring characters, either server to server, account to account, or even account to account while remaining on the same server will generate a new character GUID, because of how the process works (the character "ceases to exist" for a short period, and is then recreated). This act erases friend and ignore lists. Renaming a character does not trigger a new GUID, as that process is much simpler than a full character move.

Uniqueness is guaranteed in cross-realm battlegrounds by masking the player GUID with a server specific unique identifier, when needed.

NPC GUID collisions have also been observed. It is unknown why or when in specific they occur, but differing mob types have had a NPC ID number which corresponded to an entirely different NPC. This is considered a very rare phenomenon.

4.0 changes to NPC ID
Along with the Live 4.0.1 patch (build 13164), the NPC portion has moved 2 digits to the left, to CC DD instead of DDDD. 0xF1300073AB 06AE87,"King Varian Wrynn" 0xF13073AB 00001BB4,"King Varian Wrynn"
 * Before: (9th to 12th digit)
 * After: (7th to 10th digit)

The NPC portion of this is 0x73AB in hex, and a NPC ID of 29611 in decimal.