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 » Mon Nov 28, 2016 5:03 pm

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
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: Data structures for plot-related variables

#2 Post by Ocelot » Mon Nov 28, 2016 5:19 pm

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 » Mon Nov 28, 2016 7:29 pm

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: 59
Joined: Thu Oct 27, 2016 3:22 pm
Contact:

Re: Data structures for plot-related variables

#4 Post by Ragnos » Thu Dec 22, 2016 11:26 pm

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: 15498
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 » Fri Dec 23, 2016 12:55 am

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
"Silly and fun things are important." - Elon Musk
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 » Tue Jan 10, 2017 7:44 pm

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: Google [Bot]