Page 1 of 1

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

Posted: Sun Apr 14, 2019 10:52 am
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?

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

Posted: Sun Apr 14, 2019 12:23 pm
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]." 

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

Posted: Sun Apr 14, 2019 12:37 pm
by HochElf
This workaround helps. :)
Thanks for that.
But why does my solution not work? :|

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

Posted: Mon Apr 15, 2019 1:38 am
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.

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

Posted: Mon Apr 15, 2019 2:55 am
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.

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

Posted: Mon Apr 15, 2019 4:38 am
by HochElf
Okay, that explains it. Guess I have to rethink more concepts I'm used to. Thanks for your help.