Python statement equivalent for the default statement

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
n_dimensions
Newbie
Posts: 2
Joined: Fri May 24, 2024 5:03 pm
Contact:

Python statement equivalent for the default statement

#1 Post by n_dimensions »

=== UPDATE ===
I've decided to give up the idea, once I've noticed that even if I make it work, replacing the original object in the namespace would lead to problems if I'd like to recreate the singleton, for example when updating the variables to the never version of the game.

I'd still be interested to know how and where in the engine are the stores implemented. I've found the StoreModule class in renpy/python.py, but it's pretty bare-bones and I can't really find where is it actually used and how does the engine decide which variables should be saved and which shouldn't.
==============

=== ORIGINAL POST ===
So, I'm storing most of my game variables in singleton objects. This is a problem as changes in the fields of those objects aren't saved by Ren'Py.

An obvious solution would be to instantiate each in a separate default statement, but it would lead to spatial separation between the object and the instance and, even worse, it leads to quite a bit of boilerplate code.

So I've been thinking of automating it with something along the lines of

Code: Select all

init python:
    def singleton(cl, *args, **kwargs):
        default_store.__sett_default_attr__(cl.__class__.__name__, cl(*args, **kwargs))
        
    @singleton(1, 2)
    class ASingletonObject:
        def __init__ (self, one, two):
            self.one = one
            self.two = two
Though it doesn't seem like modifying stores in python is part of the stable API. Anyone knows how it's done or has better ideas?

I'm not too concerned about it changing between versions as it seems like a rather core feature of Ren'Py. And even if it should change, once I know how it's done, tracking down the change history shouldn't be very time consuming.
Last edited by n_dimensions on Mon May 27, 2024 5:11 pm, edited 1 time in total.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1060
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Python statement equivalent for the default statement

#2 Post by m_from_space »

n_dimensions wrote: Sun May 26, 2024 6:03 pm So, I'm storing most of my game variables in singleton objects. This is a problem as changes in the fields of those objects aren't saved by Ren'Py.
Uhm, I'm really interested, why exactly are you doing this? Using singletons I mean. I have a project with a class that only uses a single instance of it and it would be the perfect canditate for this "singleton" idea. But even there I don't use it, because I am the programmer of my project and this isn't rocket science or any security related program.

And yes, using "default" is the best and easiest way to do it (I do it like this as well for my "singleton" object). Are you planning on writing the most cryptic code ever? :P

n_dimensions
Newbie
Posts: 2
Joined: Fri May 24, 2024 5:03 pm
Contact:

Re: Python statement equivalent for the default statement

#3 Post by n_dimensions »

m_from_space wrote: Mon May 27, 2024 2:19 pm Uhm, I'm really interested, why exactly are you doing this?
Well, python is not best known for information hiding and in Ren'Py you don't even get module (file) variable scope. So I'm packing everything in objects. This way I can get an okey'ish separation.
m_from_space wrote: Mon May 27, 2024 2:19 pm Are you planning on writing the most cryptic code ever? :P
Well, everyone has their own preferred code style. I'm personally committed to DRY. When you know the language, a "cryptic" one liner is easier to wrap your head around, than 15+ ish lines of code. That is once you've written and read a few of them.

Plus programming is always the most fan part of the project, so I'm always down for messing with the engine a bit.

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

Re: Python statement equivalent for the default statement

#4 Post by Ocelot »

n_dimensions wrote: Mon May 27, 2024 5:03 pm Well, python is not best known for information hiding and in Ren'Py you don't even get module (file) variable scope. So I'm packing everything in objects. This way I can get an okey'ish separation.
Technically RenPy already does this. It hides everything related to engine itself and gives you access to fields of store object. And you can create other stores if you want to separate variables more. audio, character, persistent are some of them defined by default.


To make RenPy add something to the list of saved variables, a single thing should happen: a top-level reference should change after init phase. In old times, before default was introduced (which does the same, but discreetly), if you really want something to save, you would do this:

Code: Select all

label start:
    $ i_really_want_this_to_save = default_value
< < insert Rick Cook quote here > >

jeffster
Miko-Class Veteran
Posts: 520
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Python statement equivalent for the default statement

#5 Post by jeffster »

n_dimensions wrote: Sun May 26, 2024 6:03 pm I'd still be interested to know how and where in the engine are the stores implemented. I've found the StoreModule class in renpy/python.py, but it's pretty bare-bones and I can't really find where is it actually used and how does the engine decide which variables should be saved and which shouldn't.
Saves explained
https://renpy.org/doc/html/save_load_rollback.html

Easy to track code functionality here
https://github.com/renpy/renpy

Creator-Defined Statements - to extend the syntax with whatever you want under the hood
https://renpy.org/doc/html/cds.html

SimpleNamespace etc. can be used with import types
https://docs.python.org/3/library/types ... eNamespace
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]