Get the name of the showing image ?

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
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Get the name of the showing image ?

#1 Post by Black Cat 2412 »

Good day ladies and gentlemen.

I wonder if I can get the name of a particular image being shown onscreen return to me? Say I have:

Code: Select all

label start:
  scene bg1
  show char1 normal at left
  show char2 smile
So currently I'm having 3 images displaying. I want renpy to return to me, for example, the name of the image with the tag "char1" that is being shown onscreen and any attribute that come with it. So the result I want is a full name of "char1 normal" returned.

So far I have found these:

Code: Select all

renpy.get_showing_tags(layer='master') #Returns the set of image tags that are currently being shown on layer
# and
renpy.showing(name, layer='master') #Returns true if an image with the same tag as name is showing on layer
#and
renpy.check_image_attributes(tag, attributes) #Checks to see if there is a unique image with the given tag and attributes. If there is, returns the tag and attributes in order
What I want is a hybrid of these three: a function that return the full name of an image with a certain given tag that is being shown onscreen.
Is there anyway that I can achieve that? If there is, would you be so kind to actually write the code to show me how I can set them up? (since all of this function things are still quite vague to me)

Many thanks in advance

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Get the name of the showing image ?

#2 Post by RicharDann »

I sort of wrote a function that returns a list of image tags currently showing along with their attributes on a string, I don't know if it's what you need but it can be changed later. I tried to explain what it does step by step but if you need more help feel free to ask.

Code: Select all


image eileen happy = "eileen_happy.png"
init python:
    
    def get_showing_images(): #This is the function, it returns a list of strings containing names of images currently showing  
        
        img_list = [] #Create a local variable with an empty list, this will store all of the image names
        tags = renpy.get_showing_tags() #This checks each image with tag showing. It returns a python set.
        #As sets are not iterable (I think), we convert it to a list, then we iterate over it's contents
        for i in list(tags): 
            tag_name = i # We store the tag in a variable. 
            atrb = renpy.get_ordered_image_attributes(i) #This returns an ordered list of every attribute the image has
            for a in atrb: #For each attribute ...
                
                tag_name += ' ' + a #Include a space and then that attribute in the tag name
            img_list.append(tag_name) #Add the resultant string in tag_name to the image list
            
        return img_list #Finally, we return the list

default test = None #We create a test variable to store the list returned by the previous function

label start:
    
    "Let's show an image..."

    show eileen happy with dissolve #Show the image 
    
    $ test = get_showing_images() #We call the function here and store it's value

    "[test[0]] is showing." #Here I retrieve the first element in the list and show it as dialogue, to test.
    
The most important step is always the next one.

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Get the name of the showing image ?

#3 Post by Black Cat 2412 »

Thank you. But this function return the name of ALL the showing image, right? That's 50% of what I want it to do. How about when I want it to pick only 1 image with a certain tag?
Let me clarify myself better this time: I call the function, give it an image tag (ex: "char1") and the name of the layer it will be searching on, it takes the tag and search the layer to see if such an image with that tag is currently showing. If there is, it returns to me the full name of that one image ( so the result will be a string like "char1 pose1 smile"), if not it returns "None"
Hope that is much clearer : )

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Get the name of the showing image ?

#4 Post by RicharDann »

You can modify the function to suit your needs, or make another function where you use the previous one as a base, and then do any operation you need with it. For getting a particular image you would do something like this:

Code: Select all

init python:
    # I updated the function so it takes a layer parameter, so you can specify in what layer you want to search     
    def get_showing_images(layer='master'): 
        
        img_list = []
        tags = renpy.get_showing_tags(layer) #Now we use the layer specified above

        for i in list(tags): 
            tag_name = i
            atrb = renpy.get_ordered_image_attributes(i) 
            for a in atrb:
                
                tag_name += ' ' + a 
            img_list.append(tag_name) 
            
        return img_list 
    
    #New function get_image, it takes a tag and a layer parameter    
    def get_image(tag, layer='master'):
        
        images = get_showing_images(layer) #Call the previous function
        
        if tag in images: #Check if the tag is included in the list generated by get_showing_images
            return tag #Return the tag if it exists
        else:
            return None #If it doesn't, return None
        
default test = None 

label start:
    
    scene black
    
    show eileen happy with dissolve
    
    $ test = get_image('eileen happy', 'master')
    
    "[test]" # 'eileen happy'
                  
    $ test = get_image('mary happy', 'master')
    
    "[test]" # None
    
    return
Last edited by RicharDann on Tue Oct 17, 2017 10:55 am, edited 1 time in total.
The most important step is always the next one.

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Get the name of the showing image ?

