[Solved] Changing random mm backgound via preferences

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
recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

[Solved] Changing random mm backgound via preferences

#1 Post by recreation »

I'm using random images for my Main Menu background:

Code: Select all

default mm = renpy.random.choice(["bla.webp", "blub.webp"])
When the player reaches the end I change that to a static image, but I want the player to have an option to go back to random images again, so once the variable for the ending is set, an option shows up in preferences:

Code: Select all

                if persistent.seenEnd:
                    vbox:
                        style_prefix "radio"
                        label _("Show Random Menu Background")
                        textbutton _("Yes") action SetVariable("persistent.randbg", True)
                        textbutton _("No") action SetVariable("persistent.randbg", False)
Now I'd like to make the background change once the "Yes" buttons is clicked without the player having to restart the game, but I'm a bit at a loss here. How can I do that?
Last edited by recreation on Fri Dec 03, 2021 1:16 am, edited 1 time in total.

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#2 Post by zmook »

This should do it.

Code: Select all

default persistent.seen_end = False
default persistent.force_rand_bg = False

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"

    $ rand_bg = renpy.random.choice(["001.jpg", "002.jpg"]) 
    if (persistent.seen_end and not persistent.force_rand_bg):
        add "003.jpg"
    else:
        add rand_bg
(I guess I used slightly different variable names. Sorry about that.)
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Changing random mm backgound via preferences

#3 Post by rayminator »

you want to use conditional switch
https://www.renpy.org/doc/html/displaya ... tionSwitch

this is used for textbox but you can change it background for the main menu

Code: Select all

default show_window = True
background ConditionSwitch('not show_window', Image("gui/textbox1.png", xalign=0.5, yalign=1.0),
                            'True', Image("gui/textbox2.png", xalign=0.5, yalign=1.0))

label start:
     $ show_window = True
     or
     $ show_window = False

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#4 Post by zmook »

rayminator wrote: Tue Nov 30, 2021 8:13 pm you want to use conditional switch
Is there a practical difference between ConditionSwitch and using an explicit `if..else` in a screen? I see ConditionSwitch has some control over whether both branches are predicted or not, and depending how complicated the conditions or the alternate displayables are, there may be some readability advantages one way or the other.

Since it's a self-contained Displayable a ConditionSwitch can be shown as an image, outside of a screen block. But within a screen, using an explicit `if..else` seems like it will usually be easier and more readable, no?
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

Re: Changing random mm backgound via preferences

#5 Post by recreation »

Thanks both of you, but the problem isn't showing or switching the images, both work fine. The problem is that when switched back from static to random image, it doesn't get picked randomly, for that to work the game has to restart.

@zmook opposed to if/else, ConditionSwitch is explicitely meant to be used with displayables, it won't work without anything else. The biggest advantage for me is that you can shorten the amount of code a good bit when you have several images with conditions.
Imagine this one using if/else instead:

Code: Select all

image d10ahd44= ConditionSwitch(
    "of10 and not of11 and not of12", "/jpg/d10ahd44_1.jpg",
    "of10 == 2", "/jpg/d10ahd44_1.jpg",
    "of11 and not of10 and not of12", "/jpg/d10ahd44_2.jpg",
    "of12 and not of11 and not of10", "/jpg/d10ahd44_3.jpg",
    "of10 and of11 and not of12", "/jpg/d10ahd44_[edress1a2].jpg",
    "of10 and not of11 and of12", "/jpg/d10ahd44_[edress1a3].jpg",
    "of11 and not of10 and of12", "/jpg/d10ahd44_[edress2a3].jpg",
    "of10 and of11 and of12", "/jpg/d10ahd44_[edressall].jpg",
    "True", "/jpg/d10ahd44.jpg")

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Changing random mm backgound via preferences

#6 Post by Ocelot »

What exactly happens?
Image is not switched at all? It is switched to some other predictable image? Something else?

Also, show screen code responsible for displaying images. There is probably something wrong there, but I cannot tell without seeing it.
< < insert Rick Cook quote here > >

recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

Re: Changing random mm backgound via preferences

#7 Post by recreation »

Ocelot wrote: Wed Dec 01, 2021 6:56 pm What exactly happens?
Image is not switched at all? It is switched to some other predictable image? Something else?

Also, show screen code responsible for displaying images. There is probably something wrong there, but I cannot tell without seeing it.
It switches to the last shown (random) image.
Essentially what happens is:
- Start the game without having it completed > random image
- complete the game > switches to static image > option to switch back becomes available
up to this point everything works as intended
- user switches back to random image > it shows the last random image shown before completing the game < at this point I want it to not show the last random image, but a new random image from the same batch (once the preferences screen is closed).

I suspect the screen code won't help much, but here it is:

Code: Select all

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"

    if persistent.seenEnd and not persistent.rbg:
        add "00"
    else:
        add mm
    add "mmbg"
The rest is unchanged.
"00" is the static image and "mm" is the random image (see OP).

