Skip to content

lua npc scripts

mtanksl edited this page Sep 4, 2023 · 10 revisions

Introduction

You can use Lua scripts to extend any funcionality you want.

Example

Let's attach a new Behaviour to all the npcs.

public class NpcScript : GameObjectScript<string, Npc>
{
    public override string Key
    {
        get
        {
            return "";
        }
    }

    private Dictionary<Npc, DialoguePlugin> plugins = new Dictionary<Npc, DialoguePlugin>(); // Note: Use Context.Server.Plugins.GetDialoguePlugin() to manage object lifecycle

    public override void Start(Npc npc)
    {
        DialoguePlugin plugin = new LuaScriptingDialoguePlugin("data/plugins/npcs/default.lua"); 

        plugin.Start();

        plugins.Add(npc, plugin);

        Context.Server.GameObjectComponents.AddComponent(npc, new NpcThinkBehaviour(plugin, new RandomWalkStrategy(2) ) );
    }

    public override void Stop(Npc npc)
    {
        plugins[npc].Stop()

        plugins.Remove(npc);
    }
}

The LuaScriptingDialoguePlugin than loads \data\plugins\lib.lua, \data\plugins\npcs\lib.lua and \data\plugins\npcs\default.lua files.

local say = topic:new()
say:add("name", "My name is {npcname}.")

local handler = npchandler:new( {
    greet = "Hello {playername}.",
    busy = "I'll talk to you soon {playername}.",
    say = say,
    farewell = "Bye {playername}.",
    dismiss = "Bye."
} )

function shouldgreet(npc, player, message) return handler:shouldgreet(npc, player, message) end
function shouldfarewell(npc, player, message) return handler:shouldfarewell(npc, player, message) end
function ongreet(npc, player) handler:ongreet(npc, player) end
function onbusy(npc, player) handler:onbusy(npc, player) end
function onsay(npc, player, message) handler:onsay(npc, player, message) end
function onfarewell(npc, player) handler:onfarewell(npc, player) end
function ondismiss(npc, player) handler:ondismiss(npc, player) end
Clone this wiki locally