Define, Default, Classes, Lists

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
hapciupalit
Regular
Posts: 56
Joined: Tue Nov 13, 2018 6:00 am
Contact:

Define, Default, Classes, Lists

#1 Post by hapciupalit »

In my head it's a mess. I mean, I know a little bit of python but this thing is way over my head.
Here is the whole story.

There is a screen with a background which can be changed. You can unlock new backgorunds either by playing the story or by introducing some codes.

MobileBackground is a class that takes the name of the background in my assets and a boolean which decides if it will be possible to use that background.
mobile_bkg_01 is just an object. I wanted it to be default because you can change it's unlocked value, so I don't see the point of having a define.

Code: Select all

init python:
	class MobileBackground:
        	def __init__(self, name_id, unlocked = False):
            		self.name_id = name_id
            		self.unlocked = unlocked

        	def unlock(self):
           		self.unlocked = True
           		renpy.retain_after_load()
           		

default mobile_bkg_01 = MobileBackground("test")   		

Then I have a class CheaterCode. This class takes three params: two strings and a function that I need to call.

There is also a function checkCode which is called when a user presses submit in a screen after writing something in an input(cheater_code). In this function I check if the written value is in a list(cheater_codes) of CheaterCode objects. If it's there then it will call a function.

Then there is cheater_code_01 which is a CheaterCode object with everything I need.

Code: Select all

init python:
	class CheaterCode:
        	def __init__(self, code, message, func):
            	self.code = code
            	self.message = message
            	self.func = func
            	
       def checkCode(cheater_code):
       		for c in cheater_codes:
            		if c.code == cheater_code:
                		renpy.notify("%s"%(c.message))
                		c.func()
            	else:
                	renpy.notify("%s is wrong. Please try with a different one!"%(cheater_code))

define cheater_code_01 = CheaterCode("test", "You have unlocked a new background for your phone.", Function(mobile_bkg_01.unlock))
define cheater_codes = [cheater_code_01]
My problem right now is that when I try to define cheater_code_01 it says that mobile_bkg_01 is not defined and I guess it's because of the fact that it's a default. If I change it to define then it ruins the game because if the player starts a new game he has this background already unlocked, which I don't want.
So my question is how can I refer to a default value in define. I have the same problem when I try something simpler like this:

Code: Select all

default val1 = "Here goes nothing"
default val2 = "Hello world"

define my_list = [val1, val2]
I need this kind of approach because the game I'm developing will have an update each month val1 and val2 might get changed while playing so it needs to be default. and my_list needs to be a define because I might add more variables in the upcoming months so I can just append them in the list by default when I create them.

So I guess that if I manage to fix one problem I'll manage to fix both of them.

Thank you!

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Define, Default, Classes, Lists

#2 Post by Kia »

you can't use a 'default' in a 'define'. if I'm not mistaking, 'defined' variables are assigned when the game is launched and 'default' variables are assigned when a new game is started. one simply comes before the other.
you can re assign your values right after the start label if you want them to reset with a new game.
I think the most likely problem you'll have would be values that you want to keep but reset with a new game, in that case, you can use a persistent variable to keep track of them.

hapciupalit
Regular
Posts: 56
Joined: Tue Nov 13, 2018 6:00 am
Contact:

Re: Define, Default, Classes, Lists

#3 Post by hapciupalit »

Kia wrote: Mon Mar 02, 2020 2:16 am you can't use a 'default' in a 'define'. if I'm not mistaking, 'defined' variables are assigned when the game is launched and 'default' variables are assigned when a new game is started. one simply comes before the other.
you can re assign your values right after the start label if you want them to reset with a new game.
I think the most likely problem you'll have would be values that you want to keep but reset with a new game, in that case, you can use a persistent variable to keep track of them.
Thank you. I'll look into persistent variables because I'm not sure what are those.
Thanks a lot.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]