[Solved] Changing "say" in in-game menu with a variable

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
User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

[Solved] Changing "say" in in-game menu with a variable

#1 Post by Lezalith » Tue Nov 01, 2016 7:33 pm

Hey people!
Alright, so let's pretend we have a menu.

Code: Select all

menu:
    "Are you a boy or a girl?"
    "I'm a boy.":
        "You get to wear the blue shirt!"
    "I'm a girl.":
        "You get to wear the red shirt!"
    "I identify as a helicopter." if WeirdosAllowed == 1:
        "You get to wear this... metal... sheet...?"
I hope I didn't offend anybody. For some reason, this was the first thing that came to mind.
Let's say we'd want the "say" statement (That is currently "Are you a boy or a girl?") affected by our variable. How would we do that?

Code: Select all

menu:
    "Are you a boy or a girl?" if WeirdosAllowed == 0
    "Are you a boy, girl or a helicopter?" if WeirdosAllowed == 1
    "I'm a boy.":
        "You get to wear the blue shirt!"
    "I'm a girl.":
        "You get to wear the red shirt!"
    "I identify as a helicopter." if WeirdosAllowed == 1:
        "You get to wear this... metal... sheet...?"
This code is wrong, because the game won't start up and will want the ":" at the end of the line. That would make it a menuitem, however.

Code: Select all

menu:
    if WeirdosAllowed == 0:
        "Are you a boy or a girl?"
    else:
        "Are you a boy, girl or helicopter?"
    "I'm a boy.":
        "You get to wear the blue shirt!"
    "I'm a girl.":
        "You get to wear the red shirt!"
    "I identify as a helicopter." if WeirdosAllowed == 1:
        "You get to wear this... metal... sheet...?"
Will not let you start up either, saying it expects a menuitem instead of the "if" statement.
So, is there a way how to make the "say" depend on a variable?
Last edited by Lezalith on Tue Nov 01, 2016 9:36 pm, edited 3 times in total.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Changing "say" in ingame-menu with a variable

#2 Post by trooper6 » Tue Nov 01, 2016 8:07 pm

You could do something like:

Code: Select all

label start:
    if weirdos_allowed:
        $cap = "Are you a boy, a girl, or a helicopter?"
    else:
        $cap = "Are you a boy or a girl?"
        
    menu:
        "[cap]"
        "I'm a boy.":
            "You get to wear the blue shirt!"
        "I'm a girl.":
            "You get to wear the red shirt!"
        "I identify as a helicopter." if weirdos_allowed:
            "You get to wear this...metal...sheet...?"
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Changing "say" in in-game menu with a variable

#3 Post by Lezalith » Tue Nov 01, 2016 8:15 pm

Oh, that's a very good idea! Thanks!

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Changing "say" in in-game menu with a variable

#4 Post by Lezalith » Tue Nov 01, 2016 8:29 pm

Damn, and it looked so promising. I even flagged this as [Solved]

I didn't think that this would be a problem, but there is a variable inside that string. To explain...
The "say" I need to change is

$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with. You also have a medkit."
Where [supplies] are of course a variable, a number. When/If the medkit is sold, it changes to
$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with."

But because the menu is

Code: Select all

menu:
    [textInMenu]
    option1:
        etc......
    option2:
        etc......
In the "say" statement, it literally shows "[supplies]" instead of the value of the variable.
x.x

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Changing "say" in in-game menu with a variable

#5 Post by trooper6 » Tue Nov 01, 2016 8:52 pm

Lezalith wrote:Damn, and it looked so promising. I even flagged this as [Solved]

I didn't think that this would be a problem, but there is a variable inside that string. To explain...
The "say" I need to change is

$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with. You also have a medkit."
Where [supplies] are of course a variable, a number. When/If the medkit is sold, it changes to
$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with."

But because the menu is

Code: Select all

menu:
    [textInMenu]
    option1:
        etc......
    option2:
        etc......
In the "say" statement, it literally shows "[supplies]" instead of the value of the variable.
x.x
It should be

Code: Select all

menu:
    "[textInMenu]"
With quotation marks, is that how you did it?

