• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

NPC [How-to] Make NPCs tell stories

Summ

(\/)(;,,;)(\/) Y not?
Staff member
Global Moderator
Joined
Oct 15, 2008
Messages
4,152
Solutions
12
Reaction score
1,107
Location
Germany :O
I saw that some people have problems with this, so I release my version.
I created it some time ago based on a already released script, if the person sees this you can ask me for credits. :)

In npc/lib/npc.lua add:
Lua:
function npcDelayedTalk(cid, text, player)
	if isNpc(cid) and isPlayer(player) then
		doCreatureSay(cid, text, TALKTYPE_PRIVATE_NP, false, player, getThingPos(cid))
	end
end
 
function selfStory(msg, player, delay)
	local interval, npc = delay or 5000, getNpcCid()
	local ret = {}
	
	for i, message in pairs(msg) do
		if i == 1 then
			doCreatureSay(npc, message, TALKTYPE_PRIVATE_NP, false, player, getThingPos(npc))
		else
			local r = addEvent(npcDelayedTalk, (i-1)*interval, npc, message, player)
			table.insert(ret, r)
		end
	end
	return ret
end

In any npc script file you want to use this add this at the top:
Lua:
local story = {}

local function cancelStory(cid)
	if not(story[cid]) then return true end
	for _, eventId in pairs(story[cid]) do
		stopEvent(eventId)
	end
	story[cid] = {}
end

function creatureFarewell(cid)
	if not(isPlayer(cid)) then return true end
	cancelStory(cid)
	return true
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)

That's the basic setup for every NPC.

Everywhere you want the NPC to tell a story now add it like this:
Lua:
		if msgcontains(msg, "story") then
			cancelStory(cid)
			story[cid] = selfStory({"First message", "Second message", "Third etc.."}, cid, 6000)
			return true
		end

You should also call cancelStory(cid) whenever you use selfStory or selfSay to cancel the previous story.

~Summ

V Dat linkens
 
Summ You should make something like different delay between each messages look here:
Code:
story[cid] = selfStory(["first message"] = {interval = 1000}, ["second message"] = {interval = 3000}, ["third message"] = {interval = 5000}}, cid)

Possible to do?
 
Summ You should make something like different delay between each messages look here:
Code:
story[cid] = selfStory(["first message"] = {interval = 1000}, ["second message"] = {interval = 3000}, ["third message"] = {interval = 5000}}, cid)

Possible to do?
Not with this version.
 
Not that hard to update, I will see when I am in the mood to add it.
 
I trying fix different msgs delay but im not so experienced in table.
I've got something like this but i dont know fix it's good way to fix

in lib/npc.lua
Code:
function selfStory(msg, player, delay)
	local interval, npc = delay or 2000, getNpcCid()
	local ret = {}
 
	for i, message in pairs(msg) do
		if message == 1 then
			doCreatureSay(npc, message, 1)
		else
			local r = addEvent(npcDelayedTalk, interval, npc, message, player)
			table.insert(ret, r)
		end
	end
	return ret
end

in npc script
Code:
local story = {}
local msg = {["1"] = {interval = 2000},	["2"] = {interval = 4000}}
        if msgcontains(msg, "story") then
	   cancelStory(cid)
     story[cid] = selfStory(msg, cid, msg.interval)
	   return 1
	end
end

error
Code:
[11/05/2012  15:02:40] data/npc/lib/npc.lua:125: bad argument #1 to 'pairs' (table expected, got string)
[11/05/2012  15:02:40] stack traceback:
[11/05/2012  15:02:40] 	[C]: in function 'pairs'
[11/05/2012  15:02:40] 	data/npc/lib/npc.lua:125: in function 'selfStory'
[11/05/2012  15:02:40] 	data/npc/scripts/default.lua:37: in function 'callback'
[11/05/2012  15:02:40] 	data/npc/lib/npcsystem/npchandler.lua:299: in function 'onCreatureSay'
[11/05/2012  15:02:40] 	data/npc/scripts/default.lua:7: in function <data/npc/scripts/default.lua:7>

please help
 
Please summ, I don't understand where to edit the npcs lua file

Please write an simple script for me (how the lua file should exactly look like to work)

I made the npc, the xml file, but the lua file (dariusthemonk.lua)

How should it look like to tell stories?
 
Please summ, I don't understand where to edit the npcs lua file

Please write an simple script for me (how the lua file should exactly look like to work)

I made the npc, the xml file, but the lua file (dariusthemonk.lua)

How should it look like to tell stories?

Lua:
 if msgcontains(msg, "story") then
			cancelStory(cid)
			story[cid] = selfStory({"First message", "Second message", "Third etc.."}, cid, 6000)
			return true
		end
By editing these messages by the stories, [So if a player said to the npc [story], he says the first message, then story again, it completes.
Thanks!
 
Yes thanks but the whole Lua file, how should it look like? from scratch?
 
And in the beginning?
This story is all i want this npc to do
So how should the beginning look like?
 
I tested it and this function doesn't work:
Code:
function creatureFarewell(cid)
	if not(isPlayer(cid)) then return true end
	cancelStory(cid)
	return true
end
Because If you said bye, NPC doesn't see "cid" because simply ungreeting is faster than "cancelStory(cid)"

Any ideas how to fix?
 
This is an alternative solution for the problem when the player walk away of the NPC...... it works fine for me (using 0.3.6)

Modify the function "npcDelayedTalk" of npc.lua (This is the edited one):
Code:
function npcDelayedTalk(cid, text, player)
	if isNpc(cid) and isPlayer(player) and getDistanceBetween(getCreaturePosition(player), getCreaturePosition(cid)) <= 4 then
		doCreatureSay(cid, text, TALKTYPE_PRIVATE_NP, false, player, getThingPos(cid))
	end
end



and Add this code in all the NPC who use this System:
Code:
function onThink() npcHandler:onThink()
Ken=getNpcId()
Spec=getSpectators(getCreaturePosition(Ken), 4, 4)
if Spec ~= nil then
for i, tid in ipairs(Spec) do
if isPlayer(tid) then
if not(npcHandler:isFocused(tid)) then cancelStory(tid)
end
end
end 
end
end

;) takes me an eternity to solve it xD
 
Last edited:
This is an alternative solution for the problem when the player walk away of the NPC...... it works fine for me (using 0.3.6)

Modify the function "npcDelayedTalk" of npc.lua (This is the edited one):
Code:
function npcDelayedTalk(cid, text, player)
	if isNpc(cid) and isPlayer(player) and getDistanceBetween(getCreaturePosition(player), getCreaturePosition(cid)) <= 4 then
		doCreatureSay(cid, text, TALKTYPE_PRIVATE_NP, false, player, getThingPos(cid))
	end
end



and Add this code in all the NPC who use this System:
Code:
function onThink() npcHandler:onThink()
Ken=getNpcId()
Spec=getSpectators(getCreaturePosition(Ken), 4, 4)
if Spec ~= nil then
for i, tid in ipairs(Spec) do
if isPlayer(tid) then
if not(npcHandler:isFocused(tid)) then cancelStory(tid)
end
end
end 
end
end

;) takes me an eternity to solve it xD
EDIT
Thanks for your code but that is better:

Code:
function CreatureGreetCallback(cid)
 npcId = {cid = cid}
 talkState[talkUser] = 0
 return 1
end

 
function CreatureFareWell(cid)
   cancelStory(npcId.cid)
   return 1
end

npcHandler:setCallback(CALLBACK_GREET, CreatureGreetCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, CreatureFareWell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, CreatureFareWell)
 
Last edited:
Back
Top