Creating temporary characters

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
Gamma Vector
Regular
Posts: 26
Joined: Thu Feb 25, 2010 9:15 pm
Contact:

Creating temporary characters

#1 Post by Gamma Vector »

So I'm dynamically creating temporary, disposable characters who only need to exist for one scene and then they will never show up again. The scene they appear in is repeatable (and will repeat many, many times over the course of the game) and the characters need to be different every time. They have randomized names, and are created with a custom class and function that takes several arguments including a Character Object.

Now, here's where I'm worried. Everything works fine so far - but I am creating a new Character Object and a new Bystander Class Object every time I instantiate one of these randos, right? I'm using the same variables to hold them each time - bystander_01, bystander_02 etc etc up to bystander_12.

I THINK this should be fine. Only the last twelve objects created (that are currently stored in the variables) should be written to the save game, right? If I overwrite bystander_01 with a new instance of my custom class, the old Bystander Object and associated Character Object should just vanish into the ether, right?

Or am I setting myself up for save bloat and eventual performance issues if the player repeats this scene too often? Should I instead be doing something like creating eight Bystander Objects at the start and using defaulted variables for all their attributes and then only changing their attributes as I re-roll them instead of creating new instances? Am I just overthinking this? What's the best practice here?

User avatar
Jackkel Dragon
Veteran
Posts: 283
Joined: Mon Mar 31, 2014 7:17 pm
Organization: Nightshade, Team Despair
itch: jackkel-dragon
Location: USA
Contact:

Re: Creating temporary characters

#2 Post by Jackkel Dragon »

I'm not an expert on the topic, but unless you're saving long lists/dicts of big classes, the default Python and Ren'Py garbage collection are going to be doing most of the heavy lifting here. [for more info: https://www.geeksforgeeks.org/garbage-c ... on-python/]

For instance, if you have something like this (with no other use of char1 in your code):

Code: Select all

label repeatable_scene():
    $ char1 = NewCharacter()
    "dialogue"
    return
Because char1 is being set to a new instance each time, the old instance loses its only "reference count" and thus will be marked for collection. This means it may not stay in RAM, much less the save data.

If you're still worried, you can also manually delete variables you're done with, which I think is covered in that link above. I'm pretty sure it still works in Ren'Py, but I've rarely bothered with deleting variables created during labels, so I don't know how it interacts with saving and rollback.
Main Website
Includes information about and links to many of my current and past projects.

Major Game Projects
[Nightshade] Eldritch Academy, Eldritch University, Blooming Nightshade, Flowering Nightshade, Life as Designed
[Team Despair] Corpse Party D2 series

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

Re: Creating temporary characters

#3 Post by m_from_space »

Gamma Vector wrote: Thu Dec 28, 2023 12:28 pmIf I overwrite bystander_01 with a new instance of my custom class, the old Bystander Object and associated Character Object should just vanish into the ether, right?
Yes. It was pointed out nicely by @Jackkel Dragon. I just want to add, that Renpy will only save variables, that are created using "default" and the ones that you create during game time inside labels (with a different variable names I mean) - and probably if you specifically add variables to the store object. So if you just create a new object and reference it with an old variable, that's the new contents then. In Python everything is a reference. If you want to make sure Renpy won't save a variable, just create one using "define" instead (but changing it during runtime may have unforseen consequences, since Renpy tries to use them on init time to make things more performant).

Also I think you could have thousands of those objects without any problems.

By the way, it's weird that you also have a class like me that created chars and uses Character() objects inside of it. :lol:

Gamma Vector
Regular
Posts: 26
Joined: Thu Feb 25, 2010 9:15 pm
Contact:

Re: Creating temporary characters

#4 Post by Gamma Vector »

Thank you both for the reassurance.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Sirifys-Al, Zapor