Using "default" to declare but time it as "define"?

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
nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Using "default" to declare but time it as "define"?

#1 Post by nananame » Sun Mar 15, 2020 12:53 pm

I have a class. When an object is created for that class in the constractor it checks some stuff before adding the object to a relevant list.

This list has to be declared of course. Thing is, if I declare it as default, the class initialization runs before this and causes an error that this global variable is not defined.
If I delcare it with define it works as it should. However, this list will also be changed during the game and with it being declared using define the changes will not be saved.

My current workaround is to not use the constructor but to declare the list with "default" and then manually run the function for each object to add it to the list.

I would of course prefer to use the constructor and this brings me to my question: Is it possible to somehow declare a variable which will be normally changed and saved but which will happen early enough for the constructor to catch it?

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Using "default" to declare but time it as "define"?

#2 Post by gas » Sun Mar 15, 2020 2:46 pm

This is how the Renpy Dating Sim Engine create a list of all stats and add Stats objects to it when instantiated.

Code: Select all

init -100 python:

    __dse_stats = [ ]

    class __Stat(object):

        def __init__(self, name, var, default, max, hidden=False):
            self.name = name
            self.var = var
            self.default = default
            self.max = max
            self.hidden = hidden

    def __init_stats():
        for s in __dse_stats:
            setattr(store, s.var, s.default)

    config.start_callbacks.append(__init_stats)
    
    # Call this function to add a stat to keep track of. 
    def register_stat(name, var, default=0, max=100, hidden=False):
        __dse_stats.append(__Stat(name, var, default, max, hidden))

    ######## LATER...
init python:
    register_stat("Strength", "strength", 10, 100)
    register_stat("Intelligence", "intelligence", 10, 100)
    register_stat("Relaxation", "relaxation", hidden=True)
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: Using "default" to declare but time it as "define"?

#3 Post by nananame » Sun Mar 15, 2020 3:52 pm

So this basically creates a list of objects which are not declared as usual but created by a function which also adds them to the list at the same time.

But the question here is -> the list itself is created at init-100 and things are appended to it at start of game. Will this list be changeable and saveable mid game?
Say for instance I decide mid game that I don't want the stat Strength in there any more? If I remove it from the list will that get saved? If yes, does that mean all it takes is to create a variable at init -100 for it to be saveable with changes?

Post Reply

Who is online

Users browsing this forum: Google [Bot], TioNick