[Solved] Button list order recognition

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
KuroOneHalf
Regular
Posts: 129
Joined: Fri Apr 25, 2014 6:18 pm
Completed: Cuttlebone
Projects: Somewhere In The Shade
Deviantart: KuroOneHalf
itch: kuroonehalf
Contact:

[Solved] Button list order recognition

#1 Post by KuroOneHalf »

I'm honestly not sure how else to title this. Here's some sample code of the conundrum.

Say I have a stack of buttons like so:

Code: Select all

screen somescreen:
    vbox:
        for i in range(1, n):
            textbutton _(str(i)) action Function("printposition",x)
 
init -2 python:
    def printposition(x):
        print x
And when I press a button, like say the 3rd button from the top, I want the number corresponding to that button's order to be printed to the console - in this case the output to console would be "3". If I click the 7th from the top it prints "7", and so on.
The problem is how do I know the button's order when I click it? What is x?

This is meant to support arbitrarily long stacks of buttons, so I can't hack a solution for a fixed n amount.

Help figuring this out would be super appreciated!
Last edited by KuroOneHalf on Fri Dec 02, 2016 10:25 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Button list order recognition

#2 Post by Ocelot »

Well, without knowing exact requirements and what are you trying to do, I would store a list of objects which contain some information about button and then do something like:

Code: Select all

class Button:
    __init__(self, text, action):
        self.text = text
        self.action = action

buttons = [ Button('button 1', 'dosomething'), Button('another button', 'dosomethingelse'),   . . .   ]

screen somescreen:
    vbox:
        for x, b in enumerate(buttons):
            textbutton b.text action [Function(b.action), Function("printposition", x)]
< < insert Rick Cook quote here > >

User avatar
KuroOneHalf
Regular
Posts: 129
Joined: Fri Apr 25, 2014 6:18 pm
Completed: Cuttlebone
Projects: Somewhere In The Shade
Deviantart: KuroOneHalf
itch: kuroonehalf
Contact:

Re: Button list order recognition

#3 Post by KuroOneHalf »

enumarate() totally did the trick. Thank you so much!

Post Reply

Who is online

Users browsing this forum: Google [Bot]