Trouble with Time/Day Class

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
BM90
Newbie
Posts: 15
Joined: Sat Mar 31, 2018 2:48 am
Contact:

Trouble with Time/Day Class

#1 Post by BM90 »

I'm having creating a simple class to control the in game time and day. I'm very new to using classes, but I've watched some tutorials and looked over some examples to get a basic understanding. I feel like most of this is correct, but unfortunately I still get the following error message upon starting the game: name 'time' is not defined. I've tried lots of small tweaks and compared it against some examples, but with no luck. Can someone please explain what I've done wrong with this. I feel like it's probably something very simple that I just don't understand yet.

Code: Select all

init python:
    class Time(object):
        def __init__(self, tod, day):
            self.tod = tod
            self.day = day

        def adv_time(self):
            self.tod += 1
            if self.tod > 4:
                self.tod = 1
                self.day += 1
                if self.day > 7:
                    self.day = 1

label start:
    $ tod = 1
    $ day = 1
    jump test_label

label test_label:
    call screen test_screen
    return

screen test_screen:
    text "{b}[tod]{/b}" xalign 0.5 yalign 0.45
    text "{b}[day]{/b}" xalign 0.5 yalign 0.55
    textbutton "{b}ADVANCE TIME{/b}":
        xalign 0.5 
        yalign 0.3
        action Function(time.adv_time)

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Trouble with Time/Day Class

#2 Post by Per K Grok »

BM90 wrote: Thu Jul 16, 2020 2:45 am I'm having creating a simple class to control the in game time and day. I'm very new to using classes, but I've watched some tutorials and looked over some examples to get a basic understanding. I feel like most of this is correct, but unfortunately I still get the following error message upon starting the game: name 'time' is not defined. I've tried lots of small tweaks and compared it against some examples, but with no luck. Can someone please explain what I've done wrong with this. I feel like it's probably something very simple that I just don't understand yet.

Code: Select all

init python:
    class Time(object):
        def __init__(self, tod, day):
            self.tod = tod
            self.day = day

        def adv_time(self):
            self.tod += 1
            if self.tod > 4:
                self.tod = 1
                self.day += 1
                if self.day > 7:
                    self.day = 1

label start:
    $ tod = 1
    $ day = 1
    jump test_label

label test_label:
    call screen test_screen
    return

screen test_screen:
    text "{b}[tod]{/b}" xalign 0.5 yalign 0.45
    text "{b}[day]{/b}" xalign 0.5 yalign 0.55
    textbutton "{b}ADVANCE TIME{/b}":
        xalign 0.5 
        yalign 0.3
        action Function(time.adv_time)
try this

Code: Select all

init python:
    class Time(object):
        def __init__(self, tod, day):
            self.tod = tod
            self.day = day

        def adv_time(self):
            self.tod += 1
            if self.tod > 4:
                self.tod = 1
                self.day += 1
                if self.day > 7:
                    self.day = 1


    ttime=Time(1,1)                    ## create an object "ttime" using the class
                                        ## Time, set ttime.tod to 1 and the same
                                        ## for ttime.day




label start:
    #$ tod = 1                       # remove tod and day variables
    #$ day = 1
    jump test_label

label test_label:
    call screen test_screen
    return

screen test_screen:
    text "{b}[ttime.tod]{/b}" xalign 0.5 yalign 0.45       # use ttime.tod not tod
    text "{b}[ttime.day]{/b}" xalign 0.5 yalign 0.55       # ttime.day 
    textbutton "{b}ADVANCE TIME{/b}":
        xalign 0.5
        yalign 0.3
        action Function(ttime.adv_time)             ## use ttime.adv_time


time is a protected word, so I have named the object ttime to avoid using a protected word.

User avatar
Autumnotopia
Regular
Posts: 59
Joined: Sun May 19, 2019 8:53 pm
Completed: Making Friends
Tumblr: autumnotopiadev
Github: autumngreenley
itch: autumnotopia
Contact:

Re: Trouble with Time/Day Class

#3 Post by Autumnotopia »

^ Was my first thought too ('time' is a name I'd be cautious to use because many languages have a preexisting 'time' thing), but in addition to that, I notice that when you're trying to call the class function you use 'action Function(time.adv_time)'. This has another issue, but also Python is case sensitive. 'time' is different than 'Time'.

The other issue being, as shown in the above example code, you need to create an object of the class in order to advance the time. In this case it's basically like making a new clock and setting it, you can't advance a clock that doesn't exist yet.
Image
A game about loneliness, chess, and fighting monsters with magic

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Trouble with Time/Day Class

#4 Post by hell_oh_world »

Well the code doesn't recognize as you referenced a time variable without even creating it. You should first create an instance of the class. In your case it should be `default time = Time(1, 1)`, should be the same level of indentation as your labels and screen. If you'll ever stumble into using python libs you better avoid using reserved names such as time. But since you didn't import it it's safe to use anyways. I wouldn't also do the creation of instance directly inside the init python block like one of the answers here did, not that it's bad, but because it's not an appropriate thing to do in your case because youre modifying the value of the time object. If you'll do that you'll have problems with saves and rollback since the data for the time object wouldn't be saved because it's literally equal to defining a variable, defining and defaulting a variable are two different things. The former marks the variable as constant so the game doesn't save defined variables such as `define a = 1`, the latter, on the other hand, gets saved like what i did in the example.

BM90
Newbie
Posts: 15
Joined: Sat Mar 31, 2018 2:48 am
Contact:

Re: Trouble with Time/Day Class

#5 Post by BM90 »

Thanks for the answers. Unfortunately, while it's allowed me to get the advance time button working, it doesn't appear that the tod and day values are being saved. I tried placing "clock = Clock(1,1)" and "default clock = Clock(1,1)" just about everywhere in the code and nothing seems to work. I've been doing a lot of forum searches about this problem and I'm starting to get the impression that what I'm trying to accomplish can't be done. It seemed like a simple enough concept, but I'm new to using classes, so maybe I've misunderstood their capabilities.

Code: Select all

init python:
    class Clock(object):
        def __init__(self, tod, day):
            self.tod = tod
            self.day = day

        def adv_clock(self):
            self.tod += 1
            if self.tod > 4:
                self.tod = 1
                self.day += 1
                if self.day > 7:
                    self.day = 1

default clock = Clock(1,1)

label start:
    jump test_label

label test_label:
    call screen test_screen
    return

screen test_screen:
    text "{b}[clock.tod]{/b}" xalign 0.5 yalign 0.45
    text "{b}[clock.day]{/b}" xalign 0.5 yalign 0.55
    textbutton "{b}ADVANCE TIME{/b}":
        xalign 0.5 
        yalign 0.3
        action Function(clock.adv_clock)

User avatar
Autumnotopia
Regular
Posts: 59
Joined: Sun May 19, 2019 8:53 pm
Completed: Making Friends
Tumblr: autumnotopiadev
Github: autumngreenley
itch: autumnotopia
Contact:

Re: Trouble with Time/Day Class

#6 Post by Autumnotopia »

I don't have anything to test compile on right now, so forgive me if this is wrong. I think you should be just setting your clock in a Python statement on (for example) the first line of your start block, so like '$clock = Clock(1,1)' (including the dollar sign Python indicator)

You need to be doing that sort of manipulation in Python I'm pretty sure, so you don't want to use the 'default' keyword.

I'm also not sure but I think that between the init block and the start block doesn't seem like it would do what you want (but I'm not super clear on how renpy handles it)

Classes are absolutely capable of what you're looking for haha, just something you'll get used to working with in time
Image
A game about loneliness, chess, and fighting monsters with magic

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Trouble with Time/Day Class

#7 Post by hell_oh_world »

It does get saved, tried your code and I see what you mean, not really familiar with calls but I guess even though you save at some point like after where you clicked the advance time and the value changes, the game saves from the point before the call happens so it starts from the beginning. if you'll try putting some dialogues and saving and calling the screen again you'll see that the value is saved.

Code: Select all

screen test_screen:
    vbox:
      align (0.5, 0.5)
      text "{b}[clock.tod]{/b}"
      text "{b}[clock.day]{/b}"
      textbutton "{b}ADVANCE TIME{/b}":
          action Function(clock.adv_clock)
      textbutton "CONTINUE THE GAME" action Return()

label test_label:
    call screen test_screen # advance the time then click the continue game button and save below...
    "d1"
    "d2"
    "d3"
    # save when d3 appears then load that save and continue the game...
    "d4"
    call screen test_screen # should display the saved values right
    return
so on a short note, renpy handles calls differently. if you try to save while a screen is called, the game saves from the point before the call happens, if that makes sense. and hopefully, other knowledgeable experts here can explain how renpy handle saves while on call.

you can also try to define the variable if you're skeptical about what I said about defining and defaulting them, if you save at some point and load the save you'll realize that the values are getting saved even though they're defined, well... they're not, it's an illusion though. the changed value is the very recent value of the variable and the variable still exists while the game is running so it looks like the value is saved, but if you'll close the game, the variables cease to exist, so once you run the game again and load the same save file you'll see that the values will default to the value that you set when you defined them.

BM90
Newbie
Posts: 15
Joined: Sat Mar 31, 2018 2:48 am
Contact:

Re: Trouble with Time/Day Class

#8 Post by BM90 »

Thank you all for the help. I never would've figured out that variables weren't updating because it saves at the point when the screen is called. I updated the button to include a jump back to the label the screen originates from, basically a refresh, and everything worked fine from there. This was just a test to see if I could get the basics working, so I'm glad I understand it now. In my actual game the change in time should always involve a jump to a new label anyway, so this won't present a problem.

Code: Select all

screen test_screen:
    text "{b}[clock.tod]{/b}" xalign 0.5 yalign 0.45
    text "{b}[clock.day]{/b}" xalign 0.5 yalign 0.55
    textbutton "{b}ADVANCE TIME{/b}":
        xalign 0.5 
        yalign 0.3
        action [Function(clock.adv_clock), Jump("test_label")]

Post Reply

Who is online

Users browsing this forum: munni