Data structures for plot-related variables

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
nub
Newbie
Posts: 1
Joined: Mon Jun 20, 2016 5:01 pm
Contact:

Data structures for plot-related variables

#1 Post by nub »

Hi all,
throughout developing the story in my VN I keep adding new globals like

Code: Select all

 
$ alice_met_bob = False 
$ bob_has_done_the_thing = True 

and I think this is a very bad idea for many reasons (possible problems with updates and saves, inconvenient names etc).

What are the best practices regarding these plot-related variables? Where should I store them (in a dictionary maybe)? Is there a way to somehow systematize this mess?

PS
for the character specific stuff (dresses, emotions, stats, sprites) I use a couple of classes

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2401
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Data structures for plot-related variables

#2 Post by Ocelot »

You see, in your example variables are not related at all. You can have it in global scope, you can create a new store for them, you can place tham in dictionatry, list, set, anything, and it will be essentually the same, you still need to name them somehow and access by that name.

If you have several related variables, you can group them in some data structure and manage everything by class.

For example, if I have a bunch of characters, which can have different relationship between each other, I would probably represent it as a graph, implemented as adjacency list, and contained in and managed by some class, which makes sure that we won't accidentally break its internals.
Of course, good old variables 'alice_and_bob_relationship', 'alice_and_suzan_relationship', 'bob_and_suzan_relationship' can be used too, but it is hard to represent, say, 15 characters or quickly check if arbitrary character knows everyone.

If you are afraid that you can accidentally access wrong variable, store story-related variables in named store: https://www.renpy.org/doc/html/python.h ... -the-store
If you afraid that you can set wrong variable (i.e. mystyping a variable name and setting a variable never actually used) and introduce a bunch of bugs, create a function which would check variable existence first, and then would set it. Kinda like screen actions do.
< < insert Rick Cook quote here > >

User avatar
Sonomi
Veteran
Posts: 211
Joined: Sat Oct 29, 2016 5:34 am
Projects: Lethargy of Snow
itch: sonomi
Contact:

Re: Data structures for plot-related variables

#3 Post by Sonomi »

Based on the information you provided, I believe a dictionary is more than sufficient. This is especially true if you intend to keep adding such variables (booleans) at a later point in time, because a dictionary is a flexible way of keeping your information together. You can also easily search for things, if you need to.

Another thing...it may be beneficial to use more inconvenient names in favor of clarity down the road. :)
Image

User avatar
Ragnos
Regular
Posts: 60
Joined: Thu Oct 27, 2016 3:22 pm
Contact:

Re: Data structures for plot-related variables

#4 Post by Ragnos »

One thing I like to do with variables is create an entire new code file and place all my variables there and sort them with comments like so.

Code: Select all

#########################
# Player Stats
#########################
$ str = 12
$ int = 15
etc...
#########################
# True/False
#########################
$ OpenedPickles = False
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: Data structures for plot-related variables

#5 Post by PyTom »

Note that str and int are both terrible names for Ren'Py variables, as they're Python builtins. So if you use them, things will almost certainly break hard.

Also, you probably want to use default before a variable:

default opened_pickles = False
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

User avatar
Suika
Regular
Posts: 99
Joined: Thu Oct 03, 2013 12:23 am
Contact:

Re: Data structures for plot-related variables

#6 Post by Suika »

Ocelot offers some good advice. One strategy I like for keeping track of flags is to declare a class and define static member variables for each flag. It keeps things grouped, and helps intellisense. Plus, if you need to iterate over the flags for whatever reason you can reflect over the class (but there are arguments on both sides of that one).

Custom data structures (are POPOs a thing?) make sense when you have a few related fields that get repeated multiple times, like keeping track of stats on NPCs.

Also, while I like Ragnos' separate file concept, I strongly agree with PyTom's comment on naming and default. I'll go ahead an add a short list of general best practices:
  1. Don't use built-in types as variable names
  2. Try to keep variable names descriptive whenever possible. players_inventory, number_of_enemies_killed, etc.
  3. Try to keep your naming conventions consistent. The python community recommends snake_case for variable names and PascalCase for class names, but I think consistency is more important than consensus.

Post Reply

Who is online

Users browsing this forum: No registered users