Multiple images in CG gallery?

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.
Message
Author
Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Multiple images in CG gallery?

#1 Post by Gumaster »

I'm using the code in the cookbook (the old cg gallery, I believe) and it mentions

Code: Select all

        # The first component may consist of:
        # - A string, which is taken to be an image filename.
        # - A displayable, which is shown to the user.
        # - A tuple, which is used to display multiple of the above.
        [b]# - A list, which is used to show things one after another.[/b]
with something like

Code: Select all

page1 = [
            ( "whitehouse.jpg", "bg whitehouse" ),
How do I make it show multiple images using a list? Regardless of what I try((), []), I just get various forms of concatenation error
(Also, how would unlocking a list of images work?)

(By mutliple images, I mean [Display an image, after user clicks, displays another image, after user clicks again, returns to gallery])

Blue Lemma
Forum Founder
Posts: 2005
Joined: Sat Jan 25, 2003 2:32 pm
Completed: ToL, Shoujo Attack!, Lemma Ten
Projects: [RETIRED FROM FORUM ADMINISTRATION - CONTACT PYTOM WITH ISSUES]
Contact:

Re: Multiple images in CG gallery?

#2 Post by Blue Lemma »

Something like

Code: Select all

page1 = [
            ( "whitehouse.jpg", "bg whitehouse" ),
            ( "blackhouse.jpg", "bg blackhouse" ),
            ( "fuschiahouse.jpg", "bg fuschiahouse") ]
doesn't work?

The gallery code I see should have something like:

Code: Select all

        g.button("thumbnail1.png")
        g.display("cg1a")
        g.display("cg1b")
        g.display("cg1c")
or something of the like, where g is a Gallery.
“Among those who dislike oppression are many who like to oppress.”
- Napoleon Bonaparte


I've retired from forum administration. I do not add people to the "adult" group, deactivate accounts, nor any other administrative task. Please direct admin/mod issues to PyTom or the other mods : )

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#3 Post by Gumaster »

I'm using the old gallery code here since the new one doesnt want to work for me. Doing it that way

Code: Select all

page1 = [
            ( "whitehouse.jpg", "bg whitehouse" ),
            ( "blackhouse.jpg", "bg blackhouse" ),
            ( "fuschiahouse.jpg", "bg fuschiahouse") ]
shows 3 seperate thumbnails in the gallery (so I click on the thumbnail for whitehouse.jpg to display whitehouse.jpg, or I click on the thumbnail for blackhouse.jpg to display blackhouse.jpg)
What I'm going for is some way to group variations of one cg together, so that they only have one thumbnail in the gallery, which when clicked then displays all of the variations of that cg (So let's say I have a thumbnail of whitehouse.jpg, clicking it will display whitehouse.jpg, then clicking again will display blackhouse.jpg instead of returning to the menu, and so forth)

Blue Lemma
Forum Founder
Posts: 2005
Joined: Sat Jan 25, 2003 2:32 pm
Completed: ToL, Shoujo Attack!, Lemma Ten
Projects: [RETIRED FROM FORUM ADMINISTRATION - CONTACT PYTOM WITH ISSUES]
Contact:

Re: Multiple images in CG gallery?

#4 Post by Blue Lemma »

Why not use the new gallery code with the g.button, g.display, etc? I've seen it work like I pasted in the 2nd snippet. :wink:
“Among those who dislike oppression are many who like to oppress.”
- Napoleon Bonaparte


I've retired from forum administration. I do not add people to the "adult" group, deactivate accounts, nor any other administrative task. Please direct admin/mod issues to PyTom or the other mods : )

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#5 Post by Gumaster »

Couldn't get it to work right (Might've been because I switched to screens recently?)
Isn't the old one simpler, anyway? It seems like it would be doable by giving it a list, but I'm not sure what the correct syntax for a list is and the ones I tried ([], ()) didn't work

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Multiple images in CG gallery?

#6 Post by 0ion9 »

Gumaster wrote:Couldn't get it to work right (Might've been because I switched to screens recently?)
Isn't the old one simpler, anyway? It seems like it would be doable by giving it a list, but I'm not sure what the correct syntax for a list is and the ones I tried ([], ()) didn't work
http://docs.python.org/tutorial/introduction.html#lists

in short: lists are delimited with [], tuples with (). Your latest post containing code defines 'page1' as a list of tuples (each of which happens to contain 2 items)

If you read the source:

http://www.renpy.org/wiki/renpy/doc/cookbook/CG_Gallery

Code: Select all

                if cmd == "show":

                    if not isinstance(arg, list):
                        arg = [ arg ]

                    for i in arg:
                        ui.add(Solid((0, 0, 0, 255)))
                        show_things(i)
                        ui.saybehavior()
                        renpy.transition(gallery_transition)
                        ui.interact(suppress_overlay=True, suppress_underlay=True)
You can see that the second item of the tuple is either a single image id,
as in

('washington.jpg', 'bg washington')

, or a list of image ids, as in

('washington.jpg', ['bg washington', 'bg washington2','bg washington burnt'])