For completion:
I use the same images in the splashscreen which makes the whole thing a bit more complicated:

Code: Select all

label splashscreen:
    *snip*
    if persistent.seenEnd and not persistent.rbg:
        scene 00 with slowdissolve
    else:
        scene expression mm with slowdissolve
    show mmbg with dissolve
    return

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Changing random mm backgound via preferences

#8 Post by Ocelot »

Python and RenPy (mostly) are imperative languages. That means that all instruction are executed as soon as they are encountered. They are not saved to be executed when they are requested.

Code: Select all

default mm = renpy.random.choice(["bla.webp", "blub.webp"])
That means "choose one of two strings and assign it to mm. When mm is requested always give that image back". It does not mean "save those two strings in mm and give one of them at random when mm is requested".
Once the choice is made (after init phase), it is done, and value of mm will not change during this session, unless you explicitely change it.

If you want to change mm after pressing button, you should do it there. For example:

Code: Select all

init python:
    def reroll_mm():
        store.mm = renpy.random.choice(["bla.webp", "blub.webp"]

# . . .

textbutton _("Yes") action [Function(reroll_mm), SetVariable("persistent.randbg", True)]
< < insert Rick Cook quote here > >

recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

Re: Changing random mm backgound via preferences

#9 Post by recreation »

Ocelot wrote: Wed Dec 01, 2021 8:23 pm Python and RenPy (mostly) are imperative languages. That means that all instruction are executed as soon as they are encountered. They are not saved to be executed when they are requested.

Code: Select all

default mm = renpy.random.choice(["bla.webp", "blub.webp"])
That means "choose one of two strings and assign it to mm. When mm is requested always give that image back". It does not mean "save those two strings in mm and give one of them at random when mm is requested".
Once the choice is made (after init phase), it is done, and value of mm will not change during this session, unless you explicitely change it.

If you want to change mm after pressing button, you should do it there. For example:

Code: Select all

init python:
    def reroll_mm():
        store.mm = renpy.random.choice(["bla.webp", "blub.webp"]

# . . .

textbutton _("Yes") action [Function(reroll_mm), SetVariable("persistent.randbg", True)]
Thanks, I kind of expected that tbh, but I didn't figure out a way how to "fix" it.
Your example is working nicely, thanks again.

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#10 Post by zmook »

recreation wrote: Wed Dec 01, 2021 6:34 pmImagine this one using if/else instead:
Ack. The software engineer in me says that if *that's* your problem, the answer isn't ConditionSwitch either. I'd hate to have to debug that if I misplaced a single "not".
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

Re: Changing random mm backgound via preferences

#11 Post by recreation »

zmook wrote: Wed Dec 01, 2021 11:53 pm
recreation wrote: Wed Dec 01, 2021 6:34 pmImagine this one using if/else instead:
Ack. The software engineer in me says that if *that's* your problem, the answer isn't ConditionSwitch either. I'd hate to have to debug that if I misplaced a single "not".
Good that I'm not a software engineer (nor a coder) xD
It took me a bit, especially since I have several of those, but when you know what each var does, it's not that hard^^

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#12 Post by zmook »

recreation wrote: Thu Dec 02, 2021 12:59 am...when you know what each var does...
Word to the wise, your future self will eventually thank you for naming variables something more mnemonic. Just because you can remember what they all mean *now* doesn't mean you will still remember a year from now.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#13 Post by zmook »

May I suggest:

Code: Select all

python:
    if of10==2:
        img_filename = "/jpg/d10ahd44_2.jpg"
    else:
        img_filename = "/jpg/d10ahd44_%d_%d_%d" % (of10, of11, of12)

image d10ahd44 = img_filename
And then rename the set of images to match?
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Changing random mm backgound via preferences

#14 Post by zmook »

Or if you want a one-liner (though it might be a little harder to read), there's:

Code: Select all

image d10ahd44 = "/jpg/d10ahd44_2.jpg" if of10==2 else "/jpg/d10ahd44_%d_%d_%d.jpg" % (of10, of11, of12)
And if you want the variables to be evaluated when the screen is shown, instead of at init time, there's:

Code: Select all

show expression "/jpg/d10ahd44_2.jpg" if of10==2 else "/jpg/d10ahd44_%d_%d_%d.jpg" % (of10, of11, of12)
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

recreation
Regular
Posts: 52
Joined: Sun Jul 01, 2018 8:32 am
Contact:

Re: Changing random mm backgound via preferences

#15 Post by recreation »

zmook wrote: Thu Dec 02, 2021 11:53 am
recreation wrote: Thu Dec 02, 2021 12:59 am...when you know what each var does...
Word to the wise, your future self will eventually thank you for naming variables something more mnemonic. Just because you can remember what they all mean *now* doesn't mean you will still remember a year from now.
It's quite simple actually, "of" is short for "OutFit", there are 4 different outfits, the player can comment on them and the character commented on will wear a different outfit depending on that, nothing too fancy^^

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]