Reading randomized dialogue from file [SOLVED]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
User avatar
rockets
Newbie
Posts: 21
Joined: Wed Apr 15, 2015 2:16 pm
Projects: jerkcity_ebooks dating sim
Tumblr: dickmissiles
Contact:

Reading randomized dialogue from file [SOLVED]

#1 Post by rockets »

I'm trying to create a character whose dialogue consists of randomly spewing out lines from a huge text file database that I have saved on my hard drive, formatted as such:

Code: Select all

CLIMB MAN
COMMAND-OPTION : REBUILDS THE DESKTOP
I REALLY NEED MEIDCAL ADVICE
I THINK I'VE LOST MY ERECTION
IS SHE BIG?
MY HEAD HAS CAVED IN
NO, I MEAN, CAN SHE KICK MY ASS...?
NORTH
........
WEST
WHAT'S THE DIFFERENCE BETWEEN A NANOMETER AND A KILOMETER
X15
I have this saved as quotes.txt in the game folder. Is there a feasible way for me to load each line of this text into an array that can be randomly accessed through renpy.random.choice()?

EDIT: Here is the code, as graciously provided by nintendotoad and philat.

Code: Select all

init python:
    def getYourData(filename):
        yourData = filename
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData
and then

Code: Select all

    $ linesToSay = getYourData()
    $ justOneLineToSay = renpy.random.choice(linesToSay)
wherever you want it in your script!
Thanks a bunch, everyone!
Last edited by rockets on Wed Apr 15, 2015 9:14 pm, edited 2 times in total.

nintendotoad
Regular
Posts: 42
Joined: Sat Mar 31, 2012 2:56 pm
Location: projectexist.net
Contact:

Re: Reading randomized dialogue from file

#2 Post by nintendotoad »

Code: Select all

    # Oi! This is untested!
    def getYourData():
            yourData = whereverYourFileHappensToBe
            yourData = file(yourData).read().decode("utf-8")
            yourData = yourData.split("\n")
            return yourData
    $ linesToSay = getYourData()
    $ justOneLineToSay = renpy.random.choice(linesToSay)
I strongly suggest changing the function name and variable names so that they make more sense to you, but that is a quick and dirty way of getting a list of things the character can say. At least, I'm fairly certain that it is. Go nuts!

User avatar
rockets
Newbie
Posts: 21
Joined: Wed Apr 15, 2015 2:16 pm
Projects: jerkcity_ebooks dating sim
Tumblr: dickmissiles
Contact:

Re: Reading randomized dialogue from file

#3 Post by rockets »

errors.txt

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 15: expected statement.
    def getYourData():
                   ^

Ren'Py Version: Ren'Py 6.99.1.329
Additionally, would it be possible to write code such as

Code: Select all

def getYourData(filename):
        yourData = filename
        yourData = file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData
so that this could be used for multiple text files? or am I doing python extremely wrong?

EDIT BECAUSE I FORGOT TO PUT python: BEFORE THE CODE
there is now a new error & i don't want to double post

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData(harriet.txt)
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData(harriet.txt)
NameError: name 'getYourData' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData(harriet.txt)
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/ast.py", line 785, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/python.py", line 1432, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData(harriet.txt)
NameError: name 'getYourData' is not defined

Darwin-14.1.0-x86_64-i386-64bit
Ren'Py 6.99.1.329
JERKCITY_DATING_SIM 0.0

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Reading randomized dialogue from file

#4 Post by philat »

Looks like you put the code somewhere after the script and didn't put init before it. Try init python: instead of just python:.

User avatar
rockets
Newbie
Posts: 21
Joined: Wed Apr 15, 2015 2:16 pm
Projects: jerkcity_ebooks dating sim
Tumblr: dickmissiles
Contact:

Re: Reading randomized dialogue from file

#5 Post by rockets »

Code: Select all

While running game code:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData(harriet.txt)
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData(harriet.txt)
NameError: name 'harriet' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData(harriet.txt)
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/ast.py", line 785, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/python.py", line 1432, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData(harriet.txt)
NameError: name 'harriet' is not defined

Darwin-14.1.0-x86_64-i386-64bit
Ren'Py 6.99.1.329
JERKCITY_DATING_SIM 0.0
Perhaps I'm just way too used to Java. philat, thanks for the tip, because i totally forgot to put in the init!!!
EDIT BECAUSE I FORGOT TO PUT QUOTE MARKS!!

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData('harriet.txt')
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData('harriet.txt')
  File "game/script.rpy", line 18, in getYourData
    yourData = file(yourData).read().decode("utf-8")
IOError: [Errno 2] No such file or directory: 'harriet.txt'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 53, in script
    $ linesToSay = getYourData('harriet.txt')
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/ast.py", line 785, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/veronicalam/Downloads/renpy-6.99.1-sdk/renpy/python.py", line 1432, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 53, in <module>
    $ linesToSay = getYourData('harriet.txt')
  File "game/script.rpy", line 18, in getYourData
    yourData = file(yourData).read().decode("utf-8")
IOError: [Errno 2] No such file or directory: 'harriet.txt'

Darwin-14.1.0-x86_64-i386-64bit
Ren'Py 6.99.1.329
JERKCITY_DATING_SIM 0.0
Now i'm getting a filenotfound error, despite the fact that harriet.txt is in the game directory. Is there somewhere else I should be putting it? I feel like I'm getting closer and closer to reaching some kind of terrible breakthrough to making a game where you try to romance a horse_ebooks-like AI.
Image

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Reading randomized dialogue from file

