How to make textbutton "[lst[index_number]]" work? [SOLVED]

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
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

How to make textbutton "[lst[index_number]]" work? [SOLVED]

#1 Post by Luxliev »

Code: Select all

init python:
    my_string = "string"
    lst = [my_string]
    index_number = 0
    
screen testscreen:
    # textbutton "[lst[index_number]]"       # this gives me TypeError: list indices must be integers, not unicode
    textbutton "[lst[0]]"                    # this works
How can I make number that is variable inside another square bracket work correctly with textbutton?
Last edited by Luxliev on Thu Aug 03, 2017 1:40 pm, edited 2 times in total.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

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: How to make textbutton "[lst[index_number]]" work?

#2 Post by Remix »

I think you will have to use a temporary variable prior to the string interpolation

$ button_text = lst[index_number]
textbutton "[button_text]"

Ren'py is a bit fussy with doing some python code within the interpolation, so putting that outside (pre-doing it) is often the easiest way
Frameworks & Scriptlets:

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: How to make textbutton "[lst[index_number]]" work?

#3 Post by Luxliev »

Yeah I can do that but it's only fix for simple problem if I want to access list of class inside other list then code will quickly become messy.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How to make textbutton "[lst[index_number]]" work?

#4 Post by philat »

Bracket interpolation (which is renpy-specific) just doesn't support it. You can use python's own interpolation, or use an intermediary variable as Remix suggested.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot]