Writing answers instead of choosing menu options [EDITED]
Posted: Mon Apr 22, 2013 9:49 pm
EDITED CODE.
A simple function that recognizes the words written by the player and can be used as a substitute of the menus.
It's inspired in fiorica's "The Doll's Stories" and uses some code from SusanTheCat's "Thera'Py".
Simply add the file 'reply.rpy' to the game directory and use the 'reply()' function to make questions to the player.
The functions needs a question (a string) and a list of possible answers (only simple words), and returns one of the words given.
If the player doesn't respond any of the options, the function presents a warning message, and repeats itself.
The function reply2() returns the list of valid keywords found in the player's answer.
An example:
Here the code of 'reply.rpy':
The function takes five arguments. 'invalid_character' and 'invalid_answer' are used if the warning message is said by a character. In this example, we have Eileen (defined as the character 'e'), saying the warning message:
The code for 'reply.rpy' and an example of 'script.rpy' can be downloaded here. Put the two files in a newly created game:
Additionally, jw2pfd has made another code that does essentially the same thing, with some features added like synonyms recognition. You can found his code and explanation here: http://lemmasoft.renai.us/forums/viewto ... 45#p288203.
Every suggestion will be appreciated.
A simple function that recognizes the words written by the player and can be used as a substitute of the menus.
It's inspired in fiorica's "The Doll's Stories" and uses some code from SusanTheCat's "Thera'Py".
Simply add the file 'reply.rpy' to the game directory and use the 'reply()' function to make questions to the player.
The functions needs a question (a string) and a list of possible answers (only simple words), and returns one of the words given.
If the player doesn't respond any of the options, the function presents a warning message, and repeats itself.
The function reply2() returns the list of valid keywords found in the player's answer.
An example:
Code: Select all
$ r = reply("Where can we go?", "park, beach, home")
if r == 'park':
# do something
elif r == 'beach':
# do something
else:
# do something
$ r2 = reply2("Where can we go?", "park, beach, home")
if "park" in r2:
e "You have chosen park"
if "beach" in r2:
e "You have chosen beach"
if "home" in r2:
e "You have chosen home"
$ l = len(r2)
if l > 2:
e "You have chosen too much."
#Note that with reply2() we don't use "elif" but separated "if" because all of the keywords can be found. With "len()" you have the total of keywords found in the user's reply.
Code: Select all
# Reply and Reply2
# by xavimat (cc-by, 2013)
# inspired by fiorica's "The Doll's Stories" and SusanTheCat's "Thera'Py"
# improved with help of jw2pfd
init python:
def reply(question = "Yes or no?",
answers = "yes,no",
invalid_character = None,
invalid_answer = "(Invalid answer)",
show_answers = True):
thequestion = question
found_it = ""
ans = answers.replace(" ", "")
ans = ans.split(",")
if show_answers:
thequestion += " {size=-10}("
for element in ans:
thequestion += element + ', '
thequestion = thequestion[:-2] + "){/size}"
while found_it == "":
phrase = renpy.input(thequestion)
phrase = phrase.lower()
for chara in ',.;:!?-+*()[]{}&%$':
phrase = phrase.replace(chara, ' ')
phras = phrase.split()
for element in ans:
for theword in phras:
if found_it == '' and element == theword:
found_it = element
if found_it == "":
renpy.say(invalid_character, invalid_answer)
return found_it
def reply2(question = "Yes or no?",
answers = "yes,no",
invalid_character = None,
invalid_answer = "(Invalid answer)",
show_answers = True):
thequestion = question
found_it = [ ]
ans = answers.replace(" ", "")
ans = ans.split(",")
if show_answers:
thequestion += " {size=-10}("
for element in ans:
thequestion += element + ', '
thequestion = thequestion[:-2] + "){/size}"
while found_it == [ ]:
phrase = renpy.input(thequestion)
phrase = phrase.lower()
for chara in ',.;:!?-+*()[]{}&%$':
phrase = phrase.replace(chara, ' ')
phras = phrase.split()
for element in ans:
for theword in phras:
if element == theword and element not in found_it:
found_it.append(element)
if found_it == [ ]:
renpy.say(invalid_character, invalid_answer)
return found_it
Code: Select all
$ r = reply("Where do you want to go?", "park, beach, home", e, "Can you repeat your answer, please?")
Every suggestion will be appreciated.