Trying to add a variable in dialogue

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.
Post Reply
Message
Author
TheChatotMaestro
Regular
Posts: 91
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Trying to add a variable in dialogue

#1 Post by TheChatotMaestro » Mon Jul 31, 2017 8:45 am

Am I making a stupid mistake?
I have a system here for drawing cards- it tells you what rarity of card you got, and what character's card you got. It's very basic, and in no way done, but I was testing it to make sure it worked at this level and I kept getting this error message.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 147, in script
    "You got a [girlnam] R card!"
KeyError: u'girlnam'

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

Full traceback:
  File "game/script.rpy", line 147, in script
    "You got a [girlnam] R card!"
  File "C:\Users\SerebiiGamer\Desktop\renpy-6.99.12.4-sdk\renpy\ast.py", line 613, in execute
    renpy.exports.say(who, what, interact=self.interact)
  File "C:\Users\SerebiiGamer\Desktop\renpy-6.99.12.4-sdk\renpy\exports.py", line 1147, in say
    who(what, interact=interact)
  File "C:\Users\SerebiiGamer\Desktop\renpy-6.99.12.4-sdk\renpy\character.py", line 868, in __call__
    what = what_pattern.replace("[what]", sub(what, translate=translate))
  File "C:\Users\SerebiiGamer\Desktop\renpy-6.99.12.4-sdk\renpy\character.py", line 850, in sub
    return renpy.substitutions.substitute(s, scope=scope, force=force, translate=translate)[0]
  File "C:\Users\SerebiiGamer\Desktop\renpy-6.99.12.4-sdk\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'girlnam'

Windows-8-6.2.9200
Ren'Py 6.99.12.4.2187
Hyper Stars! Cyber Idol Project 0.0
The block in question is this:

Code: Select all

label RESULTSPROTO:
    if result == "R":
        "You got a [girlnam] R card!"
        jump scout
    if result == "SR":
        "You got a [girlnam] SR card!"
        jump scout
    if result == "SSR":
        "You got a [girlnam] SSR card!"
        jump scout
    if result == "UR":
        "You got a [girlnam] UR card!"
        jump scout
What this is supposed to do is tell you what rarity you got, determined earlier via RNG, and then use the variable to tell you the name of the character who's card you got (also determined earlier via RNG), but obviously I'm not doing it right. Can someone tell me how I can fix this? I can provide other parts of the code if needed.

User avatar
Scribbles
Miko-Class Veteran
Posts: 636
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island, As We Know It
Projects: In Blood
Organization: Jaime Scribbles Games
Deviantart: breakfastdoodles
itch: scribbles
Location: Ohio
Contact:

Re: Trying to add a variable in dialogue

#2 Post by Scribbles » Mon Jul 31, 2017 11:51 am

where and how is girlnam defined? my best guess is that it isn't defined properly, otherwise I'm not sure.
Image - Image -Image

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: Trying to add a variable in dialogue

#3 Post by Remix » Mon Jul 31, 2017 5:00 pm

Try adding
default girlnam = "Null"
or some text outside of any block or init.

Ren'py will endeavour to read ahead through labels, so might read [girlnam] before any python code runs that sets it, so just set a default and it should be good.
Frameworks & Scriptlets:

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: Trying to add a variable in dialogue

#4 Post by Remix » Mon Jul 31, 2017 5:03 pm

P.S.

Code: Select all

label RESULTSPROTO:
    "You got a [girlnam] [result] card!"
    jump scout
would suffice as you are basically just saying 'girlnam result' then jumping...
obviously set a default value for result too
Frameworks & Scriptlets:

TheChatotMaestro
Regular
Posts: 91
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Trying to add a variable in dialogue

#5 Post by TheChatotMaestro » Mon Jul 31, 2017 11:15 pm

Ok, a bit more context:
At first, I forgot defining it at the beginning was necessary, and added this in a prior place in order to 'reset' the variables before all this process starts:

Code: Select all

    $ girlnam = "none"
    $ result = "none"
    $ rarity = "none"
    $ group = "none"
    $ scout = "none"
But I removed it as I thought it was the source of a smaller problem in which the girl's name would be stuck as none forever.
This is what I use to do it:

Code: Select all

