Keyword-based text parser, anyone?
Posted: Wed Feb 17, 2016 10:42 am
Hey, guys! Me again! I'm not yet at this stage in developing my first love game, but still, it's better to know beforehand. What are your thoughts on implementing a keyword-based text parser as a way to talk to NPCs in a love2d game? It's like this:
You activate an NPC. A special NPC screen pops up. The NPC says hello. But you reply not by clicking on dialog options or stuff like that, but by directly typing in your greeting. After that you just use the sucker like a chat bot, which recognizes certain words and reacts to them. For example you might type in "Let us trade", and a shop interface pops up. Or, if the NPC is not a merchant, he tells you so and directs you to the closest store. But how do you get love to recognize text? I've used this code:
But after this part I'm stuck. It seems that the text you input this way is seen as a collection of utf8 symbols. I think love knows how many there is, for example 5 in a word "hello" typed into the line, but it doesn't know which letters are used. But I need it to go through the typed word or phrase, and compare it to words in a "dictionary" to react. For example: I type in "Hey, man, have you seen any dragons lately?". I press enter. The game finds I used the word "dragon", and gives me a pre-written reply: "They say, dragons live west of here, "..player.name..""!" How do I do it, really? And can I really do it? I learned that tons of people are doing their best at making a love rpg, so I think this topic is pretty relevant, and I'm not the only one to benefit from a nice answer! Thanks guys! Be seeing ya'll!
You activate an NPC. A special NPC screen pops up. The NPC says hello. But you reply not by clicking on dialog options or stuff like that, but by directly typing in your greeting. After that you just use the sucker like a chat bot, which recognizes certain words and reacts to them. For example you might type in "Let us trade", and a shop interface pops up. Or, if the NPC is not a merchant, he tells you so and directs you to the closest store. But how do you get love to recognize text? I've used this code:
Code: Select all
local utf8 = require("utf8")
function love.textinput(t)
if npcchat == "on" then
yourphrase = yourphrase .. t
function love.update(dt)
if kb.isDown('backspace') and npcchat == true then
local byteoffset = utf8.offset(yourphrase, -1)
if byteoffset and npcchat == true then
yourphrase = string.sub(yourphrase, 1, byteoffset - 1)
end
end
function love.keypressed(key)
if kb.isDown("return") and npcchat == true then
npcchat = "off"
InterpretingWhatYouTyped = "on"
end