Store new variable within a class?

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
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Store new variable within a class?

#1 Post by namastaii »

I can't seem to figure out the syntax for storing new information inside a variable that's within a class.

I can obviously do this:

Code: Select all

current_var = "hi"
replacement = "hello"

def new_var(x):
	store.x = replacement
	

Code: Select all

textbutton action Function(new_var(current_var))
But what if current_var was within a class?

I've tried variations of Function(new_var(class.current_var)) or including a storing function within the class and tried something like

Code: Select all

	class myclass(element):
        	def __init__(self, current_var):
            	self.current_var = current_var
           	
    		def new_var(x):
        		store.x.current_var = replacement
        		#x.store.current_var = replacement
        		#store.self.x = replacement
        		
just a lot of different combinations. And tried using the function in different ways as well because I really can't find the syntax for this

Code: Select all

textbutton action Function(myclass.new_var(current_var))

Code: Select all

textbutton action Function(new_var(myclass.current_var))

Code: Select all

textbutton action Function(myclass.new_var(myclass.current_var))
I've also tried defining new variables to avoid using so many . between things and I'm just making it worse as I go haha I hope this wasn't confusing and there's probably a really easy solution to this...

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Store new variable within a class?

#2 Post by Remix »

Directly:

SetField( store, "var_name", value )

Through a method:

Code: Select all

    def set_store_var( self, var, value ):
        setattr( store, var, value )

    Function( instance.set_store_var, "var_name", value )
You'd probably want to rewrite the setattr method of the class though, to use the store for known attributes. Alternatively, just default the instance and let Ren'Py handle the store itself
Frameworks & Scriptlets:

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Store new variable within a class?

#3 Post by namastaii »

Thank you, I'll try these^^

Post Reply

Who is online

Users browsing this forum: MisterPinetree