Accessing a class list

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
Feleven
Newbie
Posts: 10
Joined: Tue Jun 01, 2021 10:23 am
Contact:

Accessing a class list

#1 Post by Feleven »

Hey, I'm using a simple day/time script found here viewtopic.php?t=49177
More precisely I'm talking about this list found here at self.time_of_day:

Code: Select all

init python:
    class day_time(object):
        def __init__(self):
            self.day = 1 # set this to whatever starting day is
            self.weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] # arrange these so first weekday goes first
            self.time_of_day = ["morning", "noon", "evening", "night"] # add or remove to increase time of day slots
            self.end_of_day = self.time_of_day[-1] # automatically picks last slot as end of day

        @property
        def weekday(self):
            return self.weekdays[(self.day-1)%7]

        @property
        def time(self):
            return self.time_of_day[0]

        def advance(self, increment = 0, days = 0):
            if not (increment + days): # no input
                increment = 1

            if days: # add to increment by length of time_of_day
                increment += days * len(self.time_of_day)

            while increment > 0: # loop through increments to shift timeslot and days forward
                if self.time_of_day[0] == self.end_of_day:
                    self.day += 1
                self.time_of_day.append(self.time_of_day.pop(0))
                increment -= 1 # reduce incrememnt to escape loop after enough runs

default clock = day_time()

I have a free roam mode in my game and want to make events run at certain times of the day with certain conditions fulfilled. So far I've been simply using:

Code: Select all

   if chara_story2 ==1 and chara_friendship >=2 and clock.time == "morning"
   or chara_story2 ==1 and chara_friendship >=2 and clock.time == "noon"
    
But honestly this is becoming a bit of a pain already with only a few events. I've got too much spaghetti code already I'd like to keep things a bit tidier. Since it's a list I assumed the entries have numbers attached and morning equals 0, noon = 1 etc. and I could just use

Code: Select all

 if clock.time <=2
To trigger the event at anything but night but it doesn't work and does nothing.
The only thing that does work this way is

Code: Select all

 if clock.time >=0 (or any number of this matter)
But this just triggers the event all the time.
FYI: Yes, I'm bad at python.

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

Re: Accessing a class list

#2 Post by Ocelot »

First of all, you can use

Code: Select all

if chara_story2 ==1 and chara_friendship >=2 and (clock.time == "morning" or clock.time == "noon"):
# or even
if chara_story2 ==1 and chara_friendship >=2 and clock.time in ("morning", "noon"):
Alternatively, get rid of that rotating list. I changed code a little, so you an get both weeday and time as numbers and can call *__name to get stringified name of either weekday ir time point.

Code: Select all

init python:
    class day_time(object):
        weekdays =  ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
        time_of_day = ["morning", "noon", "evening", "night"] 

        def __init__(self, start_day=1, start_time=0):
            self.day = start_day
            self.cur_time = start_time

        @property
        def day(self):
            return self.day

        @property
        def weekday(self):
            return (self.day - 1) % len(day_time.weekdays)

        @property
        def weekday_name(self):
            return day_time.weekdays[ (self.day - 1) % len(day_time.weekdays) ]

        @property
        def time(self):
            return self.cur_time

        @property
        def time_name(self):
            return day_time.time_of_day[self.cur_time]

        def advance(self, increment = 0, days = 0):
            if (increment + days) == 0:
                increment = 1
            self.day += days
            self.cur_time += increment
            self.day        += self.cur_time / len(day_time.time_of_day)
            self.cur_time = self.cur_time % len(day_time.time_of_day)

default clock = day_time()
< < insert Rick Cook quote here > >

Feleven
Newbie
Posts: 10
Joined: Tue Jun 01, 2021 10:23 am
Contact:

Re: Accessing a class list

#3 Post by Feleven »

So all I missed were (). It's funny that I usually struggle a few hours looking for a solution and it's something plain simple & obvious.
Thanks a lot. I will also give the changed code a go later.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]