Ren'Py "String indices must be integers" error after screen (Solved)

GNVE

Active Member
Jul 20, 2018
724
1,184
Ok so I have this code that works:
Python:
menu (nvl=True):
        "Stressed":
            $ adder([('PC','emotional stability',-200)])
        "Routine":
            $ adder([('PC','openness',-200)])
which refers to this code:
Python:
def adder(list):
        for i in list:
            if i[0] == 'PC':
                PCstats[i[1]] += i[2]
            else:
                NPCstats[i[0]][i[1]] += i[2]
But if I call the screen below before calling the code above I get a "String indices must be integers" error on the code above. I am stumped as to why.
Python:
screen characterselection():
    imagemap:
        idle "WD90D1 gamestart PCunselected"
        hover "WD90D1 gamestart PCselected"

        hotspot (0, 0, 960, 1080):
            action SetVariable("PCstats","F"), Jump("actual_test")
        hotspot (960, 0, 960, 1080):
            action SetVariable("PCstats","M"), Jump("actual_test")

        text "Choose who you want to play as":
            align (0.5, 0.85)
You don't have permission to view the spoiler content. Log in or register now.
 

xj47

Member
Nov 4, 2017
241
409
What value is "PCstats" supposed to have? Your adder function suggests it's a dictionary, but the SetVariable() action in the screen sets it to a string (either "F" or "M")

If PCstats is set to, say, "F" then calling
Code:
adder([('PC','openness',-200)])
is equivalent to
Code:
PCstats["openness"] += -200
which is trying to access an "openness" keyed value on your "F" string. Since strings are not dictionaries, this fails and you get an error
 
  • Like
Reactions: GNVE

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
At first glance, aren't PCstats and NPCstats both some sort of ?

Where the variable PCstats is keyed by a list of strings like "emotional stability" or "openness"?

In that case, the action doesn't seem to match up. That appears to be setting a simple variable to a value of "F" or "M".

Does your dictionary include another keyed value like "gender" or "sex" ?
If that is the case, I'd expect to see something more like or something similar.


Edit: To explain the actual error... I think what is happening is that your "gender selection" is being done before your $adder() function.
So you go to all the effort of setting up your two "stats" arrays, then come along and completely replace PCstats with a simple string "F" or "M".

You can still address a simple string as if it's an array using an integer (number).
Let's say you have a simple string variable... $ my_string = "F95Zone".
my_string[0] is "F".
my_string[1] is "9".
my_string[2] is "5".
etc.

But adder() expects a dictionary. So the error is that you tried to access a string using a dictionary key, when strings can only be sub divided using an integer. But the underlying cause appears to be that your gender selection code overwrote your dictionary with a string.

On reflection, my guess is that your "gender selection" should be setting a completely different variable like PCgender or PCsex or simply sex... since "gender" isn't really a stat and NPCstats which seems to mirror PCstats probably doesn't need a gender statistic either (unless your NPCs can be a mix of male, female and/or futa).

Edit2:
On a completely different subject... do you actually need to separate PCStats and NPCstats?
It appears NPCstats is the same "statistic name and value" as PCstats but nested within another layer, which I guess is the NPC's name. With that being the case, couldn't you have a single stats dictionary that is keyed the same as NPCstats, where each stat is a combination of both the character name and the name of the statistic... but just pass the character name as "PC" ?
This is one of those "if it ain't broke, don't fix it" moments. What you have works... so you shouldn't change it. But it feels like you've added an unnecessary degree of complexity.
 
Last edited:
  • Like
Reactions: GNVE

GNVE

Active Member
Jul 20, 2018
724
1,184
Ah thanks guys.
I indeed F'd up my dictionary. I had been staring myself blind on that for a couple of hours there. Don't know how I missed that...
 

GNVE

Active Member
Jul 20, 2018
724
1,184
Edit2:
On a completely different subject... do you actually need to separate PCStats and NPCstats?
It appears NPCstats is the same "statistic name and value" as PCstats but nested within another layer, which I guess is the NPC's name. With that being the case, couldn't you have a single stats dictionary that is keyed the same as NPCstats, where each stat is a combination of both the character name and the name of the statistic... but just pass the character name as "PC" ?
This is one of those "if it ain't broke, don't fix it" moments. What you have works... so you shouldn't change it. But it feels like you've added an unnecessary degree of complexity.
There are a few small differences between the two but generally yes they could be within the same dictionary. I separated the two because it made more sense in my head if they where in two different dictionary's. And I set up the NPC's via a function. (It made more sense to have a list of all NPC's openness followed by a list of emotional stability etc so changes and lookups can be quick. Due to the adder function the extra complexity will be limited in the actual game script.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
Don't know how I missed that...

Standard programmer logic...

You spent all that time seeing the code you intended to write... not the code you actually wrote.

That's the problem with computers... they do what you tell them, not what you want.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,192
Standard programmer logic...

You spent all that time seeing the code you intended to write... not the code you actually wrote.
And for variable names, because you're focused on what you are looking at, in order to find the error, you tend to forgot the ones you used somewhere else in your code.

When looking at your adder function, "yeah, it's correct, PCStat is the dictionary where I store the stats."
When looking at your characterselection screen, "yeah, it's correct, PCStat is where I store MC's sex."
And at no time you realize that it's the same variable name in both.

One way to solve this is to use the approach. Before his death, my cat used to help me to solve a lot of problems. Just having to explain the problem to him, and say my thought while debugging, was enough in something like 75% of the time.
It put a distance between you and the problem, and help you see what is effectively wrote ; "So, you see, here I add the age of the captain... and what I do is in fact adding the number of rum bottles he drank... I'm an idiot... thanks for your help, you can go back to your nap."
 
  • Like
Reactions: GNVE and 79flavors