[Solved] Concatenate string and variable within menu choice

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
GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

[Solved] Concatenate string and variable within menu choice

#1 Post by GMHLee » Fri Aug 26, 2022 2:39 pm

Hi guys, first question. I'm looking to concatenate a string with a calculated variable to a menu choice to show percentage chance as in the example below.

Only thing of course is that the below code won't run, crashing with an error of ": expected". Any help would be appreciated, thanks.

Code: Select all

menu:
        "Test text (" + str(Ava_stats.a*0.4+Ava_stats.b*0.1) + "% chance of +1)":
Last edited by GMHLee on Sun Aug 28, 2022 7:25 am, edited 1 time in total.

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Concatenate string and variable within menu choice

#2 Post by m_from_space » Fri Aug 26, 2022 2:54 pm

In python it's always useful to use the function format() to connect things to form a string. But in this case you have to do it the following way:

Code: Select all

$ val = Ava_stats.a*0.4+Ava_stats.b*0.1
menu:
    "Test text ([val] % chance of +1)":
        pass

GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

Re: Concatenate string and variable within menu choice

#3 Post by GMHLee » Sat Aug 27, 2022 5:58 am

Aahh... I see, thanks. I was actually hoping it wasn't something like that and it's my fault for not giving clearer info.

Currently my characters have 6 stats. The above example is for the chance of one stat to go up. In future I envision seeing some events where all six have individual chances of changing, and with or 4 different characters.

That means 24 calculations have to be done regardless of condition and a potential 18 of those wasted on unnecessary overhead. Now this is a visual novel, not the next Mass Effect. It doesn't much matter on that waste but the internal programmer in me cringes at such inefficiency. I assume no other way forward as it stands?

Which lends to my second point, and this is out of the scope of the original question sorry, is that I'm confident I probably couldn't fit the 6 stat percent changes of change and the value of how much they change by onto one line. Probably might have to code a custom player choice menu?

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Concatenate string and variable within menu choice

#4 Post by enaielei » Sat Aug 27, 2022 6:41 am

More pythonic: https://www.renpy.org/doc/html/statemen ... splay_menu
You can also alter the choice screen a bit to use .format then pass the args and kwargs.

Code: Select all

screen choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            textbutton i.caption.format(*i.args, **i.kwargs) action i.action

label start:
  menu:
    "{}% chance" (0.25*100):
      pass

GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

Re: Concatenate string and variable within menu choice

#5 Post by GMHLee » Sat Aug 27, 2022 7:26 am

thanks for that, I'll take a look.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]