Page 1 of 1
Player Name With Toggled Buttons Help!
Posted: Fri Apr 23, 2021 8:45 pm
by birdtrappedinacage
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.
- 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!
Re: Player Name With Toggled Buttons Help!
Posted: Mon Apr 26, 2021 1:31 am
by Per K Grok
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
Re: Player Name With Toggled Buttons Help!
Posted: Mon Apr 26, 2021 2:57 am
by birdtrappedinacage
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?
Re: Player Name With Toggled Buttons Help!
Posted: Mon Apr 26, 2021 1:23 pm
by Per K Grok
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.
Re: Player Name With Toggled Buttons Help!
Posted: Tue Apr 27, 2021 5:59 am
by birdtrappedinacage
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.