Algorithm for tallying values of variables?

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
@berration
Regular
Posts: 70
Joined: Sun Jul 15, 2007 2:36 pm
Projects: EH...
Contact:

Algorithm for tallying values of variables?

#1 Post by @berration »

I wasn't quite sure how to phrase the subject line, but I had another weird question.

I'm toying with the idea of having a “Percentage complete” report for the player. Thanks to a scene skip system I managed to cobble together, each scene has a variable at the end that keeps track of whether or not the user finished reading it. They're named like this:

Code: Select all

persistent.SCENENAME_view
I know how to do what I want manually, but since all of the variables end in “_view” I was hoping that maybe there was an smarter/easier way to gather up the information I need.

In short: is there a way to get the total number of True values in all persistent variables with names ending with “_view”?


(I assume there's no way of counting up the total number of these variables (i.e., the total number of scenes in the VN), since they don't actually exist until they've been set to True.)
Image

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Algorithm for tallying values of variables?

#2 Post by PyTom »

Here's some python magic for you. Basically, for a python object o, o.__dict__ is a dictionary that maps the field names to their values. The following code uses a generator expression to iterate over the dictionary to find interesting values, and then len to count how many of those values it finds.

Since names beginning with _ are reserved for Ren'Py, this code filters them out for future-proofing.

Code: Select all

$ total_true = len(k for k, v in persistent.__dict__.iteritems() if k.endswith("_view") and not k.startswith("_") and v) 
I didn't actually test this code - let me know if it works.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

@berration
Regular
Posts: 70
Joined: Sun Jul 15, 2007 2:36 pm
Projects: EH...
Contact:

Re: Algorithm for tallying values of variables?

#3 Post by @berration »

Ah, that looks like precisely the kind of thing I'm looking for. Certainly better than the embarrassing alternative I came up with due to my lack of Python knowledge.
PyTom wrote:I didn't actually test this code - let me know if it works.
Unfortunately, I received this error:

TypeError: object of type 'generator' has no len()
Image

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Algorithm for tallying values of variables?

#4 Post by PyTom »

Sorry, turns out you have to turn it into a list first:

Code: Select all

$ total_true = len(list(k for k, v in persistent.__dict__.iteritems() if k.endswith("_view") and not k.startswith("_") and v)) 
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

@berration
Regular
Posts: 70
Joined: Sun Jul 15, 2007 2:36 pm
Projects: EH...
Contact:

Re: Algorithm for tallying values of variables?

#5 Post by @berration »

PyTom wrote:Sorry, turns out you have to turn it into a list first:
Ah, now that does work for me. Thanks!
Image

Post Reply

Who is online

Users browsing this forum: voluorem