Array doesn't accept integer variables as index? [Solved]

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
HochElf
Newbie
Posts: 12
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Array doesn't accept integer variables as index? [Solved]

#1 Post by HochElf »

I'm currently working on a Time system that should tell the reader which day it is. My first idea is an array of string and an index counter
My code:

Code: Select all

define i_dayOfTheWeek = 0  // Will have a value between 0 and 6
define s_dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] // The array itself
define z = Character("Zander")  

z"My Name is [z] Trope. And today is [s_dayOfTheWeek[i_dayOfTheWeek]]."  //The line that goes bonkers


The Error message is, that an index of a list has to be an integer, not Unicode. When I look into example code, they use variables too. And when I test the line with a fixed number, then there is no problem. Do I miss something?
Last edited by HochElf on Mon Apr 15, 2019 4:38 am, edited 2 times in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Array doesn't accept integer variables as index?

#2 Post by Per K Grok »

HochElf wrote: Sun Apr 14, 2019 10:52 am I'm currently working on a Time system that should tell the ready which day it is. My first idea is an array of string and an index counter
My code:

Code: Select all

define i_dayOfTheWeek = 0  // Will have a value between 0 and 6
define s_dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] // The array itself
define z = Character("Zander")  

z"My Name is [z] Trope. And today is [s_dayOfTheWeek[i_dayOfTheWeek]]."  //The line that goes bonkers


The Error message is, that an index of a list has to be an integer, not Unicode. When I look into example code, they use variables too. And when I test the line with a fixed number, then there is no problem. Do I miss something?
Try this

Code: Select all

    $ day = s_dayOfTheWeek[i_dayOfTheWeek]
    z "My Name is [z] Trope. And today is [day]." 

HochElf
Newbie
Posts: 12
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Re: Array doesn't accept integer variables as index?

#3 Post by HochElf »

This workaround helps. :)
Thanks for that.
But why does my solution not work? :|

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Array doesn't accept integer variables as index?

#4 Post by Per K Grok »

HochElf wrote: Sun Apr 14, 2019 12:37 pm This workaround helps. :)
Thanks for that.
But why does my solution not work? :|
I don't know for certain

but, you are working inside a renpy text string with interpolation. The purpose of interpolation is to change something to a string. In your case you have two steps, first idayoftheweek has to be recognized as an integer and that is probably where things break down. In this situation the variable is probably treated as a string, not an integer. When you use an integer directly there is only one step and that interpolation can handle.

That is my hypothesis.

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: Array doesn't accept integer variables as index?

#5 Post by SleepKirby »

Ren'Py's text interpolation is implemented using the standard Python string.Formatter class (the main difference being that Ren'Py customizes it to use square brackets instead of curly braces).

The text in between the outer brackets

Code: Select all

s_dayOfTheWeek[i_dayOfTheWeek]
gets passed to this text interpolation system. This interpolation system basically uses a language of its own, similar but not quite the same as Python or Ren'Py. The above text interpolation string gets interpreted like the following Python:

Code: Select all

s_dayOfTheWeek["i_dayOfTheWeek"]
So, the code would work if s_dayOfTheWeek were a dictionary which contained the string key "i_dayOfTheWeek". But s_dayOfTheWeek is a list, so it doesn't take string (Unicode) keys, hence the error message you got.

As you noticed though, the text interpolation system DOES treat integer literals between brackets as integer indices. So the following text interpolation string is equivalent to the same line in Python:

Code: Select all

s_dayOfTheWeek[0]
In short, the text interpolation 'language' is pretty limited compared to general Python/Ren'Py, so not all kinds of Python code will work there.

HochElf
Newbie
Posts: 12
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Re: Array doesn't accept integer variables as index?

#6 Post by HochElf »

Okay, that explains it. Guess I have to rethink more concepts I'm used to. Thanks for your help.

Post Reply

Who is online

Users browsing this forum: Google [Bot]