label soloyolo:
    $ scout = renpy.random.randint(0, 100)

    if scout > 25:
        $ result = "R"
        jump girl
        
    elif scout > 15:
        $ result = "SR"
        jump girl

    elif scout > 5:
        $ result = "SSR"
        jump girl

    else:
        $ result = "UR"
        jump girl
        
label girl:
    $ girlnum = renpy.random.randint(1, 12)
    if group == "Chikara":
        jump ChikaraGirls
    elif group == "Rainbow":
        jump RainbowGirls
        
label ChikaraGirls:
        if girlnum == "1":
            $ girlnam = "Namiko"
            jump RESULTSPROTO
        elif girlnum == "2":
            $ girlnam = "Risuka"
            jump RESULTSPROTO
        elif girlnum == "3":
            $ girlnam = "Kazuno"
            jump RESULTSPROTO
        elif girlnum == "4":
            $ girlnam = "Emika"
            jump RESULTSPROTO
        elif girlnum == "5":
            $ girlnam = "Miyabi"
            jump RESULTSPROTO
        elif girlnum == "6":
            $ girlnam = "Shigemi"
            jump RESULTSPROTO
        elif girlnum == "7":
            $ girlnam = "Hayane"
            jump RESULTSPROTO
        elif girlnum == "8":
            $ girlnam = "Chiya"
            jump RESULTSPROTO
        elif girlnum == "9":
            $ girlnam = "Tsukumi"
            jump RESULTSPROTO
        elif girlnum == "10":
            $ girlnam = "Yutsuko"
            jump RESULTSPROTO
        elif girlnum == "11":
            $ girlnam = "Ina"
            jump RESULTSPROTO
        elif girlnum == "12":
            $ girlnam = "Osami"
            jump RESULTSPROTO
        
label RainbowGirls:
        if girlnum == "1":
            $ girlnam = "Taree"
            jump RESULTSPROTO
        elif girlnum == "2":
            $ girlnam = "Waki"
            jump RESULTSPROTO
        elif girlnum == "3":
            $ girlnam = "Mikashi"
            jump RESULTSPROTO
        elif girlnum == "4":
            $ girlnam = "Tamaki"
            jump RESULTSPROTO
        elif girlnum == "5":
            $ girlnam = "Sachiko"
            jump RESULTSPROTO
        elif girlnum == "6":
            $ girlnam = "Sakae"
            jump RESULTSPROTO
        elif girlnum == "7":
            $ girlnam = "Kanaya"
            jump RESULTSPROTO
        elif girlnum == "8":
            $ girl = "Ruru"
            jump RESULTSPROTO
        elif girlnum == "9":
            $ girlnam = "Emiya"
            jump RESULTSPROTO
        elif girlnum == "10":
            $ girlnam = "Wakuri"
            jump RESULTSPROTO
        elif girlnum == "11":
            $ girlnam = "Kunie"
            jump RESULTSPROTO
        elif girlnum == "12":
            $ girlnam = "Orime"
            jump RESULTSPROTO
First it determines the card rarity, then the character (there are 24 characters sorted into 2 groups) and then it tells you what it got. In the future, it will either pick from the pool of cards with that rarity and girl combination, or, more likely since I came up with it between posting this and now, it will pick from the rarity pool and then tell you what girl and other information based on reading what card that is. However, I'm still not sure what's wrong about this prototype... It never gives me the name of the girl, and I keep getting only one rarity- although, that might be my awful luck rather than an actual issue. (Never Soloyolo, kids.) Am I changing the variable wrong?
Edited to add: I'm not getting any actual error message after replacing the code at the top.

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: Trying to add a variable in dialogue

#6 Post by Remix » Tue Aug 01, 2017 6:31 am

Using defaults and list comprehensions to allow renpy to use .choice()

Sub-note:
I do not expect you to understand the comprehension (the [x for y in [] for x in y] bits)
They basically take the dict keys and make a list by multiplying them by the tuple value,
so when choosing a girl from Chikara, Ren'py would be selecting one items from a list that
contained Namiko 5 times, Yuksuko once, Osami 4 times etc... adjust to (1,) if all same rarity...

Code: Select all

