[RESOLVED]renpy.exists : fails with class property

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
GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

[RESOLVED]renpy.exists : fails with class property

#1 Post by GoldenD »

Hola guys,
another quest.

Code: Select all


define PICTURES_CHARACTERS  = "pictures/characters/"
define  idSpeaker           = "player.jpg"

default lstTest             = []


#--------------------------------------------------------------------------------------------------
#                                           START
#--------------------------------------------------------------------------------------------------
label start:

    define _Speaker      = Character("", image = "[PICTURES_CHARACTERS][idSpeaker]")


    call loadTest

    $ _Speaker ( "test 01 : " + str(lstTest[1].picture) )

    show expression str(lstTest[1].picture) 					# NO PROBLEM : show find picture

    if renpy.exists( lstTest[1].picture ):					 	# PROBLEM : renpy.exists doesn't find picture
        $ _Speaker ( "test 02 : Image Find VARIABLE MODE" )
        
    else:
        $ _Speaker ( "test 02 : NOT Find In VARIABLE MODE" )

    if renpy.exists("pictures/characters/test02.jpg"):			# # BUT renpy.exists find picture if i give the same string hard coding
        $ _Speaker ( "test 03 : Image Find In FULL STRING MODE" )


    return 

label loadTest:


    $ lstTest = [
    ( testVariableScope( idEvent="test01")),
    ( testVariableScope( idEvent="test02")),
    ( testVariableScope( idEvent="test03"))
    ]

    return

Code: Select all

init python:

    class testVariableScope:

        def __init__(self, idEvent="", extPicture="jpg"):

            self.idEvent        = idEvent

        @property
        def picture(self):
            return "[PICTURES_CHARACTERS]" + self.idEvent.lower() + ".jpg"

And of course, if you like to play, you can compare the 2 strings , humanly equals, but digitaly differents.

Welcome in my world guys !!!!
Last edited by GoldenD on Wed Jun 02, 2021 10:31 am, edited 1 time in total.

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

Re: renpy.exists : fails with class property

#2 Post by Ocelot »

exists does not perform data interpolation and looks for the file string literally (after all, "[PICTURES_CHARACTERS][idSpeaker]" is a valid file name). You must perfort interpolation.

Images are a special case because if given just a string instead of proper displayable, when expected, it tries to guess, what you probably meant, and create corresponding displayable — Image, Solid, DynamicImage... Most of the time it guesses correctly, and if not, you just have to specify, what do you want yourself.

In short, handle string interpolation yourself.
< < insert Rick Cook quote here > >

GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

Re: renpy.exists : fails with class property

#3 Post by GoldenD »

Ocelot wrote: Tue Jun 01, 2021 1:37 pm In short, handle string interpolation yourself.
Yeah but how to ? Sorry but each time we talk about interpolation my blonde brain cries and burns.

I tried intermediate variable like:

Code: Select all

.../...
$ test = str(lstTest[1].picture)
.../...
if renpy.exists( test ):
...:...
# same result
or this

Code: Select all

.../...
$ test = "{}".format( lstTest[1].picture )
.../...
if renpy.exists( test ):
...:...
# same result
I miss something but what !?!

And thanks for ypur time guys !

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

Re: renpy.exists : fails with class property

#4 Post by Ocelot »

Whaen you write "[PICTURES_CHARACTERS][idSpeaker]", text in braces is a marker, that you want that text replaced with actual value of a variable, whose name is written within. Some RenPy facilities, most importantly Text and DynamicDisplayable know this and do the replacement. For everything else it is just text in square brackets. You must do the replacement yourself before passing the string to such functions.

For example, your picture property could look like:

Code: Select all

@property
def picture(self):
    return str(PICTURES_CHARACTERS) + self.idEvent.lower() + ".jpg"
This will return a string with all variables already interpolated and ready to be used.
< < insert Rick Cook quote here > >

GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

Re: renpy.exists : fails with class property

#5 Post by GoldenD »

Ocelot wrote: Tue Jun 01, 2021 3:32 pm For example, your picture property could look like:

Code: Select all

@property
def picture(self):
    return str(PICTURES_CHARACTERS) + self.idEvent.lower() + ".jpg"
This will return a string with all variables already interpolated and ready to be used.
Well try, but badly,

Code: Select all

str(PICTURES_CHARACTERS)
returns

Code: Select all

[u'pictures/characters/']
.

In fact, in my think, if renpy can interpolate, there is necessarily a way, a moment where the interpolate string is a real string. But I don't find the way to obtain this string (result of interpolation).

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

Re: renpy.exists : fails with class property

#6 Post by Ocelot »

If PICTURES_CHARACTERS is a list, you need to get element from it: str( PICTURES_CHARACTERS[0] ). Why is it a list though?
< < insert Rick Cook quote here > >

GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

Re: renpy.exists : fails with class property

#7 Post by GoldenD »

Ocelot wrote: Wed Jun 02, 2021 2:51 am If PICTURES_CHARACTERS is a list, you need to get element from it: str( PICTURES_CHARACTERS[0] ). Why is it a list though?
IT's not a list and it seems to be the source of problem. Thanks guy !! :D

Post Reply

Who is online

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