Skip to content

GetUnitTarget

The GetUnitTarget function retrieves the current target of a specified unit in the game world. This function is useful for determining which object or unit a particular unit is targeting, which can be important for gameplay mechanics such as targeting assistance, threat management, and AI behavior.

Parameters

  • unit: The object or unit identifier (GUID) or unit identifier (such as "player", "target", "focus", etc.) for which to retrieve the current target.

Returns

  • target: The object identifier (GUID) or unit identifier of the current target of the specified unit.

Example Usage in Lua

Here is an example of how to use the GetUnitTarget function in Lua:

local api = ...

-- Function to print the target of the player
function PrintPlayerTarget()
    local player = "player"
    local target = api.GetUnitTarget(player)
    if target then
        local targetName = api.GetObjectName(target)
        print("Player's target: " .. targetName)
    else
        print("Player has no target.")
    end
end

-- Function to print the target of the target
function PrintTargetTarget()
    local target = "target"
    local targetTarget = api.GetUnitTarget(target)
    if targetTarget then
        local targetTargetName = api.GetObjectName(targetTarget)
        print("Target's target: " .. targetTargetName)
    else
        print("Target has no target.")
    end
end

-- Function to print the target of a specific unit by GUID
function PrintUnitTargetByGUID(unitGUID)
    local target = api.GetUnitTarget(unitGUID)
    if target then
        local targetName = api.GetObjectName(target)
        print("Unit with GUID " .. unitGUID .. "'s target: " .. targetName)
    else
        print("Unit with GUID " .. unitGUID .. " has no target.")
    end
end

-- Call the functions
PrintPlayerTarget()
PrintTargetTarget()

-- Example unit GUID (replace with an actual GUID in your game)
local exampleUnitGUID = "1234-5678-9012-3456"
PrintUnitTargetByGUID(exampleUnitGUID)