default girl_cards = {
    # pack name : (rarity of this pack using lowest denominator, (dict of girls))
    "Chikara" : (
        1, 
        {
            # girl name : (rarity of this girl using a low max int like above)
            "Namiko" : (5,),
            "Risuka" : (5,),
            "Kazuno" : (5,),
            "Emika" : (5,),
            "Miyabi" : (5,),
            "Shigemi" : (5,),
            "Hayane" : (5,),
            "Chiya" : (5,),
            "Tsukumi" : (5,),
            "Yutsuko" : (1,), # super rare rare
            "Ina" : (5,),
            "Osami" : (4,),
        }
    ),
    "Rainbow" : (
        1, 
        {
            "Taree" : (1,), # all same rarity, so using 1
            "Waki" : (1,),
            "Mikashi" : (1,),
            "Tamaki" : (1,),
            "Sachiko" : (1,),
            "Sakae" : (1,),
            "Kanaya" : (1,),
            "Ruru" : (1,),
            "Emiya" : (1,),
            "Wakuri" : (1,),
            "Kunie" : (1,),
            "Orime" : (1,),
        }
    )
}
default rarity_chances = {
    # rarity name : (rarity of this choice,),
    "R" : (15,), # lowest order... 75/5 = 15
    "SR" : (3,),
    "SSR" : (2,),
    "UR" : (1,),

}
# always set defaults even if we quickly change them
default chosen_pack = "None"
default chosen_girl = "None"
default chosen_rare = "None"

label start:
    "Choosing a card now..."
    $ chosen_pack = renpy.random.choice( [x for y in [[i]*girl_cards[i][0] for i in girl_cards] for x in y] )
    # chosen pack is now a string
    # "[chosen_pack]"
    $ chosen_girl = renpy.random.choice( [x for y in [[i]*girl_cards[chosen_pack][0]*girl_cards[chosen_pack][1][i][0] for i in girl_cards[chosen_pack][1]] for x in y] )
    # chosen_girl is now a string
    # "[chosen_girl]"
    $ chosen_rare = renpy.random.choice( [x for y in [[i]*rarity_chances[i][0] for i in rarity_chances] for x in y] )
    # chosen_rare is just a string
    "The card was [chosen_girl] who is a [chosen_pack]Girl [chosen_rare]"
    # jump RESULTSPROTO
    jump start
Frameworks & Scriptlets:

TheChatotMaestro
Regular
Posts: 91
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Trying to add a variable in dialogue

#7 Post by TheChatotMaestro » Tue Aug 01, 2017 9:11 am

Ah, thank you! I tested it out a few times, and the changes I made so the buttons led to the right places didn't screw it up, which is great! However, there's still one more problem, and I also have a couple of questions.
The problem is there's two menus you go through once you select 'scouting'. One chooses what group you're scouting for, and the other chooses how many cards you want at once. (Scouting's going to cost currency, eventually.) How do I split this up so if you chose 'rainbow stardrops', you can only get their cards, and if you chose 'chikara', you can only get their cards? I'm getting girls from both groups in both boxes.
Question One: Like I mentioned before, there's multiple ways to scout. You can either draw one card, or 11 for the price of 10. How do I make it so you draw 11 cards at once? Repeating the code would be fine, as that could make for a nifty sequence of images as each card is determined, but I have no idea how to code that.
Question Two: What does the 'pack' do, and if I want to make, say, multiple sets of cards of the same girls with different themes, do I just make multiple packs? If that was done, I might be able to eliminate the 'rarity' bit altogether, since certain girls have rarer cards in different sets.
Question Three: Is it possible to have multiple copies of the 'script' file that have code leading into and out of each other? For example, I'd have all the stories on one file, all the scouting mechanics and cards on another, and the rest of the game features on a third. Is that possible, and if so, is it a bad idea? I'd just move all the scouting code from here to the other file. EDIT: Found out how to do this while looking up something else

Sorry if this is getting too off-topic... ^^"

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: Trying to add a variable in dialogue

#8 Post by Remix » Tue Aug 01, 2017 12:34 pm

Question 0:
Just set the $ chosen_pack = "Chikara" variable to select one pack
The chosen_girl = renpy.choice(list comprehension) bit just uses the variable chosen_pack within it

Question 1:
It partly depends on how you are storing the player's cards.
A for or while loop *should* suffice to pick cards though

Pseudo Ren'py code:
for k in range(11):
....# determine a card using renpy.choice codes
....player_cards.append( info obtained from the random choice bits )

Question 2:
Sounds like you would need no random choice code there
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Google [Bot]