Ren'Py Problem with variable resolution in lists and interpolation

Johor1

Newbie
Nov 9, 2018
55
21
OK, so I'm a newbie trying to develop my first Ren'Py game, and 90% of my time is spent debugging, but I'm almost literally hitting my head on the keyboard with stuff which previously worked but now doesn't when I add something new which (as far as I can see) doesn't affect what I've done already. I'm on version 7.3.5.606.

Interpolations suddenly stop working and the contents of lists aren't interpreted correctly. There's obviously something simple which I'm missing. In this example, I want a task to be defined in a location. I've reduced the number of locations to one for simplicity. Here's the relevant part of the code:

Code:
label start:

$ area_name = ["City"]

label generate_tasks:

$ ntasks = 2
$ itask = 0
$ tasklocationlist = []

label new_task:

# Choose task location
$ randomlocation = renpy.random.randint(0,len(area_name)-1)    # This returns: 0, obviously
$ tasklocation = area_name[randomlocation]                   
"Location [tasklocation]"                                                                   # This returns: Location City

#  (Various other task-related statements)
#   ...

if itask < ntasks:
    $ tasklocationlist.append(tasklocation)                    # This is supposed to add the location of this task to the list of locations
    "Task locations: [tasklocationlist]"                            # This returns: [u'City'] and then [u'City',u'City']
    $ newloc = tasklocationlist[itask]
    "Location [newloc]"                                                  # This returns: Location [newloc]
    $ itask += 1
    jump new_task
It returned the location before, so what's the problem? Any help would be much appreciated.
 

NoobGames

Newbie
Jun 1, 2019
55
115
I haven't used renpy before, but it seems to be storing the locations as (u "city"), I don't know what 'u' stands for, but it might be some kind of object type, search along the lines of "renpy converting u objects to strings.

I am not sure how helpful this is.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
I haven't used renpy before, but it seems to be storing the locations as (u "city"), I don't know what 'u' stands for, but it might be some kind of object type, search along the lines of "renpy converting u objects to strings.

I am not sure how helpful this is.
The 'u' prefix indicates that this is a Unicode string.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
So, the interpolation in the following works perfectly for me, as far as I can tell. (I'm using the same version of Ren'py as you.) Note the differences in indentation - maybe that has something to do with it

Code:
label start:

    $ area_name = ["City"]

label generate_tasks:

    $ ntasks = 2
    $ itask = 0
    $ tasklocationlist = []

label new_task:

    # Choose task location
    $ randomlocation = renpy.random.randint(0,len(area_name)-1)
    $ tasklocation = area_name[randomlocation]
    "Location [tasklocation]"

    #  (Various other task-related statements)
    #   ...

    if itask < ntasks:
        $ tasklocationlist.append(tasklocation) 
        "Task locations: [tasklocationlist]"
        $ newloc = tasklocationlist[itask]
        "Location [newloc]"
        $ itask += 1
        jump new_task

     return
 

Johor1

Newbie
Nov 9, 2018
55
21
Thanks, but indenting made no difference, neither did trying a different font. (Yes, that's the stage I'm at.)
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,976
16,231
Thanks, but indenting made no difference, neither did trying a different font. (Yes, that's the stage I'm at.)
Then the answer is probably here :
Code:
    #  (Various other task-related statements)
    #   ...
 

Johor1

Newbie
Nov 9, 2018
55
21
OK, found it. I had some lines from not completely implemented code which weren't all commented out and obviously messed up the interpolation. I still get the "u'City'" stuff, but it's interpolated correctly, so I can move on to coding the next bug now.

Thanks, AON.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,611
2,258
"u'City'" is perfectly normal. It's just the way RenPy chooses to show strings held within lists. The actual value is "City" stored as a Unicode string (because sometimes people want to write games in Chinese or using characters not normally part of ASCII).

If neither Unicode or ASCII mean anything to you... it just each character stored in computer memory. ASCII is usually what "plain text" files are on PC's. 255 characters defined (and redefined) well over 35 years ago to cover the basic alphabet, numbers and symbols commonly used by western culture. Unicode evolved as a somewhat unlimited character set which is constantly being expanded and includes pretty much every individual character you can possibly imagine (Chinese as one example, Emoji's as another).

So yeah, RenPy sometimes shows the u'...' to you, but usually obfuscates (hides) it. Don't worry about it, pretend it's not even there. Python is pretty good at comparing like for like, even when the programmer has written some really diabolic code.
 

Johor1

Newbie
Nov 9, 2018
55
21
Python is pretty good at comparing like for like, even when the programmer has written some really diabolic code.
That's what I thought, and what I'm hoping for. I'll probably write a lot of code which goes around the block to cross the road, but I've already found some good tips here, and it's just a hobby project anyway.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,976
16,231
If neither Unicode or ASCII mean anything to you... it just each character stored in computer memory. ASCII is usually what "plain text" files are on PC's. 255 characters defined (and redefined) well over 35 years ago to cover the basic alphabet, numbers and symbols commonly used by western culture.
Not exactly, the stop at 127. It cover the 26 letters of the Latin alphabet (upper and lower case), the 10 Arabic numerals, and the basic punctuation signs.
To this, have to be added 31 characters said "blank", that are control characters used in the old age of teletransmission to control the said transmission. Some of those control characters being still used to mark the end of each lines (one in Unix system, two in Windows). The others are still interpreted, but don't really have usage nowadays.
It's the that goes up to 255 characters. The 127 firsts being an exact copy of the ASCII table, and the 128 others being specific to the language(s) covered by this table.


Unicode evolved as a somewhat unlimited character set which is constantly being expanded and includes pretty much every individual character you can possibly imagine (Chinese as one example, Emoji's as another).
Since I talk technical, the Unicode table started more or less when Internet became effectively a world wide thing.
With people around the world not having the same charset (the table of characters, like the ASCII one or the ISO ones), some pages available on the web were unreadable. By example, the word "émotion" (french for "feeling") as wrote with the Western Europe ISO table, would look like "щmotion" with the Latin/Cyrillic ISO table. Even if someone understanding the language, wasn't in position to understand the text, because he constantly had to decipher it ; unless your OS also have the right charset and the HTML page correctly wrote, but it's a detail.
And obviously it was worse with charset that don't even include the ASCII table, like the Chinese one to keep your example. Therefore, it became clear that modern computers needed a global charset that would include all the characters, and so Unicode rose.


So yeah, RenPy sometimes shows the u'...' to you, but usually obfuscates (hides) it. Don't worry about it, pretend it's not even there.
More precisely it happen because Ren'py isn't asked to show the content of the string, but the content of the list. And what is stored in the list isn't the word "City", but an Unicode string containing the word "City".
From most of coders there's no real difference... until the day you pass hour trying to debug your code, before discovering that he was comparing Unicode with none Unicode. Python is cool with this, it let you know immediately is you ask him to show the string and not just its content.
But you're right, there's no need to bother with this.


Python is pretty good at comparing like for like, even when the programmer has written some really diabolic code.
Well, in fact it's thanks to Ren'py, because Python is pretty bad at this. It's Ren'py that silently convert all the strings into Unicode ones ; unless they are defined in an external Python file, but it's something to know, not something to bother with.
This happen from the same reason that the one I gave above to explain why Unicode exist. Ren'py being intended as game engine usable with any languages, and having a native translation feature, it's important to ensure that the text will be always readable ; this whatever the language of the author, the language of the translator, and the language of the player. And the only way to achieve this is to convert all texts into Unicode.