Is it possible to define a list of text values for a variable, that can be changed like numerical values?

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
skinnyfitgenes
Newbie
Posts: 6
Joined: Wed May 12, 2021 11:15 am
Contact:

Is it possible to define a list of text values for a variable, that can be changed like numerical values?

#1 Post by skinnyfitgenes » Fri May 21, 2021 2:00 pm

I don’t think I explained that very well, so I’ll try better here. Apologies, English is not my first language.

For example, if I have the seasons in my game. Is there any way to define a list in Renpy to say that the seasons go Spring, Summer, Fall, Winter, and then use “= +1” to cause that variable to change?

So in effect, I can set the default season as Summer, then include something like “ $ Season += 1” and have it change to Fall?

Thank you in advance for any help! Hopefully I’ve explained this well enough.

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

Re: Is it possible to define a list of text values for a variable, that can be changed like numerical values?

#2 Post by Alex » Fri May 21, 2021 3:12 pm

You can get the item of the list by its index, like

Code: Select all

seasons[0]
So, you can have a list 'seasons' and a variable to store an index of current season. Later you'll be able to increas the value of this variable, but be sure to not make it greater than list's length.

Try

Code: Select all

default seasons_list = ['Spring', 'Summer', 'Fall', 'Winter']
default season = 0

init python:
    def next_season_func(x=1):
        global seasons_list, season
        season = (season + x) % len(seasons_list) # reminder of division season on seasons_list length
        
screen ui_scr():
    text seasons_list[season] align (0.5, 0.05)

# The game starts here.
label start:
    "..."
    show screen ui_scr
    "... ..."
    $ next_season_func()
    "Summer..."
    $ next_season_func(3)
    "Time flies so fast - it's spring again."
    "?!"

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

Re: Is it possible to define a list of text values for a variable, that can be changed like numerical values?

#3 Post by Ocelot » Fri May 21, 2021 3:22 pm

If you have basic knowledge of Python, it is easy to do:

Code: Select all

class Seasons:
    SPRING = 0
    SUMMER = 1
    FALL = 2
    WINTER = 3
    NAMES = ["spring", "summer", "fall", "winter",]

    def __init__(self, start_season=0):
        self.current_season = start_season

    def advance(step=1):
        self.current_season += step
        self.current_season %= 4

    def set(season):
        self.current_season = season

    @property
    def now():
        return Seasons.NAMES[current_season]
This is a class, which encapsulates all season logic and allows easy access to it:

Code: Select all

$ season = Seasons(Seasons.WINTER)
"Right now it is [season.now]"
"If we wait two more seasons, it will become..."
$ season.advance(2)
"[season.now!c]!!"
"And the next one is..."
$ season.advance()
"[season.now!c]!"
"You can also set it manually"
$ season.set(Seasons.SPRING)
"Right now it is [season.now], just as expected"
If you really want, you can even make it work with +1, but it is not a good idea to do that.
< < insert Rick Cook quote here > >


Post Reply

Who is online

Users browsing this forum: No registered users