Questions about python 3d dictionary and hbox

In this forum we discuss the future of Ren'Py, both bug fixes and longer-term development. Pre-releases are announced and discussed here.
Post Reply
Message
Author
trey5498
Newbie
Posts: 1
Joined: Sun Mar 14, 2021 5:40 pm
Contact:

Questions about python 3d dictionary and hbox

#1 Post by trey5498 »

This is a compound question. I am working on a character introduction gallery. I have the base gallery working with the images showing up with a name label showing below. Once you click on the image, it will show the image larger and will close again once the larger image is click again. I want to add more information to the larger image.

Issues:
1) Issues with trying to format the image with the hbox correctly and display the text correctly to the right of the image
2) pulling the information from the python 3d Dictionary

Image Details:
- Created each image as a daz3d render of each character
- base size 1920 x 1080

Gallery Setup:

Code: Select all

screen gameGallery():
    tag menu

    use game_menu(_("Extras"), scroll="viewport"):
        style_prefix "imggallery"
        label _("Character Introductions")

        if Lightbox_image != "":
            $ lb_image = im.Scale("imagegallery/" + Lightbox_image + ".jpg", 800, 600)

            imagebutton:
                idle lb_image
                hover lb_image
                xalign 0.5
                yalign 0.5
                focus_mask True
                action SetVariable("Lightbox_image", "")
        else:
            frame:
                xpos 40
                ypos 20
                background None
                vpgrid:
                    cols 3
                    spacing 40

                    for q in imageGalleryList:
                        $ qimage = "imagegallery/" + q + ".jpg"
                        $ lb_image = im.Scale(qimage, 250, 150)

                        vbox:
                            spacing 10
                            imagebutton:
                                idle lb_image
                                hover lb_image
                                action SetVariable("Lightbox_image", q)

                            if q == "Michael":
                                $ chname = "Main Character (Default: " + q + ")"
                            else:
                                $ chname = q

                            text "Name: " + chname:
                                style "slot_name_text"
How is looks:
Gallery: https://imgur.com/drz1hup
Image clicked: https://imgur.com/iaBgYQf


Dictionary Setup:

Code: Select all

init python:
    chardesc = [{
         'Michael' : 'name',
        'age' : '23',
        'desc' : 'Main character who is ex-military intellegence officer who suffered past psychological trauma in the past'
    },
    {
        'Rachel' : 'name',
        'age' : '21',
        'desc' : 'Long time best friend who has a secret love for the main character'
    }]

Working Code that is not Working Correctly:

Code: Select all

if Lightbox_image != "":
	$ lb_image = im.Scale("imagegallery/" + Lightbox_image + ".jpg", 800, 600)
	
	hbox:
		imagebutton:
			idle lb_image
			hover lb_image
			xalign 0.5
			yalign 0.5
			focus_mask True
			action SetVariable("Lightbox_image", "")
		vbox:
			#Lightbox_image is a variable that holds the name of the chararcter
			text "Name: " + Lightbox_image:
				style "char_style"
			text "age: " + chardesc[[Lightbox_image]][age]:
				style "char_style"
			text "Description: " + chardesc[[Lightbox_image]][desc]:
				style "char_style"
The main issues is I can't nail down the proper scaling down of the image. Ultimately, I want the the image to be shown to span over menu as well, but I am new to renpy development. I am also having issues trying to nail down the python dictionary as well (I think I have the syntax wrong on the dictionary lookup using a variable for the character name). The Lightbox_image variable holds the name of the character.

IE: If you click on the first image, Lightbox_image will hold the name Michael. so it should have the larger image on the left with the details from the dictionary on the right. Can anyone assist? And since I am new to renpy, please don't hesitate to let me know if I am using the wrong syntax entirely.

Ultimately if there is a tool that assist with this to find the proper image size to use and preview what the hbox code should look like before implenenting, it would be magical!

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

Re: Questions about python 3d dictionary and hbox

#2 Post by zmook »

Okay, taking your second question first:

You have defined `chardesc` as a list of dictionaries, but your structure doesn't quite make sense. It looks like you want to store some data about each character, keyed by the character name, so each character will have a record that looks like this:

Code: Select all

    {
        'name' : 'Rachel',
        'age' : '21',
        'desc' : 'Long time best friend who has a secret love for the main character'
    }
Right now you have names stored so that each of the characters has an item keyed with the character's name and always the value 'name' -- that's backwards. You want to know the key so you can look up a value, not vice versa.

You can use a list of dictionaries, like you have currently, by numerical index or by looping over them, like so:

Code: Select all

for char in chardesc:
    text "name: " + char['name']
But looking at your code, you actually want to be able to look up a character's data from knowing their name, not from knowing that their record is, say, the third one in the chardesc list. So what you want is a dict of dicts, not a list of dicts. Like so:

Code: Select all

default chardesc = {
    'Michael' : {
        'name' : 'Michael',   # this entry is now redundant and you could probably remove it
        'age' : '23',
        'desc' : 'Main character who is ex-military intellegence officer who suffered past psychological trauma in the past'
    },
    'Rachel' : {
        'name' : 'Rachel',    # this entry is now redundant and you could probably remove it
        'age' : '21',
        'desc' : 'Long time best friend who has a secret love for the main character'
    }
}
Now you can pull data out of this structure as `chardesc['Michael']['age']` to get the value 23. Your screen code becomes this:

Code: Select all

			text "Name: " + Lightbox_image:
				style "char_style"
			text "age: " + chardesc[Lightbox_image][age]:
				style "char_style"
			text "Description: " + chardesc[Lightbox_image][desc]:
				style "char_style"
One more thing: showing in the default name 'Michael' when the player quite likely identifies with another name, is kind of clunky or immersion-breaking. There's a few ways to fix this, but one that probably doesn't require changing too much of your code is just to do this, at some point after the player has picked a name (I'm assuming it's stored as `mcname`):

Code: Select all

    imageGalleryList[0] = mcname                # assuming you have put the mc first in this list
    chardesc[mcname] = chardesc.pop['Michael']  # replace the 'Michael' entry with one for [mcname] 
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: Questions about python 3d dictionary and hbox

#3 Post by zmook »

Oh, also: you should post in "Questions and Announcements" instead of "Development of Ren'Py", so the people who answer questions will see it.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: No registered users