Ren'Py [Solved]Functions and variables

GoldenD

Member
Sep 30, 2018
102
70
Hi guys,
another trouble with the following code.
The first idea was to use a variable to display one picture : that's allright.
But now I have to resize the picture because container or displayable does'nt respect the original size. And there, the scale function doesn't understand my variable ?

Code:
    .../...
    define idSpeaker = "myPicture"
    .../...

    vbox:
        yalign 1.0

        window:
            style "vbox_window"
            text what id "what"

            # THIS CODE IS OK, ADD READ VARIABLE idSpeaker

            # add "images/characters/[idSpeaker].jpg" xalign 0.0 yalign 0.5


            # BAD : FactorScale doesn't translate variable

            add im.FactorScale("images/characters/[idSpeaker].jpg",87,113) xalign 0.0 yalign 0.5
            # Couldn't find file 'images/characters/[idSpeaker].jpg'
Of course, with FactorScale, if i replace [idSpeaker] by myPicture, everything works fine .

An idea, a dream, a cup of tea ?
 
Last edited:

Porcus Dev

Engaged Member
Game Developer
Oct 12, 2017
2,582
4,692
I'm not sure, but you can try that:

Python:
    .../...
    define idSpeaker = "myPicture"
    .../...

    vbox:
        yalign 1.0

        window:
            style "vbox_window"
            text what id "what"

            add im.FactorScale("images/characters/%d.jpg" %idSpeaker,87,113) xalign 0.0 yalign 0.5
or this:
Python:
    .../...
    define idSpeaker = "myPicture"
    .../...

    vbox:
        yalign 1.0

        window:
            style "vbox_window"
            text what id "what"

            add im.FactorScale("images/characters/%d.jpg" % (idSpeaker) ,87,113) xalign 0.0 yalign 0.5
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,355
15,268
And there, the scale function doesn't understand my variable ?
It's normal, text interpolation is a feature of Ren'py, and can only works with Ren'py statements (and yet not all of them). It will never works with anything else, especially not with the parameters past to a function, like in this particular case.


You can define you image otherwise, then use the created displayable in place of the full path to the image :
Code:
image speaker = "images/characters/[idSpeaker].jpg"

screen whatever():
[...]
            # add speaker xalign 0.0 yalign 0.5
[...]
            add im.FactorScale( speaker ,87,113) xalign 0.0 yalign 0.5
Or you can use Python's text interpolation, like showed by mgomez0077 (Er... shouldn't it be %s instead of %d ?) , or like this :
Code:
            add im.FactorScale("images/characters/{}.jpg".format( idSpeaker ) ,87,113) xalign 0.0 yalign 0.5

All this said, you'll then discover that is absolutely not what you thought it was ; well, unless you effectively wanted your image to be 87 times wider and 113 times higher that the original, but I doubt of that.
What you need to use is , coupled to an add with a defined size :
Code:
            add Frame("images/characters/{}.jpg".format( idSpeaker ) ) size( 87, 113 ) xalign 0.0 yalign 0.5
 

GoldenD

Member
Sep 30, 2018
102
70
It's normal, text interpolation is a feature of Ren'py, and can only works with Ren'py statements (and yet not all of them). It will never works with anything else, especially not with the parameters past to a function, like in this particular case.


You can define you image otherwise, then use the created displayable in place of the full path to the image :
Code:
image speaker = "images/characters/[idSpeaker].jpg"

screen whatever():
[...]
            # add speaker xalign 0.0 yalign 0.5
[...]
            add im.FactorScale( speaker ,87,113) xalign 0.0 yalign 0.5
Or you can use Python's text interpolation, like showed by mgomez0077 (Er... shouldn't it be %s instead of %d ?) , or like this :
Code:
            add im.FactorScale("images/characters/{}.jpg".format( idSpeaker ) ,87,113) xalign 0.0 yalign 0.5

All this said, you'll then discover that is absolutely not what you thought it was ; well, unless you effectively wanted your image to be 87 times wider and 113 times higher that the original, but I doubt of that.
What you need to use is , coupled to an add with a defined size :
Code:
            add Frame("images/characters/{}.jpg".format( idSpeaker ) ) size( 87, 113 ) xalign 0.0 yalign 0.5

Hey guys,
what a team !
Ok for the last soluce with frame. The others using Python's text interpolation crash with "out of memory" but i notice the syntax which is different of what i tried and find on the net.

See you soon guys, and the subject will be : adding a key action...
 

Porcus Dev

Engaged Member
Game Developer
Oct 12, 2017
2,582
4,692
By the way, now that you've mentioned it, what's the difference between using %d or %s ? :unsure:
Thanks!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,355
15,268
By the way, now that you've mentioned it, what's the difference between using %d or %s ? :unsure:
Thanks!
Promise me to not hit your head too hard, because when you'll see it, it will become obvious : %s is for strings, while %d is for decimal values.
 
  • Haha
Reactions: Porcus Dev

Porcus Dev

Engaged Member
Game Developer
Oct 12, 2017
2,582
4,692
Ugh... I can't promise, lol :eek::ROFLMAO:

Well, now it makes sense, in my game I once used %d when dealing with numbers, now I understand.

Thanks ;)
 
  • Like
Reactions: anne O'nymous