I'm hoping some of you guys with extensive python and/or ren'py experience can help me streamline some code I have.
So, I have this simple character page that you can call up at anytime by just clicking the "Characters" textbutton that is displayed during most of my VN. This is really the first real code I wrote in ren'py (almost two years ago) and it works okay, but I know there's likely a way to make it much more streamlined. It didn't matter that much though, until I just started adding features to it. What I need to do is give the player the ability to change the characters names at anytime, via this character Bio screen. I got some help from the other devs and I have a simple rename function added to each characters Bio page that works great. However, to do this new feature, I ended up having to add another label block and then duplicate that for each characters regular block. On top of that, because I am still new at this, I wasn't able to make some sort of dynamic variable function to reuse the same label block over and over again, just with different source variables.
The end result is to do this the way i want to and to be able to change the character's name, as well as their nicknames, would result in about ~600 lines of extra code. A year ago I would have said "Fuck it!" it works.. that's all that's important. But now i am trying to make my code better and it bothers me to see something I KNOW can most likely be reduced to a fraction of that if I could just figure out how to make the dynamic variables work.
Anyhow... to the code.
When a player clicks "Characters", it opens a screen built as a frame with the 16 Bio portraits (and their names below each portrait). That's straight forward and doesn't need to change.
Example...
When you click one of the portraits above, it closes the character screen and opens the individual Bio screen of that particular character. Here is how that code looks now, with the new rename code already done for the character's name but not for the nickname. This works fine and it's tested. It's just duplicated 16 times with different values for the names. Also, the reason I have a separate style for button_text is because I have colored the highlighted text in each Bio to match the color that the characters text is displayed in, during the various Say states (Say, Thinking, Whispering, etc) Each character has their own custom color to help players differentiate the dialogue on-screen.
Anyhow... I can do dynamic variables in simple code - like the name scripts many ren'py games use. But doing it for multiple variables (character name, nickname and the colors for each character) combined with making it work for an input field that a player can click on right in the Bio... well that's beyond me ATM.
Any suggestions of how I can turn the above 38 lines of code, which are repeated 16 times, into something less bloated and clunky?
So, I have this simple character page that you can call up at anytime by just clicking the "Characters" textbutton that is displayed during most of my VN. This is really the first real code I wrote in ren'py (almost two years ago) and it works okay, but I know there's likely a way to make it much more streamlined. It didn't matter that much though, until I just started adding features to it. What I need to do is give the player the ability to change the characters names at anytime, via this character Bio screen. I got some help from the other devs and I have a simple rename function added to each characters Bio page that works great. However, to do this new feature, I ended up having to add another label block and then duplicate that for each characters regular block. On top of that, because I am still new at this, I wasn't able to make some sort of dynamic variable function to reuse the same label block over and over again, just with different source variables.
The end result is to do this the way i want to and to be able to change the character's name, as well as their nicknames, would result in about ~600 lines of extra code. A year ago I would have said "Fuck it!" it works.. that's all that's important. But now i am trying to make my code better and it bothers me to see something I KNOW can most likely be reduced to a fraction of that if I could just figure out how to make the dynamic variables work.
Anyhow... to the code.
When a player clicks "Characters", it opens a screen built as a frame with the 16 Bio portraits (and their names below each portrait). That's straight forward and doesn't need to change.
Example...
Python:
screen bios_UI():
style_prefix "bioUI"
frame:
modal True
xalign 0.5
yalign 0.5
xpadding 20
ypadding 20
hbox:
spacing 10
vbox:
imagebutton:
idle bio1_pic + "_idle"
hover bio1_pic + "_hover"
action [ToggleScreen( "bios_UI" ), ToggleScreen( "bio1_screen" )]
text "[bio1_stat2]"
Python:
screen bio1_screen():
style_prefix "BioStyle"
frame:
modal True
right_padding 40
top_padding 20
xalign 0.5
yalign 0.5
hbox:
vbox:
image "bio_[bio1_stat1]"
vbox:
textbutton "[bio1_stat2]" style_prefix "Bio01" ysize 35 ypadding 10 xcenter 0.5 action [ToggleScreen( "bio1_screen" ), ToggleScreen( "Bio01_input" )]
text "([bio1_stat4])\n" size 20 xalign 0.5
text "Age: [bio1_stat3]" size 20 xalign 0.0
text "Nickname: “{color=#ff0033}" + str(mc_nic) + "{/color}”\n" size 20 xalign 0.0
text "[bio1_stat5]" size 20 xalign 0.0
text "(click the name to change it)" size 20 xalign 0.5
textbutton "{size=40}CLOSE{/size}" xcenter 0.5 ycenter 1.0 style_prefix "bioUI" action ToggleScreen( "bio1_screen" )
screen Bio01_input():
frame:
modal True
padding (30, 20)
xalign 0.5
yalign 0.5
hbox:
vbox:
text "Enter your new name and then click OK."
input default "[mc_name]":
color "#ffff00"
length 32
value VariableInputValue("mc_name")
key "K_RETURN" action NullAction()
textbutton "OK" style_prefix "inputUI" action [ToggleScreen( "Bio01_input" ), Call( "set_bios" )]
style Bio02_button_text:
size 40
font "BibleScript-Regular.ttf"
idle_color "#fa07e0"
hover_color "#ffff00"
Anyhow... I can do dynamic variables in simple code - like the name scripts many ren'py games use. But doing it for multiple variables (character name, nickname and the colors for each character) combined with making it work for an input field that a player can click on right in the Bio... well that's beyond me ATM.
Any suggestions of how I can turn the above 38 lines of code, which are repeated 16 times, into something less bloated and clunky?