Page 1 of 1

[RESOLVED]renpy.exists : fails with class property

Posted: Tue Jun 01, 2021 1:11 pm
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 !!!!

Re: renpy.exists : fails with class property

Posted: Tue Jun 01, 2021 1:37 pm
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.

Re: renpy.exists : fails with class property

Posted: Tue Jun 01, 2021 3:15 pm
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 !

Re: renpy.exists : fails with class property

Posted: Tue Jun 01, 2021 3:32 pm
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.

Re: renpy.exists : fails with class property

Posted: Tue Jun 01, 2021 11:59 pm
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).

Re: renpy.exists : fails with class property

Posted: Wed Jun 02, 2021 2:51 am
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?

Re: renpy.exists : fails with class property

Posted: Wed Jun 02, 2021 10:31 am
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