Accessing 2 Object Variables in a Single Interpolation

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
JRGrim
Newbie
Posts: 5
Joined: Sat Feb 08, 2020 3:55 pm
Contact:

Accessing 2 Object Variables in a Single Interpolation

#1 Post by JRGrim »

According to Tom, it's not possible to double interpolate using str.format(), which makes sense after reading the Python documentation on the subject. However, what I've been practicing is simplifying my code, cutting back on needles disposable variables cluttering the namespace. Currently, I'm looking for a way to display the current time of day in a dialogue. This was the failure: "It's [game.day_times[game.current_time]]."

My object uses a list for the times of day: ["Morning", "Noon", "Afternoon", "Evening", "Night"] and a int for the current time of day (index in list). I'm 100% self taught as a programmer, so there are practices I have not learned, but I'm hoping over time I will develop into a proficient coder. My quest is, what is the most efficient way to resolve this without making a "cast away" variable in the global.

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: Accessing 2 Object Variables in a Single Interpolation

#2 Post by trooper6 »

It seems like these variables are part of a class, is that true? How is the class defined?
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

JRGrim
Newbie
Posts: 5
Joined: Sat Feb 08, 2020 3:55 pm
Contact:

Re: Accessing 2 Object Variables in a Single Interpolation

#3 Post by JRGrim »

trooper6 wrote: Wed Feb 12, 2020 2:15 am It seems like these variables are part of a class, is that true? How is the class defined?
It is a class, and this is the simplified version:

Code: Select all

init -4 python:
    class Game(object):
        def __init__(self):
            self.day_times = ["Morning", "Noon", "Afternoon", "Evening", "Night"]
            self.current_time = 0
            
            
 #### In the script.rpy:
 default game = Game()

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: Accessing 2 Object Variables in a Single Interpolation

#4 Post by trooper6 »

You could use properties.

So this is a little script I made, check it out:

Code: Select all

init python:
    class Game(object):
        def __init__(self):
            self._day_times = ["Morning", "Noon", "Afternoon", "Evening", "Night"]
            self._current_time = 0

        @property
        def current_time(self): #getter
            return self._day_times[self._current_time]

        def advance(self):
            self._current_time += 1
            if self._current_time == 5:
                self._current_time = 0

default game = Game()

# The game starts here.

label start:
    "Hello! It is [game.current_time]"
    "Advancing the game."
    $game.advance()
    "Now it is [game.current_time]"
    "Advancing the game."
    $game.advance()
    "It is now [game.current_time]"
    "Advancing the game"
    $game.advance()
    "Now it is [game.current_time]"
    "Advancing the game."
    $game.advance()
    "Right now it is [game.current_time]"
    "Advancing the game, this should flip it over back to morning."
    $game.advance()
    "Wow! It is [game.current_time]"
    "Let's set the time to a Night specifically."
    $game._current_time = 4
    "What time is it? It is [game.current_time]"
    "Okay, 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

JRGrim
Newbie
Posts: 5
Joined: Sat Feb 08, 2020 3:55 pm
Contact:

Re: Accessing 2 Object Variables in a Single Interpolation

#5 Post by JRGrim »

trooper6 wrote: Wed Feb 12, 2020 3:05 am You could use properties.
Hey, neat! Thanks for that. I didn't know a property could return like a variable. I tried something similar, but defined a method, which just returned the method object. LOL

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: Accessing 2 Object Variables in a Single Interpolation

#6 Post by trooper6 »

No worries. Using properties enables you to avoid a bunch of shenanigans.
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

Post Reply

Who is online

Users browsing this forum: Google [Bot]