This is certainly how the implementation is intended to work, it's clear from the code. If it doesn't, then there is a bug.

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#7 Post by Gumaster »

0ion9 wrote: in short: lists are delimited with [], tuples with (). Your latest post containing code defines 'page1' as a list of tuples (each of which happens to contain 2 items)

If you read the source:

http://www.renpy.org/wiki/renpy/doc/cookbook/CG_Gallery

Code: Select all

                if cmd == "show":

                    if not isinstance(arg, list):
                        arg = [ arg ]

                    for i in arg:
                        ui.add(Solid((0, 0, 0, 255)))
                        show_things(i)
                        ui.saybehavior()
                        renpy.transition(gallery_transition)
                        ui.interact(suppress_overlay=True, suppress_underlay=True)
You can see that the second item of the tuple is either a single image id,
as in

('washington.jpg', 'bg washington')

, or a list of image ids, as in

('washington.jpg', ['bg washington', 'bg washington2','bg washington burnt'])

This is certainly how the implementation is intended to work, it's clear from the code. If it doesn't, then there is a bug.
Actually, isn't the first item of the tuple the one that can be a single image id or a list of image ids? (The second item of the tuple is the name that unlocks the image)
# The first component may consist of:
# - A string, which is taken to be an image filename.
# - A displayable, which is shown to the user.
# - A tuple, which is used to display multiple of the above.
# - A list, which is used to show things one after another.
Either way, no matter what I try

Code: Select all

        page1 = [
            ( ["Event/hospital_0.jpg", "Event/hospital_1.jpg"], "cg hospital_0" ),
        ]
