Player Name With Toggled Buttons Help!

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
birdtrappedinacage
Newbie
Posts: 4
Joined: Fri Apr 23, 2021 8:09 pm
Projects: Diabolik Lovers (OC Route)
Tumblr: the-mostdiabolik-of-lovers
Contact:

Player Name With Toggled Buttons Help!

#1 Post by birdtrappedinacage » Fri Apr 23, 2021 8:45 pm

Hello! For my own personal project I am working on, I wanted to recreate a screen similar to the one below. I have all the assets (the background, the buttons which are in the english alphabet not Japanese, etc.) but I am just stumped on the coding for the actual implementation of it.
kR1tUgy.png
Image Example of Screen
My goal was to have the player click the buttons (multiple buttons) and then, depending upon what letters have been clicked and in what order, the player name will be selected based on that. For example, for the name Bella, the "B" button is clicked and it will appear under "First Name" in the first square, and then the button "E" is selected and so on. The only coding i have found for multiple selection is a toggle function as follows:

screen multichoice:
tag menu

add gui.name_select
default a = False
default b = False

imagebutton idle "gui/a.png" hover "gui/a_select.png" xpos 24 ypos 24 action ToggleScreenVariable("A"), activate_sound "select.wav"
imagebutton idle "gui/b.png" hover "gui/b_select.png" xpos 24 ypos 24 action ToggleScreenVariable("B"), activate_sound "select.wav"

Does anyone know what type of coding I would need to replace it with/add in to have the script function in the way i was hoping for?
Thank you in advance for any answers!

User avatar
Per K Grok
Miko-Class Veteran
Posts: 823
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1 & 2, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Player Name With Toggled Buttons Help!

#2 Post by Per K Grok » Mon Apr 26, 2021 1:31 am

birdtrappedinacage wrote:
Fri Apr 23, 2021 8:45 pm
Hello! For my own personal project I am working on, I wanted to recreate a screen similar to the one below. I have all the assets (the background, the buttons which are in the english alphabet not Japanese, etc.) but I am just stumped on the coding for the actual implementation of it.

kR1tUgy.png

My goal was to have the player click the buttons (multiple buttons) and then, depending upon what letters have been clicked and in what order, the player name will be selected based on that. For example, for the name Bella, the "B" button is clicked and it will appear under "First Name" in the first square, and then the button "E" is selected and so on. The only coding i have found for multiple selection is a toggle function as follows:

screen multichoice:
tag menu

add gui.name_select
default a = False
default b = False

imagebutton idle "gui/a.png" hover "gui/a_select.png" xpos 24 ypos 24 action ToggleScreenVariable("A"), activate_sound "select.wav"
imagebutton idle "gui/b.png" hover "gui/b_select.png" xpos 24 ypos 24 action ToggleScreenVariable("B"), activate_sound "select.wav"

Does anyone know what type of coding I would need to replace it with/add in to have the script function in the way i was hoping for?
Thank you in advance for any answers!

This is one way you could do this.


Start with defining a variable to hold a string for the name. Do this before label start and not as a part of the screen.

default pname=""


To add a letter to the string on a click on a button use this action

action SetVariable("pname", pname + "a")
This is for button A, Button B it would be pname + "b", and so on.


to show the string in the screen you just do
text pname

Code: Select all


default pname=""



screen multichoice():
    vbox:
        imagebutton idle "gui/a.png" action SetVariable("pname",p name + "a")
        imagebutton idle "gui/b.png" action SetVariable("pname", pname + "b")
        imagebutton idle "gui/c.png" action SetVariable("pname", pname + "c")
        textbutton "Close" action Return()
        text pname


label start:

    call screen multichoice

birdtrappedinacage
Newbie
Posts: 4
Joined: Fri Apr 23, 2021 8:09 pm
Projects: Diabolik Lovers (OC Route)
Tumblr: the-mostdiabolik-of-lovers
Contact:

Re: Player Name With Toggled Buttons Help!

#3 Post by birdtrappedinacage » Mon Apr 26, 2021 2:57 am

Per K Grok wrote:
Mon Apr 26, 2021 1:31 am

Code: Select all


default pname=""



screen multichoice():
    vbox:
        imagebutton idle "gui/a.png" action SetVariable("pname",p name + "a")
        imagebutton idle "gui/b.png" action SetVariable("pname", pname + "b")
        imagebutton idle "gui/c.png" action SetVariable("pname", pname + "c")
        textbutton "Close" action Return()
        text pname


label start:

    call screen multichoice
This worked wonderfully, thank you so much!

If I may, can I ask if it's possible to make a "clear" button / "backspace" button for this type of code? In case the player makes a mistake when clicking the buttons. Also, can I add a limit to the number of letters chosen?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 823
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1 & 2, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Player Name With Toggled Buttons Help!

#4 Post by Per K Grok » Mon Apr 26, 2021 1:23 pm

birdtrappedinacage wrote:
Mon Apr 26, 2021 2:57 am
----

This worked wonderfully, thank you so much!

If I may, can I ask if it's possible to make a "clear" button / "backspace" button for this type of code? In case the player makes a mistake when clicking the buttons. Also, can I add a limit to the number of letters chosen?
Clear is pretty simple

textbutton "Clear" action SetVariable("pname", "")


Backspace is a bit more tricky. This could be one way.

Code: Select all


        textbutton "Backspace" action Call("backspace")
     
        
--------------


label backspace:
    $ pname = pname[:-1]
    call screen multichoice
    return        


For the maximum length of the string you could put in a condition for each action that adds a letter to the string

action If(len(pname)<6, true=SetVariable("pname", pname + "a"), false=None)

This will limit the length of the string to six letters.

birdtrappedinacage
Newbie
Posts: 4
Joined: Fri Apr 23, 2021 8:09 pm
Projects: Diabolik Lovers (OC Route)
Tumblr: the-mostdiabolik-of-lovers
Contact:

Re: Player Name With Toggled Buttons Help!

#5 Post by birdtrappedinacage » Tue Apr 27, 2021 5:59 am

Per K Grok wrote:
Mon Apr 26, 2021 1:23 pm
Clear is pretty simple

textbutton "Clear" action SetVariable("pname", "")


Backspace is a bit more tricky. This could be one way.

Code: Select all


        textbutton "Backspace" action Call("backspace")
     
        
--------------


label backspace:
    $ pname = pname[:-1]
    call screen multichoice
    return        


For the maximum length of the string you could put in a condition for each action that adds a letter to the string

action If(len(pname)<6, true=SetVariable("pname", pname + "a"), false=None)

This will limit the length of the string to six letters.
Thank you so much! Works perfectly. :)

Post Reply

Who is online

Users browsing this forum: Google [Bot]