Page 1 of 1
KeyError: u'playername' when dialogue statements come up.
Posted: Mon Feb 20, 2017 7:50 pm
by Steffenator
Hey gang!
I'm getting an error... the one in the subject. It only happens in one area of the game so far. Though I seem to be partially blind to minor syntax typos, I've tried to look over it and have found no difference to the dialogue in the rest of the game.
This particular issue happens within a player choice menu. Though it's not the only one, just the one that gives me this excellent error.
The error below is what comes from only one of the buttons, though they all do the same thing.
"[playername]" is the name of the player within the dialogue, so that a typed-in name will appear there.
Code: Select all
I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/lstare01.rpy", line 45, in script
me "*You shake your head and rub your face with both hands.*\nSorry... my eyes got stuck."
KeyError: u'playername'
-- Full Traceback ------------------------------------------------------------
Full traceback:
File "game/lstare01.rpy", line 45, in script
me "*You shake your head and rub your face with both hands.*\nSorry... my eyes got stuck."
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
Any ideas?
Re: KeyError: u'playername' when dialogue statements come up
Posted: Mon Feb 20, 2017 8:10 pm
by PyTom
The obvious problem is that playername is not set when this code has been run.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Mon Feb 20, 2017 11:04 pm
by Steffenator
You mean by this line right here...?
Code: Select all
define me = Character("[playername]", color="#000000", font="ALEAWB_.ttf", size=35, outlines=[ (2, "#ffffff") ])
As I said, the error only happens in one part of the game. The name and associated dialogue work fine everywhere else.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Mon Feb 20, 2017 11:15 pm
by Imperf3kt
When in the script, does a player set the playername?
Re: KeyError: u'playername' when dialogue statements come up
Posted: Mon Feb 20, 2017 11:44 pm
by Steffenator
Here's the code of the menu...
Code: Select all
label lstare01:
### Variables
$ lStare01 = False
$ Points_Tguy += 1
$ lStare01 = True
show jodskirt02 at right with Dissolve(.5)
a "Be careful."
hide jodskirt02 with Dissolve(.5)
hide lsmile01 with Dissolve(.2)
show lsmirk01 with Dissolve(.5)
l "You look like you might be trouble..."
menu:
"Avoid her":
$ Points_Gentle += 1
me "Sorry, I... I'll just wait out here."
hide lsmirk01 with Dissolve(.2)
show lsmile01 with Dissolve(.2)
l "Do what you want, but you're welcome to come in."
me "I think I'll be fine."
jump yourName
"Flirt":
me "I'm not sure, but...\nIf I'd known my cousin had such an attractive room-mate, I would have stopped by months ago."
l "Ha ha. I'll have to be careful around you."
me "Oh, don't worry. I'm harmless."
$ Points_Sexy += 1
$ Points_Fun += 1
jump yourName
"Make a joke":
$ Points_Funny += 1
$ Points_Fun += 1
me "*You shake your head and rub your face with both hands.*\nSorry... my eyes got stuck."
hide lsmirk01 with Dissolve(.2)
show llaugh01 with Dissolve(.2)
l "Ha ha. It's all right."
me "It happens sometimes."
jump yourName
@ Imperf3kt... The player name is set about line 180. Downstream of the
label start line, using the code below... got it off of youtube.
It's immediately before the line that sends the game to a different document for the above menu. So, the jump command sends the game back to the script.rpy file.
The characters names and abbreviations are defined uphill of the
label start line, but the rest of them have never given me problems.
Code: Select all
label yourName:
scene fdooropen
show lsmile01 with D_2
l "Spring already told me, but I have a lousy memory, so..."
$playername = renpy.input("What's your name?")
$playername = playername.strip()
I don't know if it makes a difference, but this part of the game wasn't having problems until after I updated a couple days ago. Could there be some code that was updated in the Renpy app that would prefer newer code in my game. I started making the game on the previous version.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 12:48 am
by Imperf3kt
Looks like you're missing a part. If a player doesn't set a name, ie leaves it blank, you need to define it.
The renpy documentation explains it quite well.
https://www.renpy.org/wiki/renpy/doc/co ... r_own_name
This is the outdated wiki, but its still the same in the new documentation.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 12:50 am
by philat
Just throw a
default playername = "smth"
somewhere. Like, why go through the hassle of figuring otherwise when the answer is this easy?
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 1:48 am
by Steffenator
I thought I did that when I added this definition.
Code: Select all
define me = Character("[playername]", color="#000000", font="ALEAWB_.ttf", size=35, outlines=[ (2, "#ffffff") ])
Or do you mean, leave the typed name blank? because the bit under the "yourName" label is what makes [playername] thing an actual name. It asks the player what his or her name is, then replaces [playername] in the dialogue with that and in the box that puts the name of the "speaker" with said dialogue. I promise, the thing works during every other part of the game.
I've even tried leaving the name field blank in the game, just to see if it caused an error, but it just leaves the place where a name usually goes blank in the game.
There's a break in understanding here. Either I don't get what you guys are telling me, or I'm not explaining myself clearly enough.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 2:00 am
by Divona
[playername] is a variable.
Character() is a class.
define me = Character() - is decalre a variable named "me" and assign Character class to it.
default playername = "" - is decalre a variable named "playername" and assign empty string to it as default value.
You haven't declared "playername" variable. Therefore if the player doesn't set a name, that variable doesn't exist, and Ren'Py can't find it, so Ren'Py scream in agony and send out errors.
What you have to do here is to declare a variable call "playername" before use it:
Code: Select all
default playername = "?"
define me = Character("[playername]", color="#000000", font="ALEAWB_.ttf", size=35, outlines=[ (2, "#ffffff") ])
label start:
. . .
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 2:03 am
by Steffenator
Ok, I figured it out... (I typed this response as Divona submitted the previous post. Thanks anyway Divona. You beat me by seconds.)
It's true, we were't understanding each other.
I went back to what Imperf3kt asked about when in the script is the playername set... It's not a matter of the name not being set, it's that it hadn't been set YET.
I did what philat said and set a default so the player is just called "You" until the name is set.
I'm only half an idiot, but at least I understood in the end.
Thanks guys, and sorry for the misunderstanding.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 4:01 am
by Imperf3kt
Hmm, upon looking again, that wiki is not actually useful at all.
I guess that's what happens when you're not able to fully check what you link.
In future, for any other variable you may have, remember that you need to define a "default" before your game starts, so that even if nothing is set during the game, it'll still have something to go by and not fail to work.
Divona explained it best.
Think of define me = Character("[playername]") as what you were replacing.
In this case, you have told renpy what "me" means and what to replace - playername - but you haven't told renpy what to replace it with, at least, not if you load an old save or start the game from a point where the player doesn't type anything into the variable.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 11:08 am
by Steffenator
The wiki had some dang good info in it, but it just didn't specify anything related to my mistake. There's no such thing as perfect documentation. Fer instance: I still haven't found anything that specifies what variables and definitions can go before
label start, and which must go after.
Either way, despite the misunderstanding, this thread has given me some great information.
I appreciate your guys' patience.
Question related to this... I wanted to put a rule related to setting the playername with something like this...
Code: Select all
if playermame.len() == 0 or not playername.isalpha():
$ playername = Jack
l "[i]Jack[/i] it is then."
But when it didn't work, I realized that I was mixing Renpy with good ol' Python. Is there a Renpy equivelant of the .len() and .isalpha thingy?
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 12:09 pm
by Divona
Not really sure what you're trying to do, but here it is:
Code: Select all
if len(playername) == 0 or not playername.isalpha():
$ playername = "Jack"
l "[i]Jack[/i] it is then."
Ren'Py is still Python. Not sure if Python even have str.len(). Usually it's len(str).
The wiki had some dang good info in it, but it just didn't specify anything related to my mistake. There's no such thing as perfect documentation. Fer instance: I still haven't found anything that specifies what variables and definitions can go before {i}label start{/i}, and which must go after.
Do try to avoid the old wiki, it's outdated. There is
new Ren'Py documentation available, and the information on init/define/default statements are in
Python Statements section.
Re: KeyError: u'playername' when dialogue statements come up
Posted: Tue Feb 21, 2017 1:58 pm
by Steffenator
Thanks, Divona, you're a doll.
By the by, the art and design of The Falconers Moonlight is gorgeous. I've always loved that industrial era style.