#6 Post by philat »

Actually ran the code and looked at the documentation -- relatively minor fix. http://www.renpy.org/doc/html/file_python.html

Code: Select all

init python:
    def getYourData(filename):
        yourData = filename
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData

User avatar
rockets
Newbie
Posts: 21
Joined: Wed Apr 15, 2015 2:16 pm
Projects: jerkcity_ebooks dating sim
Tumblr: dickmissiles
Contact:

Re: Reading randomized dialogue from file

#7 Post by rockets »

thank you so so so much it works like a dream! <3
Image

nintendotoad
Regular
Posts: 42
Joined: Sat Mar 31, 2012 2:56 pm
Location: projectexist.net
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#8 Post by nintendotoad »

I should probably take a better look at my imports - the code runs in my testbed without "renpy." preceding the .file() call. Glad everything worked out though. :)

User avatar
MGR_59
Newbie
Posts: 11
Joined: Fri Apr 17, 2015 11:22 am
Location: South West UK
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#9 Post by MGR_59 »

I get a syntax error... I tried this code with CSV file named "questions.csv". The CSV in the game directory.
The player should say the contents from one random line in the CSV. So I changed the file name like this:

Code: Select all

init python:
    def getYourData(questions.csv):
        yourData = questions.csv
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData

$ linesToSay = getYourData()
$ justOneLineToSay = renpy.random.choice(linesToSay)
In the script I'm trying to get the player to say the one random line, like this:

Code: Select all

p "[justOneLineToSay]"
But I get the error message:

Parsing the script failed.
File "game/script.rpy", line 18: invalid syntax
def getYourData(questions.->csv):

What am I doing wrong?

User avatar
rockets
Newbie
Posts: 21
Joined: Wed Apr 15, 2015 2:16 pm
Projects: jerkcity_ebooks dating sim
Tumblr: dickmissiles
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#10 Post by rockets »

edit: thanks, philat!

You want to write

Code: Select all

init python:
    def getYourData():
        yourData = "questions.csv"
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData

$ linesToSay = getYourData()
$ justOneLineToSay = renpy.random.choice(linesToSay)
Last edited by rockets on Sat Apr 18, 2015 11:07 am, edited 1 time in total.
Image

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#11 Post by philat »

Actually, in addition to what rockets said, you'd also need to put questions.csv in quotes -- it needs to be a string.

User avatar
MGR_59
Newbie
Posts: 11
Joined: Fri Apr 17, 2015 11:22 am
Location: South West UK
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#12 Post by MGR_59 »

This helped me a lot, many thanks for your input. I found that PyTom recommends using tab limited TXT files instead of CSV's.
Consequently, the string table with my questions is now called "questions.txt" instead. (The answers string table is of course called "answers.txt" in the same way.)

I also added .rstrip() to the random spoken string in the "justOneLineToSay" variable.
The newline character showed up at first as a square at the end. I think it was the same problem as in this thread:
http://lemmasoft.renai.us/forums/viewto ... le#p285496
The .rstrip() removes the newline character.

On this page I also found some more info about how to remove the newline character, perhaps this can be useful to some other newbie as well:
http://stackoverflow.com/questions/2750 ... -in-python


Here's the code that I am using now. Ren'py now fetches a random question from my separate tab delimited questions.txt file and it seems to work fine:

Code: Select all

init python:
    def getYourData():
        yourData = "questions.txt"
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData

$ linesToSay = getYourData()
$ justOneLineToSay = renpy.random.choice(linesToSay).rstrip()

p "[justOneLineToSay]"

Thanks again, I'm very grateful. :D
cheers/Magnus

User avatar
ameliori
Veteran
Posts: 201
Joined: Sun Apr 06, 2014 9:20 am
Completed: Who is Mike?, Cupid
Projects: The Black Beast [Fairytale]
Organization: FERVENT
Tumblr: ameliori
itch: fervent
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#13 Post by ameliori »

Hello guys! Sorry for necro-ing this thread but I really really need this function. It looks so promising so I ventured to try. Unfortunately, it's giving me an error

Code: Select all

While running game code:
  File "game/script.rpy", line 27, in script
    e "[justOneLineToSay]"
KeyError: u'justOneLineToSay' 
Is there another way around this? Thank you so much!
ameliori
TwitterPatreon

Acceptable
Newbie
Posts: 7
Joined: Mon Jun 27, 2016 5:00 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#14 Post by Acceptable »

Apologies for bumping this thread yet again, but I'm having the same problem as ameliori. I've entered the code pretty much exactly as written, but I'm getting the same KeyError: u'justOneLineToSay' error.

Code: Select all

init python:
    def getYourData():
        yourData = "quotes.txt"
        yourData = renpy.file(yourData).read().decode("utf-8")
        yourData = yourData.split("\n")
        return yourData

$ linesToSay = getYourData()
$ justOneLineToSay = renpy.random.choice(linesToSay).rstrip()

label quote_test:
    show words full
    "[justOneLineToSay]"
    jump quote_test
The version of Ren'py I'm using is 6.99. Thanks for any help you can provide.

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#15 Post by philat »

I would guess your $ lines are before label start.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], DewyNebula, Google [Bot]