[Solved] Saving games, classes, and constants. How to use init in the right way?

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
User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

[Solved] Saving games, classes, and constants. How to use init in the right way?

#1 Post by Chekhov »

Here is a bit of a mystery to me. I think I'm structuring things incorrectly, but I'm not sure in what way.

I'll make a simplified version of what I'm dealing with.

Code: Select all

init python:
	hair_color_list =   ["blonde", "brown", "black"]
	class Girl(object):
        	def __init__(self):
            		self.hair = random.choice(hair_color_list)
	
	
	
Now you might already see the double bind that I am in. 1. If I put this after a init python: it means that when I load a game, it will be gone.
2. If I put it in regular python:, then there is no Girl class when I load the game and the game crashes.
3. If I put only the Girl class in the init python:, then it misses a hair_color_list
4. Of course I could solve this by defining the list twice, but that seems a very inelegant solution, and bound to lead to errors later on in development.

How should I resolve this?
Last edited by Chekhov on Sun Mar 31, 2019 2:28 am, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#2 Post by xavimat »

What's the problem with number 1?
The code you've provided should work. Classes are defined in an "init python" block.

On the other hand, your code does nothing in an actual game. You need to create instances of that class. If you create the instances in a label, they should be saved and loaded. If you are experiencing a problem in your game, probably lies there. Where do you create your instances?

Btw, renpy has its own random functions, for example "renpy.random.choice()". Better if you use those.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#3 Post by trooper6 »

1) All variables should be initialized outside of any block using either default if that variable might change, or define if it won't. This will fix the problem you are asking about.
2) rather than random.choice, that code should be renpy.random.choice

So try this code (I'm assuming hair color won't change):

Code: Select all

define hair_color_list = ["blonde", "brown", "black"]
init python:
    class Girl(object):
        def __init__(self):
            self.hair = renpy.random.choice(hair_color_list)

default jane = Girl()

label start():
    "Quick check."
    "Jane's hair color is [jane.hair]"
    "Test Over."
    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#4 Post by Chekhov »

xavimat wrote: Sat Mar 30, 2019 3:47 pm What's the problem with number 1?
The code you've provided should work.
Because I independantly need the hair_color_list. But since it's been set in a init block, if you load the game, it will be gone/empty.
Btw, renpy has its own random functions, for example "renpy.random.choice()". Better if you use those.
Is it the full random functionality of python? I've been using import random and functionality such as shuffle and sample.

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#5 Post by Chekhov »

trooper6 wrote: Sat Mar 30, 2019 3:52 pm 1) All variables should be initialized outside of any block using either default if that variable might change, or define if it won't. This will fix the problem you are asking about.
I had been wondering what define was for, I guess this solves it, thanks.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#6 Post by Alex »

Chekhov wrote: Sun Mar 31, 2019 2:28 am I had been wondering what define was for, I guess this solves it, thanks.
https://www.renpy.org/doc/html/python.h ... -statement

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Saving games, classes, and constants. How to use init in the right way?

#7 Post by xavimat »

Chekhov wrote: Sun Mar 31, 2019 2:27 amBecause I independantly need the hair_color_list. But since it's been set in a init block, if you load the game, it will be gone/empty.
Are you sure? Why it should be gone when loading? Are you emptying it?

Declaring variables in an init python block causes problems with save/load (that's the reason they should be declared with "default") but that's only a problem if the variable is going to change in-game. If you are using that variable as a constant, it really does not make a difference (but it's better to declare it with "define" though).
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Andredron