Been meddling with npc conversation scripts. What I've found so far:
You can make a function run every time a player replies to an npc by doing m.chat_func = <
name>, where
name is a string of the target function's name (you would do this on the npc spawn script).
This target function receives the following parameters: f(
npc,
first_word,
full_msg), where
npc is a pointer to the speaking npc,
first_word is the first word of the player's spoken phrase to the npc in lowercase, and
full_msg is the complete phrase given to npc, case sensitive.
The return value for this function is a string that determines the npc's answer to the player.
This is an example of how I've been using chat_func:
Code: Select all
function npc_george_chat(npc, first_word)
local word = string.sub(first_word, 1, 4)
if word == "balr" then --Branch: balron ; Branches into: pits
return "Very powerful demonspawn they are! Be careful if you ever find one in your journey. Have you ever been to the pits below Minotaur Halls?"
elseif word == "pits" then --Branch: pits
return "A treacherous place, deep down in these pits you can find yourself surrounded by these balrons!"
end
return "I cannot help you with that."
end
In this function, an npc will respond to the words
"balron" and
"pits". As you can see, I simulated EUO's conversation system where only the first 4 letters of the spoken word matter by using string.sub. Those "branch" comments aren't really necessary but they help me in case the conversations get too convoluted.
Another thing I've found is that you can highlight those keywords by doing this (running these once at map load is enough):
Code: Select all
convos:add_line("George", Item(0), "balron", "") --balron
convos:add_line("George", Item(0), "pits", "") --pits
This is the add_line function from the api, for reference:
Code: Select all
class Conversations
{
public:
void add_line(const string & name, const Item & item, const string & keyword, const string & reply);
};
I might be reinventing the square wheel here but it has worked for me so far!
The one problem I have is I don't know how to get a pointer to the player speaking to the npc, thus I can't use player name in npc speech or do stuff involving the player (e.g. give/take/check items, heal, etc).