Using Array for Menu items?

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
kzmaster
Newbie
Posts: 11
Joined: Thu Sep 26, 2019 4:16 pm
Organization: Kzmaster Entertainment LLC 2020
Deviantart: kzmaster
Contact:

Using Array for Menu items?

#1 Post by kzmaster » Thu Jan 07, 2021 2:13 pm

Greetings all,

I'm completely stumped on how to get this function to work properly. For this game, a player can have between 1-5 characters.
I'm trying to establish a character select screen that retrieves the player's character(s) from an API and returns them in an array.

But I can't figure out how to list the items in the array as as selectable menu items.

Code: Select all

"Train Character":
            python:
                r = requests.get('http://127.0.0.1:8000/getchars/' + username)
                r = r.json()
                fighters = r['fighters']
                fighter_names = []
                for i in fighters:
                    fighter_names.append(i)
                j = 0
            jump character_select

    label character_select:                
        screen hbox_screen:
            hbox:
                while j < len(fighter_names):
                textbutton "[fighter_names[j]]"
                $ j += 1  
                
Any help is appreciated!
KZ

User avatar
RicharDann
Veteran
Posts: 284
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Using Array for Menu items?

#2 Post by RicharDann » Thu Jan 07, 2021 2:49 pm

You should'nt define a screen inside a label, you need to define it outside of a block, and then call the screen from the label with a show screen or call screen statement.
Also, in the screen, be sure to check indentation, in the while loop textbuttons should be indented below the statement or it will cause errors. And why not use for loop instead of while?

Code: Select all

# outside of label
screen hbox_screen:
    hbox:
        for i in fighter_names:
            textbutton i action NullAction() #here would go the function that would run when character is selected

# in your script, after filling the array
label character_select:
    call screen hbox_screen
The most important step is always the next one.

User avatar
kzmaster
Newbie
Posts: 11
Joined: Thu Sep 26, 2019 4:16 pm
Organization: Kzmaster Entertainment LLC 2020
Deviantart: kzmaster
Contact:

Re: Using Array for Menu items?

#3 Post by kzmaster » Thu Jan 07, 2021 5:18 pm

Hey thank you very much, this worked perfectly.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]