Unicode Error in For Loop (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
User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Unicode Error in For Loop (solved)

#1 Post by noeinan »

Hello! I am trying to simplify some code so I can eventually add multiple NPCs to this without having to write out a copy for each possible NPC. However, when I added my loop, I'm getting an error message 'unicode' object has no attribute 'lhand_state' which I *think* is because when I iterate through the loop instead of npc1.lhand_state it's printing 'npc1'.lhand_state

I'm guessing this is the problem because of this: https://stackoverflow.com/questions/279 ... g-json-dic

However, because quotations are special, I can't figure out how to remove these quotations! Any advice on this?

I tried adding .interkeys() as shown in the above, but didn't work. I also tried this method, and again did not work: https://www.geeksforgeeks.org/python-re ... om-string/

Here's what I'm trying to do:

Code: Select all

for npc in npc_dict:
            npc.lhand_state = 0
            npc.rhand_state = 0
And here's my npc Generate function:

Code: Select all

    def GenerateMultipleNPCs(npc):
          ## npc is number of npcs you want to create
          npc_dict = {}

          for x in range(npc):
              ## add new dictionary entries named npc1, npc2, npc3 etc
              npc_dict['npc{}'.format(x+1)] = NPCList(racelist, typelist, procaselist, 1)
          return npc_dict
Last edited by noeinan on Sat Nov 28, 2020 5:12 pm, edited 1 time in total.
Image

Image
Image

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Unicode Error in For Loop

#2 Post by hell_oh_world »

When you do.

Code: Select all

for npc in npc_dict:
You're actually looping on the keys of the dict and not on your npc objects.
It's also the same as...

Code: Select all

for npc in npc_dict.keys(): # loop through the keys.
Just use the .values() method of the npc dict

Code: Select all

for npc in npc_dict.values(): # loop through the values which in ur case are the npc objects.

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Unicode Error in For Loop

#3 Post by noeinan »

Ah, that did it! Thank you!
Image

Image
Image

Post Reply

Who is online

Users browsing this forum: Lucyper, Majestic-12 [Bot]