calling a dynamic variable within a class function

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
Theh0llowone
Newbie
Posts: 2
Joined: Fri May 06, 2022 1:43 pm
Contact:

calling a dynamic variable within a class function

#1 Post by Theh0llowone » Fri May 06, 2022 2:22 pm

Hi, this is my first post here so I'm sorry if I screw up any rules/formatting,
I'm also sorry if the title is unclear as to what I'm asking, but even I don't know what to call it,
What I generally want to know is if there is a way to have the variables in a function of a class be changed by the input, for example having something along the lines of info.update (Info, name of the variable).
Just to be able to not have to have a function to update each individual variable.
Thank you for taking the time to read about my issue.

User avatar
RicharDann
Veteran
Posts: 284
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: calling a dynamic variable within a class function

#2 Post by RicharDann » Fri May 06, 2022 2:59 pm

I don't fully understand your question, but if you want to change the value of an object property, you can just assign the new value with an equal sign (=):

Code: Select all

init python:

    class Person():

        def __init__(self): 

            self.name = "Unknown"

default john = Person()

label start:
    "Default name: [john.name]"

    $ john.name = "John Doe"

    "New name: [john.name]"
The most important step is always the next one.

Theh0llowone
Newbie
Posts: 2
Joined: Fri May 06, 2022 1:43 pm
Contact:

Re: calling a dynamic variable within a class function

#3 Post by Theh0llowone » Fri May 06, 2022 4:57 pm

Ahh, sorry my explanation wasn't the greatest, thanks for trying to answer anyway, I'm trying to see if it's possible to have one function that takes two arguments, one that is the information to be stored, and the other being the variable to store it in, for example, something like

Code: Select all

class LoveInterest(object):
        def __init__ (self, Affection, Friendship):
            self.Affection = Affection
            self.Friendship = Friendship
       
        @property
        def UpdateInfo(self, info, variablename):

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: calling a dynamic variable within a class function

#4 Post by Ocelot » Fri May 06, 2022 10:10 pm

Code: Select all

class Test:
    def update_info(self, variable, value):
        setattr(self, variable, value)

# . . .
$test = Test()
$test.update_value("level", 555)
"Level is :[text.level]"
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot]