Page 1 of 1

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

Posted: Fri May 21, 2021 2:00 pm
by skinnyfitgenes
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.

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

Posted: Fri May 21, 2021 3:12 pm
by Alex
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."
    "?!"

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

Posted: Fri May 21, 2021 3:22 pm
by Ocelot
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.

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

Posted: Fri May 21, 2021 6:31 pm
by skinnyfitgenes
Thank you so much, this is working great!