> For the complete documentation index, see [llms.txt](https://fehdead.gitbook.io/npc-kit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fehdead.gitbook.io/npc-kit/examples/magic-sword.md).

# A Magic Sword

**A sword stands stuck in the ground, and a curious Seeq nearby says it has magic powers. Inspect the sword and it bursts into flames; inspect it again and the fire goes out.**

This example takes place on map id 289, Rabanastre / North End.

Download: [magic-sword.lua](https://github.com/FelipeJulio/NPC-Kit/tree/main/examples/magic-sword.lua)

```lua
local ok, npc = pcall(require, "scripts.NPCKit.Helper")
if not ok then
    print("Couldn't install, NPC Kit is missing.")
    return
end

local rumour = [[
Did you know that sword has magic powers?
Just walk up to it and take a closer look.
]]
local ignite = [[
The blade answers your touch. Flames rise
from the steel and wrap around the hilt.
]]
local douse = [[
You touch the hilt again. The flames sink
back into the blade and go quiet.
]]

local burning = false

local function placeObject(model, x, y, z, heading)
    npc.setautorelax(1);
    npc.usecharhit(0);
    npc.bindp2_d4(model, 1, 0);
    npc.stdmotionread(16);
    npc.stdmotionreadsync();
    npc.stdmotionplay_2c2(0x01000000, 0);
    npc.setoverheadicontype(0);
    npc.usemapid(0);
    npc.usecharhit(1);
    npc.setweight(-1);
    npc.set_ignore_hitgroup(1);
    npc.motionloop(1);
    npc.setpos2(x, y, z);
    npc.dir(heading);
    npc.fetchambient();
    npc.bonecalcforce(-1);
    npc.clearhidecomplete();
    npc.show();
    npc.showshadow();
end

local function placeNpc(model, x, y, z, heading)
    npc.setautorelax(1);
    npc.usecharhit(0);
    npc.bindp2_d4(model, 1, 0);
    npc.stdmotionread(16);
    npc.stdmotionreadsync();
    npc.stdmotionplay_2c2(0x01000000, 0);
    npc.setoverheadicontype(0);
    npc.usemapid(1);
    npc.usecharhit(1);
    npc.setweight(-1);
    npc.set_ignore_hitgroup(1);
    npc.motionloop(1);
    npc.setpos2(x, y, z);
    npc.dir(heading);
    npc.fetchambient();
    npc.bonecalcforce(-1);
    npc.clearhidecomplete();
    npc.show();
    npc.showshadow();
end

local function talkOpen()
    npc.sethpmenu(0);
    npc.ucoff();
    npc.settrapshowstatus(0);
    npc.setaturnlookatlockstatus(0);
    npc.raturna(-1);
    npc.turnsync();
    npc.stdmotionplay_2c2(0x01000002, 20);
    npc.setkutipakustatus(1);
    npc.setunazukistatus(1);
end

local function talkClose()
    npc.setkutipakustatus(0);
    npc.setunazukistatus(0);
    npc.lookatoff();
    npc.ucon();
    npc.sethpmenu(1);
    npc.clear_force_char_nearfade();
    npc.setmaphighmodeldepth(-1);
    npc.setmapmodelstatus(1);
    npc.setstatuserrordispdenystatus(0);
    npc.settrapshowstatus(1);
    npc.stdmotionplay_2c2(0x01000000, 20);
end

local function talkTerm()
    npc.setaturnlookatlockstatus(1);
    npc.aturn_261(npc.getdestrotangzx_227(0));
end

local function myMod()
    npc.add {
        id = "npckit_0067",
        map = 289,
        name = "Magic Sword",

        init = function()
            placeObject(0x00770011, 21.384, 1.408, 44.297, 2.323);
            npc.setscale(1.323);
            npc.setscale_164(1.0, 1.453, 1.0);
            npc.rotate(2.745, -0.195, -0.137);
            npc.setnavimapdrawstatus(0);
            npc.reqenable(13);
            npc.fieldsignicon(3);
            npc.showfieldsign();
            npc.lua(function() burning = false end);
        end,

        main = function()
            npc.wait(90);
        end,

        talk = function()
            npc.ucoff();
            npc.lua(function(handle)
                burning = not burning
                local function done()
                    handle.run(burning and "light" or "putOut", function() handle.finish() end)
                end
                if not dialog.amese.open {
                        speaker = "Magic Sword",
                        icon = 3,
                        body = burning and ignite or douse,
                        queue = true,
                        onClose = done,
                        onError = done
                    } then
                    done()
                end
            end);
            npc.ucon();
        end,

        light = function()
            npc.efx(34);
        end,

        putOut = function()
            npc.efx(0);
        end
    }

    npc.add {
        id = "npckit_0068",
        map = 289,
        name = "Curious Seeq",

        init = function()
            placeNpc(0x006E0019, 18.645, 0.0, 45.957, 2.017);
        end,

        main = function()
            npc.wait(90);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if not dialog.amese.open {
                        speaker = "Curious Seeq",
                        body = rumour,
                        queue = true,
                        onClose = function() handle.finish() end,
                        onError = function() handle.finish() end
                    } then
                    handle.finish()
                end
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }
end

event.registerEventAsync("onInitDone", myMod)
```

The sword is an [inspectable object](https://fehdead.gitbook.io/npc-kit/examples/inspectable-object.md) whose model is turned upside down. `npc.rotate(x, y, z)` turns a model on all three axes, in radians, x first; y is the heading `npc.dir` sets. Here x close to π flips the blade down and the other two tilt it a little, so it reads as driven into the ground. `usemapid(0)` keeps it off the walkmesh, so the Y of `setpos2` is where the hilt ends. z goes back to 0 if the model turns again (a `dir`, a turn, a walk): keep `rotate` for things that stay put.

`npc.efx(h)` gives the model a character effect, `chara\h<h>.efx` (h0034 here), in place of its own; the script waits while the effect's model is read. `npc.efx(0)` takes the effect off. `npc.efxrgba(r, g, b, a)` would tint it, 128 being the colour as drawn.

The talk flips `burning`, says what happens, and when the message closes runs `light` or `putOut` with `handle.run`. Those two are named functions of the NPC: they run in the game script, after the window, and the talk ends when they do. `init` sets `burning` back to `false`, because a new map load builds the sword cold.
