[Solved] How to (key/value) loop through a dictionary of class objects?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] How to (key/value) loop through a dictionary of class objects?

#1 Post by henvu50 »

Solved thanks to Remix from discord.

This is how you loop through a dictionary and output it's content via renpy.say ... I would've never figured this out on my own.

Code: Select all


define dict_people = {
    "Bob": aPerson("Bob", 34),
    "Zoe": aPerson("Zoe", 54)}

# if this isn't -1, it won't work. i have no idea why.
init -1 python:

    class aPerson():
        def __init__ (self, str_name, str_age):
            self.s_name = str_name
            self.age = str_age

    def say_data():
        for k,v in dict_people.items():
            renpy.say("", "{0} is {1.s_name} aged {1.age}".format(k,v))

label start:
    $ say_data()    
how to add entries to the dictionary using update

Code: Select all

define personTed = aPerson("Ted", 76)
define personNuget = aPerson("Nuget", 98)

#this adds the entries
$ dict_people.update({"Ted": personTed})
$ dict_people.update({"Nuget": personNuget})

Post Reply

Who is online

Users browsing this forum: No registered users