I haven't tried your helper function yet, but it did give me an idea. For anyone else who's using a set of dicts frequently like this, a quick function does the job beautifully and renders it into a single line of code in the dialogue sections.
Function:
Code: Select all
def somedescriptivebutshorttitle(string): # I'm actually using cgenSay as this is a set of defs that are constant throughout character creation. Something short, snappy, and clear is always the ideal - so if it's going to handle past dialogue variables set it to pastChat and so on! We want it to be less hassle than typing out the codes below, afterall.)
abdict = dict(object.a_dict.items() + object.b_dict.items()) # Presumably this will work for loose dictionaries too.
renpy.say(N, string.format(**abdict)) # I use N as my narrator shortref, which is probably bad practice. You need to wrap .format in a renpy.say(x,...) if it's going to be displayed using the dialogue system, or else it throws a no character exception. Swap N with whatever character the dialogue belongs to.
Calling the code
Code: Select all
$somedescriptivebutshorttitle('This is a sample string {a1} {b1}')
If you're not using a fixed dict for the string but want a more general function you'd just add dict1,dict2 as arguments to the function and redefine abdict (or whatever you're calling it) to pull dict(dict1.items()+dict2.items()). The function call would then need those added ($x('string',dict1,dict2)) etc) but has the benefit of being basically universal for any time you need to do this and call multiple dictionaries to fill in the .format {} strings. For my purpose fixing the dictionary to my equivalent of object.a_dict, object.b_dict is easier and a time saver, but a general purpose function is probably better practice.
The downside is each time it calls it it's still having to compile abdict, so if your dictionaries are static you could predefine them earlier and just use **abdict without the abdict = ... line. All it's really doing is keeping the code in your labels a little less messy rather than actually optimizing much (though it does cut down on the repetition - if you've only got one string that you need to do this in, then this'd be a bad way. Once you're at ten strings though, you've cut the amount of lines of code actually present in your file for this purpose by half!) Doing it this way also locks it to certain character voices, but as for my purpose this one is solely used for narrator's voice sections that's not an issue. If that needs to change too, you'd just set it to have character name in there, but by then you're probably better off doing it all directly so you can also properly do emotion changes and whatnot.
Presumably it should be fairly trivial to integrate your helper function code in and do it that way too.