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
Acceptable
Newbie
Posts: 7
Joined: Mon Jun 27, 2016 5:00 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#16 Post by Acceptable »

Thank you, that was indeed the case. You saved me so much extra work! I feel like a bit of an idiot.

User avatar
Steffenator
Regular
Posts: 53
Joined: Fri Feb 17, 2017 11:58 am
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#17 Post by Steffenator »

I keep getting the same issue, it happens in random places in my dialogue.
It started after the 6.99.12.3 update.

Here's my log.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/lstare01.rpy", line 32, in script
    me "I'm not sure, but...\nIf I'd known my cousin had such an attractive roomate, I would have stopped by months ago."
KeyError: u'playername'

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

Full traceback:
  File "game/lstare01.rpy", line 32, in script
    me "I'm not sure, but...\nIf I'd known my cousin had such an attractive roomate, I would have stopped by months ago."
  File "C:\Users\Jared\Desktop\XFer\renpy-6.99.12.2\renpy\ast.py", line 613, in execute
    renpy.exports.say(who, what, interact=self.interact)
  File "C:\Users\Jared\Desktop\XFer\renpy-6.99.12.2\renpy\exports.py", line 1147, in say
    who(what, interact=interact)
  File "C:\Users\Jared\Desktop\XFer\renpy-6.99.12.2\renpy\character.py", line 855, in __call__
    who = who_pattern.replace("[who]", sub(who))
  File "C:\Users\Jared\Desktop\XFer\renpy-6.99.12.2\renpy\character.py", line 850, in sub
    return renpy.substitutions.substitute(s, scope=scope, force=force, translate=translate)[0]
  File "C:\Users\Jared\Desktop\XFer\renpy-6.99.12.2\renpy\substitutions.py", line 232, in substitute
    s = formatter.vformat(s, (), kwargs)
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 563, in vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 585, in _vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 646, in get_field
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 605, in get_value
KeyError: u'playername'

Windows-7-6.1.7601-SP1
Ren'Py 6.99.12.3.2123
Just_Visiting 1.0
There once was a language called Ren'Py
that made all the wannabes cry... with joy.
It gave them the power,
they spent every hour,
to finish a game, or die.


skip bittman
Newbie
Posts: 5
Joined: Mon Jul 02, 2018 5:38 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#18 Post by skip bittman »

Sorry to resurrect this thread, not sure of the correct etiquette -- anyway, it seemed to work at first but every time I insert "[justOneLineToSay]" for a character's dialogue, it just picks the same exact line from the questions.txt file, with no randomizing going on during any subsequent uses. If I relaunch the project, it'll pick a different random line but again just the same one over and over. Any suggestions on how I can get a different line each time the command is used?

Also -- What are the specifications for the external text file to work properly? Some lines just result in bugs until I edit them out, with no seeming rhyme or reason to it. I'm missing something incredibly obvious, and would super appreciate someone setting me straight!

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#19 Post by Remix »

Using @property in an object is likely your best bet so as to force the interpolation to recall random...

Code: Select all

init python:

    class RandomReply(object):

        def __init__(self, filename=None):

            with renpy.file(filename) as f:

                self.lines = f.readlines()

            renpy.python.rng.shuffle( self.lines )

        @property
        def line(self):
            return self.lines.pop() if self.lines else "No quotes left."

    reply = RandomReply("quotes.txt")

label start:

    e "[reply.line!q]"
Note the !q which will help with lines including {, [, and quotes
Also note I opted to shuffle the lines so repeat calls do not accidentally repeat the same line (until lines exhausted)
Frameworks & Scriptlets:

skip bittman
Newbie
Posts: 5
Joined: Mon Jul 02, 2018 5:38 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#20 Post by skip bittman »

Perfect! Now it's randomizing properly from the hundreds of weird gibberish lines in the txt file. Thank you so much for pointing out "!q," missed that completely. Is there anything else to keep in mind with the formatting on the text file so everything works properly without squares being thrown in there?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#21 Post by Remix »

Try:

return unicode(self.lines.pop()).encode('utf-8').replace("\r", "") if self.lines else "No quotes left."

Not going to manage any non unicode characters mid string though. For those you'd have to either edit the file or do more replaces.
Frameworks & Scriptlets:

skip bittman
Newbie
Posts: 5
Joined: Mon Jul 02, 2018 5:38 pm
Contact:

Re: Reading randomized dialogue from file [SOLVED]

#22 Post by skip bittman »

That sorted it! Now to prevent myself from going overboard and abusing this.

Too late!


Post Reply

Who is online

Users browsing this forum: No registered users