Using PersistenteData for global flags and vars?

Discussion in 'General Modification' started by Sitra Achara, Sep 27, 2009.

Remove all ads!
  1. Sitra Achara

    Sitra Achara Senior Member

    Joined:
    Sep 1, 2003
    Messages:
    3,613
    Likes Received:
    537
    The global flags and vars list is starting to get a little crowded. As the amount of used flags increases, the potential for script conflicts rises. In fact, it's already happened to me, because I forgot to reserve some vars that Ranth later used.

    So, I set out to finally figure out how to use that Co8PersistentData thing Spellslinger added a long time ago. I don't understand half of what's going on behind the scenes because I'm not really a programmer, but in laymans terms it seems to be like this:


    • You can save data permanently into your own "global variables".
    • This data is kept in the slot000x.co8 file in your Save folder when you save the game (you may easily view it outside ToEE).
    • You can read and write data into said variables from any script.

    Following the example in the enlarge person script file, I've created a few scripts to read and write flags, variables and even strings.

    Code:
    import _include
    from co8Util.PersistentData import *
    
    def get_f(flagkey):
        flagkey_stringized = 'Flaggg' + str(flagkey)
        tempp = Co8PersistentData.getData(flagkey_stringized)
        if isNone(tempp):
            return 0
        else:
            return int(tempp) != 0
    
    def set_f(flagkey, new_value):
        flagkey_stringized = 'Flaggg' + str(flagkey)
        Co8PersistentData.setData(flagkey_stringized, new_value)
    
    def get_v(varkey):
        varkey_stringized = 'Varrr' + str(varkey)
        tempp = Co8PersistentData.getData(varkey_stringized)
        if isNone(tempp):
            return 0
        else:
            return int(tempp)
    
    def set_v(varkey, new_value):
        varkey_stringized = 'Varrr' + str(varkey)
        Co8PersistentData.setData(varkey_stringized, new_value)
    
    
    def get_s(strkey):
        # reads strings
        strkey_stringized = 'Stringgg' + str(strkey)
        tempp = Co8PersistentData.getData(strkey_stringized)
        if isNone(tempp):
            return ''
        else:
            return str(tempp)
    
    def set_s(strkey, new_value):
        # writes strings
        new_value_stringized = str(new_value)
        strkey_stringized = 'Stringgg' + str(strkey)
        Co8PersistentData.setData(strkey_stringized, new_value_stringized)
    
    Example of use:

    Code:
    {6}{Ladies. I guess you had some exciting company recently, eh? Beautiful woman?}{}{8}{get_f(499) and not get_f('fuga1')}{30}{set_f('fuga1', 1)}
    
    As you can see, you can create special variable names (e.g. 'fuga1') or just use a number (499, to supplant game.global_flags[499] for instance).

    Another example: you can make a script to save you the trouble of uniquely naming your variables.

    E.g. let's say Gaear wants his own set of flags:
    Code:
    import _include
    from co8Util.PersistentData import *
    
    def gaear_f_get(flagkey):
         flagkey_stringized = 'GaearFlaggg' + str(flagkey)
         tempp = Co8PersistentData.getData(flagkey_stringized)
         if isNone(tempp):
             return 0
         else:
             return int(tempp) != 0
     
     def gaear_f_set(flagkey, new_value):
         flagkey_stringized = 'GaearFlaggg' + str(flagkey)
         Co8PersistentData.setData(flagkey_stringized, new_value)
    
    Also, as noted, you can easily save string this way too. Sadly, it seems like we can't use @pcname@ for real scripting inside dialogue, but hopefully someone will find a use for it...
     
Our Host!