(tried using ' instead of " too)
I get

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/cggallery.rpy", line 319, in script
        $ gallery()
  File "game/cggallery.rpy", line 316, in python
    
  File "game/cggallery.rpy", line 276, in python
                            if renpy.loadable("thumbnail_" + filename):
TypeError: cannot concatenate 'str' and 'RevertableList' objects

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "C:\Users\Dropbox\EF\renpy-6.12.1\renpy\execution.py", line 261, in run
  File "C:\Users\Dropbox\EF\renpy-6.12.1\renpy\ast.py", line 630, in execute
  File "C:\Users\Dropbox\EF\renpy-6.12.1\renpy\python.py", line 974, in py_exec_bytecode
  File "game/cggallery.rpy", line 316, in <module>
  File "game/cggallery.rpy", line 276, in gallery
TypeError: cannot concatenate 'str' and 'RevertableList' objects

Windows-post2008Server-6.1.7600
Ren'Py 6.12.1.1520

---EDIT----

Okay, problem seems to be with getting the filename from the list of images. Replacing the code with

Code: Select all

                        ui.add(im.Scale("Event/comet.png",
                                            gallery_width,
                                            gallery_height)
Makes everything work perfectly with the obvious exception of making every thumbnail that of Event/comet.png.
Last edited by Gumaster on Sun Jul 17, 2011 2:59 am, edited 1 time in total.

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Multiple images in CG gallery?

#8 Post by 0ion9 »

A traceback is worth a million 'codes' :)
Now I know exactly what it is. see this:

Code: Select all

# Otherwise, get the filename and spec and see if
                    # we've unlocked it.
                    if len(images[i]) == 2:
                        filename, spec = images[i]
                        toshow = filename
                    elif len(images[i]) == 3:
                        toshow, spec, filename = images[i]

                    if spec:
                        spec = spec.split()
                        
                    if spec and tuple(spec) not in persistent._seen_images:
                        filename = None
                        clicked = None
                    else:
                        clicked = ui.returns(('show', toshow))
I personally regard the design of this as somewhat warped. According to this code, what you want is not a 2-tuple but a 3-tuple
(to be exact: (imagelist, spec, thumbnail). what you are missing is the thumbnail filename as the final item.)
Last edited by 0ion9 on Sun Jul 17, 2011 3:44 am, edited 1 time in total.

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#9 Post by Gumaster »

Indeed, it works now if I specify a thumbnail.
I assume the problem was that while the code to display an image checked if it was a list or not, the code to create a thumbnail didn't? In that case, is it possible to write some code to do so? I'm not too familiar with python, so...
Off to create thumbnails for everything~
Thanks for the help, you were great

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Multiple images in CG gallery?

#10 Post by 0ion9 »

The problem is that the gallery code contradicts expectations!
you could have

(image, cond)

or

(images, cond, thumbnail)

But the latter seems to be undocumented, and naturally it's not obvious that in the first instance the image filename is used to generate a thumbnail filename, but in the latter instance the thumbnail must be separately named and placed as the last item in the tuple.

IMO it should be coded (and documented) so that

(thumbnail, cond, images) is the second possibility.
This would make it consistent with the first case (where the first item's name is also used to generate a thumbnail filename), and reduce confusion..

Let's see..

Code: Select all

# Otherwise, get the filename and spec and see if
                    # we've unlocked it.
                    if len(images[i]) == 2:
                        filename, spec = images[i]
                        toshow = filename
                    elif len(images[i]) == 3:
                        filename, spec, toshow = images[i]

                    if spec:
                        spec = spec.split()
                        
                    if spec and tuple(spec) not in persistent._seen_images:
                        filename = None
                        clicked = None
                    else:
                        clicked = ui.returns(('show', toshow))
^^^ modified version of those lines that does exactly what I said it should do.

Of course, the ideal design probably is
(cond, thumbimage, images)
With 'images' simply being optional.
The advantage of this is that it lets you define things in a somewhat english readable way:

If COND, Show THUMBIMAGE. When THUMBIMAGE is clicked, show either the IMAGES, if available, or the non thumbnail version of the THUMBIMAGE.

(the parameters above appear in the order they do in the sentence.

If you find that preferable, here's a version of those lines of code which implements it:

Code: Select all

# Otherwise, get the filename and spec and see if
                    # we've unlocked it.
                    spec, filename = images[i][:2]
                    if len(images[i]) == 3:
                        toshow = images[i][2]
                    else:
                        toshow = filename

                    if spec:
                        spec = spec.split()
                        
                    if spec and tuple(spec) not in persistent._seen_images:
                        filename = None
                        clicked = None
                    else:
                        clicked = ui.returns(('show', toshow))

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#11 Post by Gumaster »

Hmm, I'm not sure if it's even possible to do this but, is it possible to have different unlock conditions for various images?
For example,

Code: Select all

( ["Event/alice_sleeping.jpg", "Event/alice_sleeping_2.jpg"], ["cg alice_sleeping", "cg alice_sleeping_2"], "Thumbnails/thumbnail_alice_sleeping.jpg" ),
if I wanted the first image to be unlocked via the first condition and the second image to be unlocked via the second condition (pretend I'm just using a regular thumbnail regardless)
I might just have to find a....incredibly deviant and messy workaround for now

EDIT: Using an incredibly deviant and stupid workaround for now.
It works.
It works, but...
I feel so dirty.

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Multiple images in CG gallery?

#12 Post by 0ion9 »

Code: Select all

                    # Otherwise, get the filename and spec and see if
                    # we've unlocked it.
                    clicked = None
                    if len(images[i]) == 2:
                        filename, spec = images[i]
                        toshow = [filename]
                    elif len(images[i]) == 3:
                        toshow, spec, filename = images[i]
                    # we only multi-filter when spec is not a simple string
                    if spec and type(spec) != str:
                        # Produce an error if input data is obvious BS
                        if len(spec) != len(toshow):
                            raise ValueError ('number of conditions mismatches number of images to show')
                        # Now we need to filter out images whose COND fails.
                        old_toshow = toshow
                        toshow = []
                        for show,spec in zip (old_toshow, spec):                
                            if spec:
                                spec = spec.split()
                                if tuple(spec) not in persistent._seen_images:
                                    continue
                            toshow.append(show)
                        if len(toshow) == 0:
                            filename = None
                            clicked = None
                        else:
                            clicked = ui.returns(('show', toshow))
                    else:
                        if spec and tuple(spec.split()) not in persistent._seen_images:
                            filename = None
                            clicked = None
                        else:
                            clicked = ui.returns(('show', toshow))
Pretty much off the top of my head. Let me know how it goes.

EDIT: Fixed an initialization bug
Last edited by 0ion9 on Mon Jul 18, 2011 9:43 am, edited 1 time in total.

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#13 Post by Gumaster »

Well, it's rather...odd
It doesn't break, but it behaves rather oddly.

Code: Select all

            ( "Event/alice_changing.jpg", "cg alice_changing" ),
            ( ["Event/alice_sleeping.jpg", "Event/alice_sleeping_2.jpg"], ["cg alice_sleeping", "cg alice_sleeping_2"], "Thumbnails/thumbnail_alice_sleeping.jpg" ),
the thumbnail displays as it should, however it now displays "Event/alice_changing.jpg" when I click on the alice_sleeping thumbnail
If I clear persistent data, it displays the alice_sleeping thumbnail in gallery, even though I shouldn't have unlocked it yet. (However, it does nothing should I click on it)
If I then view either alice_sleeping cg, or both, nothing changes.
If I view alice_changing and nothing else, then the alice_sleeping thumbnail becomes active and displays alice_changing when clicked

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Multiple images in CG gallery?

#14 Post by 0ion9 »

I've made one bugfix above. I probably need to test this myself though. Here it's Monday night, I'll probably get to that Tuesday afternoon.

Gumaster
Regular
Posts: 93
Joined: Sun Sep 06, 2009 6:54 am
Contact:

Re: Multiple images in CG gallery?

#15 Post by Gumaster »

It works, it works well, and it is glorious. goddamnitisglorious
This next question isn't so important right now (since it's impossible in the sequence of game events to unlock the second cg without unlocking the first), but is there a way to determine the thumbnail used?
Something like,
if cg1 is unlocked, use thumbnail_cg1
if cg1 is locked and cg2 is unlocked, use thumbnail_cg2
if both are locked, use whatever's being used to show a locked image

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]