#5 Post by Black Cat 2412 »

Hmm, that's strange: I copied - pasted the code, and every answer I get is "None" even if the image is showing.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Get the name of the showing image ?

#6 Post by RicharDann »

Black Cat 2412 wrote: Tue Oct 17, 2017 10:54 am Hmm, that's strange: I copied - pasted the code, and every answer I get is "None" even if the image is showing.
That's odd, I just tried it in a new project and it seems to work fine.

EDIT:

Wait, no, you're right, that seems to happen when the image has more than one attribute. Looks like we also need to check if the image is actually showing. This is likely the part that needs fixing:

Code: Select all

        for i in list(tags): 
            if renpy.showing(i, layer): # Adding this seems to fix it
                tag_name = i
                atrb = renpy.get_ordered_image_attributes(i) 
                for a in atrb:
                    tag_name += ' ' + a 
                img_list.append(tag_name) 
            
            return img_list
The most important step is always the next one.

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Get the name of the showing image ?

#7 Post by Black Cat 2412 »

It still shows "None" to me even with the new line added

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Get the name of the showing image ?

#8 Post by RicharDann »

I've been testing this for a while with different images and I've found that some images are properly added to the list but some are not. The problem seems to be that Ren'Py searches tags in the image file name as well as the variable name.

Unfortunately for this approach to work, the image file names must have underscores instead of spaces to separate words, or else Ren'Py will treat the spaces in their name as tags, adding them to the name generated by the function and thus making it different from the name you ask for with get_image(). I couldn't find a way to specify that I only want the variable names, as I can't access (and don't really want to mess with) built-in internal functions.

This is what I've got so far:

Code: Select all

 
image eileen pose1 smile = "eileen_pose_smile.png" #It works well with this image.
image mary pose1 smile = "mary pose smile.png" #With this one it doesn't because of different name
#For it to work, the filename must be the same as the image file, or alternatively, have no spaces

init python:

    def get_showing_images(layer='master'): 
        
        img_list = []
        tags = renpy.get_showing_tags(layer) 

        for i in list(tags):
            if renpy.showing(i, layer):
                tag_name = i
                atrb = renpy.get_ordered_image_attributes(tag_name) 
                for a in atrb: 
                    tag_name += ' ' + a
                img_list.append(tag_name) 
            
        return img_list 
    
    def get_image(tag, layer='master'):
        
        images = get_showing_images(layer) 
        
        if tag in images: 
            return tag 
        else:
            return None 
        
default test1 = None 
default test2 = None 

label start:
        
    show eileen pose1 smile with dissolve
    
    show mary pose1 smile at right with dissolve
    
    $ test1 = get_showing_images('master')
    
    "[test1]" #['eileen pose 1 smile', 'mary pose1 pose smile']
                  
    $ test2 = get_image('eileen pose1 smile', 'master')
    
    "[test2]" # 'eileen pose1 smile'
    
    $ test2 = get_image('mary pose1 smile', 'master')
    
    "[test2]" # None, because error explained above. If names match, this works.
 
    return
This is as far as I can go with this, if it still doens't help I hope that someone more experienced in Ren'Py's inner workings is able to offer a better solution.
The most important step is always the next one.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Get the name of the showing image ?

#9 Post by PyTom »

The new prerelease lets you combine renpy.get_showing_tags() and renpy.attributes() to get the image name.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Get the name of the showing image ?

#10 Post by Black Cat 2412 »

Yay, thank a lot ^^ That's awesome!
Just one thing: when I use it like this:

Code: Select all

default test = None 
label start:
    scene bg1 
    show c normal 
    $ test = renpy.get_attributes('c')
    c "[test]"
The result onscreen is:
(u'normal',)
What are the "u", the parentheses and the comma for? Why are they in the result? What can I do so that the result is only
c normal
(no quote mark, no comma, etc, just plain old name)?

User avatar
tacoen
Newbie
Posts: 13
Joined: Fri May 10, 2019 2:48 am
Contact:

Re: Get the name of the showing image ?

#11 Post by tacoen »

Code: Select all

	scene home bedroom
To file what file is "home bedroom", i use:

Code: Select all

	t = tuple(renpy.get_showing_tags('master',True))
	a = renpy.get_attributes(t[0])
	name = t[0] +" "+ a[0]
	renpy.get_registered_image(name).filename
That only works if the scene is image, not solid color, neither conditionalswitch:

To explore more, __dict__ may give you a clue.

Code: Select all

	renpy.get_registered_image(name).__dict__

Post Reply

Who is online

Users browsing this forum: No registered users