Page 2 of 2

Re: Reading randomized dialogue from file [SOLVED]

Posted: Tue Jan 24, 2017 12:13 am
by Acceptable
Thank you, that was indeed the case. You saved me so much extra work! I feel like a bit of an idiot.

Re: Reading randomized dialogue from file [SOLVED]

Posted: Sun Feb 19, 2017 11:43 am
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

Re: Reading randomized dialogue from file [SOLVED]

Posted: Mon Jul 02, 2018 5:40 pm
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!

Re: Reading randomized dialogue from file [SOLVED]

Posted: Mon Jul 02, 2018 6:33 pm
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)

Re: Reading randomized dialogue from file [SOLVED]

Posted: Mon Jul 02, 2018 6:43 pm
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?

Re: Reading randomized dialogue from file [SOLVED]

Posted: Mon Jul 02, 2018 7:01 pm
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.

Re: Reading randomized dialogue from file [SOLVED]

Posted: Mon Jul 02, 2018 7:51 pm
by skip bittman
That sorted it! Now to prevent myself from going overboard and abusing this.

Too late!

Re: Reading randomized dialogue from file [SOLVED]

Posted: Fri Apr 19, 2024 12:57 am
by Andredron
Wow! Thank you!