Ren'Py won't recognize array index variable as integer

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
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Ren'Py won't recognize array index variable as integer

#1 Post by Ayutac »

First off I'm sorry if this problem is easy to find with the search because it won't really work with me. (Fixing in progress.)

But now to the problem: The error looks like this:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/girlNavData.rpy", line 118, in script
TypeError: list indices must be integers, not unicode

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "/home/nait/Downloads/renpy-6.14.1-sdk/renpy/execution.py", line 266, in run
    node.execute()
  File "/home/nait/Downloads/renpy-6.14.1-sdk/renpy/ast.py", line 1222, in execute
    renpy.exports.say(None, "\n".join(narration), interact=False)
  File "/home/nait/Downloads/renpy-6.14.1-sdk/renpy/exports.py", line 750, in say
    who(what, interact=interact)
  File "/home/nait/Downloads/renpy-6.14.1-sdk/renpy/character.py", line 762, in __call__
    what = renpy.substitutions.substitute(what)
  File "/home/nait/Downloads/renpy-6.14.1-sdk/renpy/substitutions.py", line 218, in substitute
    s = formatter.vformat(s, (), kwargs)
  File "./install/lib/linux-x86_64/lib/python2.7/string.py", line 549, in vformat
  File "./install/lib/linux-x86_64/lib/python2.7/string.py", line 571, in _vformat
  File "./install/lib/linux-x86_64/lib/python2.7/string.py", line 640, in get_field
TypeError: list indices must be integers, not unicode

Linux-3.2.0-32-generic-x86_64-with-debian-wheezy-sid
Ren'Py 6.14.1.366
The code fragment is the following:

Code: Select all

init python:
    [...]
    girlsname = ["Misty", "Saphire", "Cynthia"]
    girlsnameS = [ # the genitive
        "'s", ## Misty
        "'s", ## Saphire
        "'s"] ## Cynthia
[...]
label meetMisty:
    $ girl = 0
    show misty normal at right
    jump meetGirl
[...]
label meetGirl:
    if girllist[girl].hasChanged() == True:
        "Congratulation, now you are [girlsname[girl]][girlnameS[girl]] [girllist[girl].MilestoneName]!"
    menu:
        "HP: [prota.HealthPoints]\nRelation: [girllist[girl].MilestoneName]\nExperience: [girllist[girl].Exp]"
        
        "speak":
            jump speakGirl
        [...]
girllist is a array with (self-created) girl-objects in it. The line in question is line 118, which is the line with "menu:". I strongly believe the error comes from the line after it, since it didn't appear after I removed the array-properties and since the choice in line 116 returns False for the moment, so line 117 is not entered.

I solved the problem by doing a trick literally just now:

Code: Select all

label meetGirl:
    $ GIQ = girllist[girl] ## short for "girl in question"
    $ GNIQ = girlsname[girl] + girlsnameS[girl] ## short for "girl's name in question"
    if girllist[girl].hasChanged() == True:
        "Congratulation, now you are [GNIQ] [GIQ.MilestoneName]!"
    menu:
        "HP: [prota.HealthPoints]\nRelation: [GIQ.MilestoneName]\nExperience: [GIQ.Exp]"
But I would really like to know what went wrong before. It seems the Character's say-Statement can't really use array properties or what?
Last edited by Ayutac on Fri Oct 19, 2012 3:59 pm, edited 1 time in total.
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: Ren'Py won't recognize array index variable as integer

#2 Post by jw2pfd »

I just toyed with some code and I hopefully can shed some light on your question. In the line of code:

Code: Select all

"HP: [prota.HealthPoints]\nRelation: [girllist[girl].MilestoneName]\nExperience: [girllist[girl].Exp]"
You are trying to display 'girllist[girl].MilestoneName'. The code has an error because the index is not looked at as a variable that results in its contained value. If you edit this line of code with hard values such as:

Code: Select all

"HP: [prota.HealthPoints]\nRelation: [girllist[0].MilestoneName]\nExperience: [girllist[0].Exp]"
Then you shouldn't get an error. I understand how this isn't desirable and the fix you posted solves this probably by avoiding using a variable index within the say statement.

Another possibility that came to mind is to use python's .format() function by turning your statement into a python statement. For example:

Code: Select all

   $ narrator("HP: {0}\nRelation: {1}\nExperience: {2}".format(prota.HealthPoints, girllist[girl].MilestoneName, girllist[girl].Exp))
The above code doesn't look all that pretty, but it should also work!

User avatar
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Re: Ren'Py won't recognize array index variable as integer

#3 Post by Ayutac »

jw2pfd wrote:Another possibility that came to mind is to use python's .format() function by turning your statement into a python statement.
Ah, this should work just fine. I thought about how to insert the whole stuff into the string in Python but didn't thought it had to have a format function. So, thank you very much. I would be still curious how it would work in Ren'Py mode :D
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

Post Reply

Who is online

Users browsing this forum: No registered users