Ever wanted to walk around your map and place npc's around the gameworld or forget to set an npc or 2 but don't feel like shutting down the server opening up the map editor and setting the npc's? Or what if you wanted to create last minute npc's? Or even dynamic npcs?
Well with this code you can set npc's on the map and they will respawn where ever you placed then in the event you ever have to restart the server.
This can be done with a modal window but we are just going to use a basic talkaction.
Save this as npc.lua in data/talkactions/scripts/
Then add this to talkactions.xml
The command for this would be
Save this as npcList.lua inside data/actions/scripts/tools
Next save this as npc.lua in data/actions/scripts/tools
Add this to actions.xml
Now open up startup.lua in data\globalevents\scripts and place this at the bottom before the closing end keyword.
Well with this code you can set npc's on the map and they will respawn where ever you placed then in the event you ever have to restart the server.
This can be done with a modal window but we are just going to use a basic talkaction.
Save this as npc.lua in data/talkactions/scripts/
Code:
function onSay(player, words, param)
if not player:getGroup():getAccess() then
return true
end
if player:getAccountType() < ACCOUNT_TYPE_GOD then
return false
end
_G["NPCN"] = param
broadcastMessage("Npc Selected " .. param ..".", MESSAGE_STATUS_WARNING)
return true
end
Then add this to talkactions.xml
HTML:
<talkaction words="/npc" separator=" " script="npc.lua" />
The command for this would be
Code:
/npc npc_name
Save this as npcList.lua inside data/actions/scripts/tools
Lua:
npcList = {
}
Next save this as npc.lua in data/actions/scripts/tools
Lua:
local fileEx = 'data/actions/scripts/tools/npclist.lua'
dofile(fileEx)
function setNpcs(name, pos)
-- open the file for reading only
local file = io.open(fileEx, 'r')
-- get the contents of the file and store them in str
local str = file:read("*a")
-- now close the file
file:close()
-- remove the last character ( } )
str = str:sub(1, #str - 1)
-- open the file but this time make it writeable
file = io.open(fileEx, "w+")
-- write the initial value to the file, which over writes any previous data
file:write(str)
-- write the new npc to the file
file:write("\t[\""..name.."\"] = ".."{ x = "..pos.x..", y = "..pos.y..", z = "..pos.z.." },")
-- write a bracket to close the table
file:write('\n}')
-- close the file
file:close()
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local name = _G["NPCN"]
local p = target:getPosition()
local npc = Game.createNpc(name, p)
if npc ~= nil then
npc:setMasterPos(p)
setNpcs(name, p)
p:sendMagicEffect(CONST_ME_MAGIC_RED)
else
player:sendCancelMessage("Cannot Create NPC "..name..".")
p:sendMagicEffect(CONST_ME_POFF)
end
return true
end
Add this to actions.xml
HTML:
<!-- npc wand -->
<action itemid="7735" script="tools/npc.lua" allowfaruse="1" />
Now open up startup.lua in data\globalevents\scripts and place this at the bottom before the closing end keyword.
Lua:
-- load npc's on the map at startup from a lua table
local cfile = 'data/actions/scripts/tools/npclist.lua'
local c_npc = 0
dofile(cfile)
if next(npcList) then
for name, pos in pairs(npcList) do
local npc = Game.createNpc(name, pos)
if npc ~= nil then
npc:setMasterPos(pos)
c_npc = c_npc + 1
end
end
end
broadcastMessage("Npc's loaded: " .. c_npc ..".", MESSAGE_STATUS_WARNING)