Violet

NPC

On this page

The NPC object represents an NPC entity in the game world.

Functions

npc:is_valid() -> boolean

Available on GMS and CW

Validates the object exists in the game world. Always call this before using other methods.


npc:get_id() -> number | nil

Available on GMS and CW

Returns the NPC template ID, or nil if the NPC is invalid or disabled.


npc:get_position() -> table | nil

Available on GMS and CW

Returns position as a table with x and y fields, or nil if the NPC is invalid or disabled.

lua
local pos = npc:get_position()
-- pos.x, pos.y

npc:get_name() -> string | nil

Available on GMS and CW

Returns the NPC name, or nil if the NPC is invalid or disabled.


npc:get_appearance_id() -> string | nil

Available on GMS and CW

Returns an ID for how the NPC looks. NPCs that look the same share an ID even when their NPC IDs differ, so you can compare the NPCs on a map to find the one that looks different. Returns nil if the NPC is invalid or disabled, copies its look from a character, or its look cannot be read.

The ID describes the look the NPC shows you now. Some NPCs change their look with your quest progress, level or the date, and their ID changes with it. It covers the NPC's usual pose, not the animation it is playing. IDs can change between updates, so compare them with each other instead of saving them.

lua
local looks, counts = {}, {}
for _, npc in ipairs(map:get_npcs()) do
    local look = npc:get_appearance_id()
    if look then
        looks[npc] = look
        counts[look] = (counts[look] or 0) + 1
    end
end
for npc, look in pairs(looks) do
    if counts[look] == 1 then
        core.log("Looks different: " .. npc:get_name())
    end
end

npc:distance() -> number | nil

Available on GMS and CW

Returns the distance from the local player to the NPC, or nil if the NPC or player is unavailable.


npc:talk() -> boolean

Available on GMS and CW

Attempts to talk to the NPC. Returns true if the talk request was sent, or false if the NPC is invalid, the player is unavailable, or the NPC is out of interaction range.