• 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!

¿How to make a conversation with an npc?

Leyenda

New Member
Joined
Jul 13, 2012
Messages
28
Reaction score
1
Hello, i want to know how to make a conversation on an npc, for example:
Player says hi
Npc says Hey, what are you doing in my castle?
player says castle
Npc says Yes, this is my castle
And so on..

Here's an npc code, what do i need to add?
PHP:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Carlos" walkinterval="2000" floorchange="0">
	<health now="100" max="100"/>
	<look type="134" head="78" body="88" legs="0" feet="88" addons="3"/>
	<parameters>
		<parameter key="message_greet" value="Hey, what are you doing in my castle?"/>
	</parameters>
</npc>

Please note that im not asking that you finish the code, i want to know what do i need to use!
 
I'd suggest you to create a conversation with "correct" keywords in the .lua file in npcs/scripts. To link an NPC to a .lua file, please add this to the second line:
XML:
<npc name="Carlos" script="carlos.lua" walkinterval="2000" floorchange="0">
 
Like this?
XML:
keywordHandler:addKeyword({'Hi'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = '¿What are you doing on my castle?'})
keywordHandler:addKeyword({'castle'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Yes, this is my castle'})
 
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid)			npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg)			npcHandler:onCreatureSay(cid, type, msg) end
function onThink()					npcHandler:onThink() end
 
function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
 
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
 
	if(msgcontains(msg, 'castle')) then
		selfSay('Yes, this is my castle.', cid)
	end
	return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

If you want to add more it's important to understand how to use talkstates and storage. Storage is for the NPC to remember what a player said when he comes back and talkstate is for in the conversation only.
Storage and talkstate example:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid)			npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg)			npcHandler:onCreatureSay(cid, type, msg) end
function onThink()					npcHandler:onThink() end
 
function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
 
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
	local storage = 9456
 
	if(msgcontains(msg, 'castle')) then
		if(getPlayerStorageValue(cid, storage) == -1) then
			selfSay('Yes, this is my castle, do you want to know more?', cid)
			talkState[talkUser] = 1
		elseif(getPlayerStorageValue(cid, storage) == 1) then
			selfSay('Ok, I see you came back, so I will tell you more now.', cid)
		end
	elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
		selfSay('Well it is a nice castle, I will tell you more when you come back.', cid)
		setPlayerStorageValue(cid, storage, 1)
	end
	return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Again, thank you very much limos! I have 2 questions, hope that you can awnser them:
1-Can i use ANY storage? how do i know if one is alredy i use? or it dosent matter?
2-Npc's talk to you when you say hi or hello, how can i change this words? (My server is 100% spanish :-] ) So like if you say hi, they dont talk, if you say "Hola" (Hello in spanish) they start to talk to you.
 
1. Yes, it doesn't matter which storage you use, and you can't use it if it's already used for other scripts. If you are not sure if it's already used you can look in the scripts you have which storages you used (unique ids you used in chests with action id 2000 for the system.lua count as storage too).

2. npcsystem.lua
Lua:
	FOCUS_GREETWORDS = {'hi', 'hello', 'hey'}
 
Last edited:
Thankyou, again!.. I am trying to add more dialogue lines, but i am doing something wrong.. for what is this used for?
PHP:
 talkState[talkUser] = 1
 
Talkstate is silimar to storage, but only in the conversation with the NPC. when that stops, it's back to the beginning talkstate. In the example I made you see this
Lua:
                        selfSay('Yes, this is my castle, do you want to know more?', cid)
			talkState[talkUser] = 1
Here it sets the talkstate to 1, then when a person says yes and you check for talkstate 1 it will respond
Lua:
	elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
		selfSay('Well it is a nice castle, I will tell you more when you come back.', cid)

Like this if a person starts with yes, the npc would not respond, because then the talkstate is not 1 and it will only respond with talkstate 1. This is also usefull if you want to use yes more times.
 
Ok, but i cant make more lines of conversation:/! Look, on the npc xml file i wrote this so when the player talks to him it dosent says "Welcome, i have been expecting you!"

PHP:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Carlos" script="carloscript.lua" walkinterval="2000" floorchange="0">
	<health now="100" max="100"/>
	<look type="134" head="78" body="88" legs="0" feet="88" addons="3"/>
	<parameters> 
        <parameter key="message_greet" value="Hey |PLAYERNAME| ¿What are you doing in my castle?"/> 
    </parameters>
</npc>

And now on the script, i just pasted yours but cant figure out how to make more lines of conversation, do i have to add more storages?

PHP:
 local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid)			npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg)			npcHandler:onCreatureSay(cid, type, msg) end
function onThink()					npcHandler:onThink() end
 
function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
 
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
	local storage = 60000
 
	if(msgcontains(msg, 'castle')) then
		if(getPlayerStorageValue(cid, storage) == -1) then
			selfSay('Yes, this is my castle. ¿Do you like it?', cid)
			talkState[talkUser] = 1
                end
		if(msgcontains(msg, 'Yes') and talkState[talkUser] == 1) then
			selfSay('Thanks, ¿do you live in here? ')
				talkState[talkUser] = 2
                end
		if(msgcontains(msg, 'yes') and talkState[talkUser] == 2) then
			selfSay('¿WHAT?', cid)
			talkState[talkUser] = 3
		end
	return true
	end


npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Thats just an example!
 
Every if has to end with end when it's not a part of that if anymore. Also the function creatureSayCallback(cid, type, msg) should have an end at the end. So now you have 4 ifs and 3 ends.
You only have to use storage if you want the npc to remember what a person did or said when he comes back to the npc.
 
¿Like this?:
PHP:
if(msgcontains(msg, 'castle')) then
        if(getPlayerStorageValue(cid, storage) == -1) then
            selfSay('Yes, this is my castle. ¿Do you like it?', cid)
            talkState[talkUser] = 1
        if(msgcontains(msg, 'Yes') and talkState[talkUser] == 1) then
            selfSay('Thanks, ¿do you live in here? ')
                talkState[talkUser] = 2
        if(msgcontains(msg, 'yes') and talkState[talkUser] == 2) then
            selfSay('¿WHAT?', cid)
            talkState[talkUser] = 3
        end
        end
        end
    return true
    end


npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new()) 
end
 
Lua:
 local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
function onCreatureAppear(cid)                npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid)            npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg)            npcHandler:onCreatureSay(cid, type, msg) end
function onThink()                    npcHandler:onThink() end
 
function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
 
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
 
	if(msgcontains(msg, 'castle')) then
            	selfSay('Yes, this is my castle. ¿Do you like it?', cid)
            	talkState[talkUser] = 1
  	elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
            	selfSay('Thanks, ¿do you live in here?', cid)
                talkState[talkUser] = 2
        elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 2) then
            	selfSay('¿WHAT?', cid)
        end
    	return true
end


npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
I tried that before, but i get this error
[16/1/2013 17:47:45] [Error - LuaInterface::loadFile] data/npc/scripts/carlosscript.lua:37: 'end' expected (to close 'function' at line 11) near '<eof>'
[16/1/2013 17:47:45] [Warning - NpcEvents::NpcEvents] Cannot load script: data/npc/scripts/carlosscirpt.lua
[16/1/2013 17:47:45] data/npc/scripts/carlosscript.lua:37: 'end' expected (to close 'function' at line 11) near '<eof>'
The code is the same as the one you just posted, i added an end and now the npc dosent responds when talking to it!
 
Thank you very much limos, fixed it, the problem was with the end on the last line, and now it works :). Now, is there a way to edit what the npc says when the player says bye?
 
Back
Top