Because the code I posted comes directly from a fresh Renpy project I made to answer your question, and it works. Try putting my code into a fresh project to see.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Changing "say" in in-game menu with a variable

#6 Post by Lezalith » Tue Nov 01, 2016 9:03 pm

Oh yeah, I have quotation marks there.

Code: Select all

$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with. You also have a medkit."

Code: Select all

label week10trade:
    menu:
        "[textInMenu]"
Image

I'm getting Minecraft flashbacks now. I used to have the same problems there with a variable inside a string.

User avatar
NialGrenville
Regular
Posts: 184
Joined: Tue Aug 30, 2016 6:30 pm
Projects: PaS: The Awakening; The Heart Woods; The Witch and the Fairy
Organization: Gabe Works, Inc.
Contact:

Re: Changing "say" in in-game menu with a variable

#7 Post by NialGrenville » Tue Nov 01, 2016 9:11 pm

Don't you have a supplies var? If that is what is poping up, the only explination is the lack of a var (that is an assumption of course).
*Me*, *You, ***Calamazoo: *Singularity-A.I.*: *Proceed to processing
PaS: The Awakening, current status: 42%
The HeartWoods, current status: 0%
The Wonderful Mell! My PFI creator Honest Critique

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Changing "say" in in-game menu with a variable

#8 Post by Lezalith » Tue Nov 01, 2016 9:15 pm

You mean, the variable not being defined?

Nope, it is defined right below the start label. :/

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Changing "say" in in-game menu with a variable

#9 Post by trooper6 » Tue Nov 01, 2016 9:17 pm

I see what the problem is. This post is a place holder while I'm writing up an explanation of what's going on.

So, in a Renpy program, you can have lines written in Ren'Py or you can have lines written in Python. Lines written in Python have the $ in front of them, or they are in a python block.

Let's look at do different ways of having the character e say something.
In Renpy:

Code: Select all

e "Hello"
In Python:

Code: Select all

$e("Hello")
String interpolation allows you to put a variable in a string. Variables are declared in Python. So let's have a variable:

$ var = "World!"

The thing to remember is that Renpy and Python interpolate strings differently. Renpy interpolates using [var]. Python interpolates with .format.

So in Renpy:

Code: Select all

e "Hello [var]"
Will give you: "Hello World!"

But in Python you must you Python interpolation. So:

Code: Select all

$e("Hello {0}".format(var))
That will give you: "Hello World!"

So you have two bits of code, one in Renpy and One in Python and you are interpolating in both...but you use the renpy interpolation for both, when you should you renpy for renpy and python for python.

So this, which is Python:

Code: Select all

$ textInMenu = "\n\n\nYou currently have [supplies] supplies to trade with. You also have a medkit."
should use python interpolation and look like this:

Code: Select all

$ textInMenu = "\n\n\nYou currently have {0} supplies to trade with. You also have a medkit.".format(supplies)
While this is in Renpy and uses the Renpy interpolation properly.

Code: Select all

label week10trade:
    menu:
        "[textInMenu]"
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Changing "say" in in-game menu with a variable

#10 Post by trooper6 » Tue Nov 01, 2016 9:29 pm

Hope that helps!
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Changing "say" in in-game menu with a variable

#11 Post by Lezalith » Tue Nov 01, 2016 9:36 pm

It helped a ton! Works like a charm, and I'm glad you actually made me understand that. Surely will be useful in the future :)

User avatar
NialGrenville
Regular
Posts: 184
Joined: Tue Aug 30, 2016 6:30 pm
Projects: PaS: The Awakening; The Heart Woods; The Witch and the Fairy
Organization: Gabe Works, Inc.
Contact:

Re: Changing "say" in in-game menu with a variable

#12 Post by NialGrenville » Tue Nov 01, 2016 9:37 pm

*Processing trooper6 saving the day*
*Generating response*
*Finished: trooper6 Lezalith's hero!
*Me*, *You, ***Calamazoo: *Singularity-A.I.*: *Proceed to processing
PaS: The Awakening, current status: 42%
The HeartWoods, current status: 0%
The Wonderful Mell! My PFI creator Honest Critique

Post Reply

Who is online

Users browsing this forum: Google [Bot]