I want to replace the directory of an image of the same name, depending on the player's choices. [SOLVED]

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
User avatar
MaySilva
Newbie
Posts: 4
Joined: Wed Jul 01, 2020 3:21 pm
Contact:

I want to replace the directory of an image of the same name, depending on the player's choices. [SOLVED]

#1 Post by MaySilva »

First of all, forgiveness for my English :oops:

My visual novel will have a protagonist with different looks: red hair and black hair (there are other combinations, but for simplicity, let's just leave these).

My visual novel also has an image gallery.

I want the unlocked image to be of the hair chosen by the player. However, because they are two identical images (only with changed hair color) I want only one to exist in the game and go to the gallery.

I'll explain it better by code:

Code: Select all

if blackhair == True:
	image cg1 = "cg1black.jpg"
else:
	image cg1 = "cg1red.jpg"
I'm sorry if it was stupid, but I tried to do it like that :oops: So I learned that the image will always be the last in the code, as if it ignored the '' if else '' and only replaced the cg1 image with the last one that appeared.

This is my gallery code:

Code: Select all

init python:

    def MaxScale(img, minwidth=config.screen_width, minheight=config.screen_height):
        currwidth, currheight = renpy.image_size(img)
        xscale = float(minwidth) / currwidth
        yscale = float(minheight) / currheight

        if xscale > yscale:
            maxscale = xscale
        else:
            maxscale = yscale

        return im.FactorScale(img, maxscale, maxscale)

    def MinScale(img, maxwidth=config.screen_width, maxheight=config.screen_height):
        currwidth, currheight = renpy.image_size(img)
        xscale = float(maxwidth) / currwidth
        yscale = float(maxheight) / currheight

        if xscale < yscale:
            minscale = xscale
        else:
            minscale = yscale

        return im.FactorScale(img, minscale, minscale)

    maxnumx = 2
    maxnumy = 2
    maxthumbx = config.screen_width / (maxnumx + 1)
    maxthumby = config.screen_height / (maxnumy + 1)
    maxperpage = maxnumx * maxnumy
    gallery_page = 0
    closeup_page = 0


    class GalleryItem:
        def __init__(self, name, images, thumb, locked="lock"):
            self.name = name
            self.images = images
            self.thumb = thumb
            self.locked = locked
            self.refresh_lock()

        def num_images(self):
            return len(self.images)

        def refresh_lock(self):
            self.num_unlocked = 0
            lockme = False
            for img in self.images:
                if not renpy.seen_image(img):
                    lockme = True
                else:
                    self.num_unlocked += 1
            self.is_locked = lockme


    gallery_items = []
    gallery_items.append(GalleryItem("Image 1", ["cg1"], "thumb1"))
The images are called by the name given in Ren'py, so I wanted to substitute that of creating several images within the game, with several different names and messing up the gallery.

I hope you understand, I hope it is possible and thank you for your help! :D
Last edited by MaySilva on Sun Jul 05, 2020 11:51 pm, edited 1 time in total.

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: I want to replace the directory of an image of the same name, depending on the player's choices.

#2 Post by drKlauz »

Try ConditionSwitch maybe?

Code: Select all

image cg1=ConditionSwitch(
  "blackhair","cg1black.jpg",
  "True","cg1red.jpg",
  )
https://www.renpy.org/doc/html/displaya ... tionSwitch
As for how it will work with gallery... As long as you use default blackfair=True to define variable it probably should work.
P.S.: If you want gallery use last selected hair color, you may want to use persistent.blackhair instead.
https://www.renpy.org/doc/html/persistent.html
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
MaySilva
Newbie
Posts: 4
Joined: Wed Jul 01, 2020 3:21 pm
Contact:

Re: I want to replace the directory of an image of the same name, depending on the player's choices.

#3 Post by MaySilva »

drKlauz wrote: Thu Jul 02, 2020 2:18 pm Try ConditionSwitch maybe?

Code: Select all

image cg1=ConditionSwitch(
  "blackhair","cg1black.jpg",
  "True","cg1red.jpg",
  )
https://www.renpy.org/doc/html/displaya ... tionSwitch
As for how it will work with gallery... As long as you use default blackfair=True to define variable it probably should work.
P.S.: If you want gallery use last selected hair color, you may want to use persistent.blackhair instead.
https://www.renpy.org/doc/html/persistent.html
Thank you! About your answer, I'm trying here, but the code only works with two combinations, right? I used two combinations to make the example easier, but actually my game has nine.

For that, what do you recommend?

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: I want to replace the directory of an image of the same name, depending on the player's choices.

#4 Post by drKlauz »

ConditionSwitch allow you to put more than two options, it is
condition, image,
condition, image,
condition, image,
"True", image,
Combining with Composite and general ATL you can make quite impressive (animated) images.
https://www.renpy.org/doc/html/displaya ... #Composite

If you really need more complex things, then it may make sense to check Custom Displayables/CDD/UDD. While it is more complex than usual RenPy screens/atl it allow very complex mechanics/features/visuals.
https://www.renpy.org/doc/html/udd.html
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
MaySilva
Newbie
Posts: 4
Joined: Wed Jul 01, 2020 3:21 pm
Contact:

Re: I want to replace the directory of an image of the same name, depending on the player's choices.

#5 Post by MaySilva »

I think this will work yes! Can you tell me if the variable for this function can be accompanied by another one?
Example;

If blackhair == True and purpleeyes == True, cg1blackpurple.jpg

User avatar
MaySilva
Newbie
Posts: 4
Joined: Wed Jul 01, 2020 3:21 pm
Contact:

Re: I want to replace the directory of an image of the same name, depending on the player's choices.

#6 Post by MaySilva »

My God, it worked! Thank yoooou! <3

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]