how do I change the opacity of the dialogue box in Ren’Py

This information is valid for Ren’Py 7.3.x. I believe it should work all the way back to Ren’Py 7.x. The names and values I use for the variables do not need to be yours.

This tutorial’s going to reference the last one a bit. We’re going to use the Preferences screen with persistent variables again. Only this time, we’re playing with a slider and transforms. The goal is for the player to adjust how opaque they want the dialogue background (but not the dialogue), because it can increase readability in different circumstances.

Open up your script editor and define the persistent variable, probably in options.rpy or wherever you have other persistent variables stored.

Screencap of code, defined below

Below’s where I put the code, in between the stock gui.about variable and the game’s short name:

define gui.about = _p("""
""")

# additional persistent variables begin here
define persistent.dialogueBoxOpacity = 1.0
# end extra variables

## A short name for the game used for executables and directories in the built
## distribution. This must be ASCII-only, and must not contain spaces, colons,
## or semicolons.

We’ve named the persistent variable dialogueBoxOpacity, but you can name it whatever you want. We set the default value to 1 because that means completely opaque.

Next, we figure out where on the Preferences screen you want to add the slider that will control the opacity. Your Preferences screen is probably defined pretty far down in the screens.rpy file, in the screen preferences() function.

screen shot- code is below

In this case I’ll put it in a new column (vbox). Here’s the code for the menu setting:

label _("Dialogue box opacity")
bar value FieldValue(persistent, "dialogueBoxOpacity", range=1.0, style="slider")

The label value is what you want the user to see headingwise. The bar is a Ren’Py object that is used when you want more fine-grained control of your variables.

In the parentheses, persistent means we’re setting the value of a persistent variable. The next string should be the same as the persistent variable name you used above.

The range=1.0 statement defines the max value of the bar, which is something we can set to correspond to the values we want possible for the opacity setting. All bars have a minimum value of 0.

That wraps up our work in the preferences screen part of the file.

Now, this is a bit more complicated than the style refresh we used to change the background of the dialogue window. Since there isn’t a predefined style for the opacity, I don’t know how to just inject that.

Instead, I put a transform for opacity in the Say screen. Every time this window gets rendered, it will check the opacity of the dialogue box and render that before it renders any text (so the text is always opaque).

The Say screen is traditionally defined about a hundred lines in to the stock screens.rpy file.

Under id “window”, we’ll add the following line:

window background Transform(Frame("gui/textbox.png",xalign=0.5, yalign=1.0), alpha=persistent.dialogueBoxOpacity)

window background Transform, uh transforms the window background. Frame is used to define a window that has a background. We need to specify the background being rendered, which is traditionally saved as textbox.png in the gui folder.

xalign=0.5, yalign=1.0 puts the window at the bottom of the screen, centered. You may have different settings in your custom GUI, in which case you should change these values to whatever you have defined in your style window: block.

alpha=persistent.dialogueBoxOpacity applies an alpha transform to the window. Here, the code consults the persistent variable to determine how opaque your image will be.

You should be good to go. Any questions or things I should clear up?

Further notes

As it is, this code allows you to use only one background. You can have the name of the background be a persistent variable too, but I’m assuming this is not a common use case. If you want to do it, leave a comment.

how do I use different color schemes for my dialogue boxes in Ren’Py

This information is valid for Ren’Py 7.3.x. I believe it should work all the way back to Ren’Py 7.x. The names and values I use for the variables do not need to be yours.

Some readers prefer reading light text on a dark background. Some prefer the opposite. You can make this a user setting, or Preference, in your Ren’Py game.

Ren’Py gives you several baked in Preferences in the default project that you can use, or not. There are a heck of a lot more that are native to Ren’Py. However, they don’t cover everything, and you may want to make some of your own.

Preferences are a kind of persistent variable. A persistent variable is a game setting that stays constant when a player exits a game, and even stays the same across playthroughs. To make your custom settings, you’ll need to define and set a persistent variable.

Open up your scripting files in your code editor of choice.

options file

Here’s a “blank” game created by Ren’Py 7.2.3. I’ve opened the options.rpy file. You can put persistent variables many places in your scripts. You can make a file just for them! In this case, though, I put my custom persistent variables in the options file because it makes sense to me.

# additional persistent variables begin here
define persistent.colorscheme = "lightondark"
# end extra variables

The comments (# lines) are for my own convenience so I can locate the preferences in my files later.

With this statement I’ve added a persistent variable called colorscheme and given it the default value of lightondark (with quotes, so the computer reads it as a text string). In order to make a variable persistent, you need to have “persistent.” in front of it.

Next, we want to add the option to our Preferences screen. Open up screens.rpy and find the screen preferences() function.

This is an example of the default Preferences screen. It makes a layout with vboxes and hboxes. You may have already changed some code in here, and that is fine.

n.b. I have word wrap enabled in my editor. Make sure you put what’s in line 874 all on one line. Same with line 875.

Figure out where you want to position your new option. In this case I decided to stick the custom preference in a new column.

style_prefix radio

style_prefix "radio"
label _("Dialogue box")
textbutton _("Light on Dark") action SetVariable("style.say_dialogue.color","#fff"), SetVariable("style.window.background", "gui/textbox.png"), SetVariable('persistent.colorscheme','lightondark'), style.rebuild
textbutton _("Dark on Light") action SetVariable("style.say_dialogue.color","#000"), SetVariable("style.window.background", "gui/game_menu.png"), SetVariable('persistent.colorscheme', 'darkonlight'), style.rebuild

radio means the user can only select one of the options. The label’s what you want the preference to be called. Here we add two options which display as Light on Dark and Dark on Light.

action tells Ren’Py to do something when that text is clicked. On these lines we’re telling Ren’Py to set some variables and then refresh the look and feel.

SetVariable("style.say_dialogue.color","#fff"),

This will make the text color in the dialogue box white. You can sub in any hex color for #fff.

SetVariable("style.window.background", "gui/textbox.png")

Surprisingly window.background only seems to apply to the dialogue window in a blank project. If you have other windows defined in your UI, though, this line will change the background for them too. Anyway, we’re setting the dialogue box background to be the textbox.png file stored in the gui folder. It’s a dark image, but you can use whatever you want. I don’t think you can define a color for window.background.

SetVariable('persistent.colorscheme','lightondark')

Here’s the variable we defined in options.rpy. Note that this is the same value, lightondark, as the default. That means the Preferences screen will render this as selected.

style.rebuild

We want to make Ren’py refresh the colors/images now according to what the user has just set.

The next line of code lets you define an additional possible background and text color for your dialogue box.

Further notes

You do not need to use these two color schemes. You can have as many as you like. You can also change the font itself, or spacing. However, the more tweaks you want to make, the more you may want to define your own style.

I didn’t suggest defining a style above because we’re just trying to get things to work. That’s something for another day.