Why isn't the variable updated?

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
Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Why isn't the variable updated?

#1 Post by Ingword »

Hi! Tell me, please, why in this code I do not update the value of $ today?

I set the variables:

Code: Select all

   $ day = 4
   $ week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
   $ today = week[(day)]
This code put in script.rpy:

Code: Select all

init -1 python:
   def tik():
       global hours
       global day
       hours += 1
       if hours > 23:
           day += 1
           hours = 0
       return
This code put in screen.rpys:

Code: Select all

label info:
   screen home:
       fixed:
           text "[today]" xalign 0.5 yalign 0.10
   call screen home

Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Re: Why isn't the variable updated?

#2 Post by Ingword »

I found a solution, but now there was another question: why the changed values are not stored in memory?

If you save the game and load, then the day and time values will return to the original, and any conditions are not checked.

Code: Select all

init -1 python:
    
    def clocktick():
        global hours
        global day
        hours += 1
        if hours > 23:
            hours = 0
            day += 1
        return

    day = 4
    hours = 18
    daynames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    def weekday():
        return daynames[(day%7)]

Code: Select all

label room:
    scene room
    show bg room
    if day == 5:
        "It's saturday!"
    call home
But... It doesn't work.

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: Why isn't the variable updated?

#3 Post by trooper6 »

Current best practices, especially to ensure the proper functioning of save and rollback is to declare your variable using default. Also you forgot the global declaration in your weekday function.

So:

Code: Select all

default day = 4
default hours = 18
default daynames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

init -1 python: 
    def clocktick():
        global hours
        global day
        hours += 1
        if hours > 23:
            hours = 0
            day += 1
        return

    def weekday():
        global daynames
        return daynames[(day%7)]
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

Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Re: Why isn't the variable updated?

#4 Post by Ingword »

trooper6, thanks for the answer! In general, everything works as it should, except for one thing: while saving and later loading, the values are reset to the initial ones that were set as default :(

Except for one case, if there is a code like:

Code: Select all

label my_room:
    scene my_room
    show bg my_room
    if hours == 21:
        #something
    call home
Then while saving and later loading, the value of the variable "hours" 21, even i saving at 23:00...

Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Re: Why isn't the variable updated?

#5 Post by Ingword »

Now i have:

Code: Select all

default day = 0
default hours = 0
default daynames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

init -1 python:

    def clocktick():
        global hours
        global day
        hours += 1
        if hours > 23:
            hours = 0
            day += 1
        return

    def weekday():
        global daynames
        return daynames[(day%7)]


label start:

    $ day = 4
    $ hours = 18
    $ daynames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

label my_room:
    scene my_room
    show bg my_room
    if hours == 21:
        "It's 21 o'clock"
    call bar #it's icon's bar

Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Re: Why isn't the variable updated?

#6 Post by Ingword »

I found a regularity: if my function clocktick() returns True or any other value, then the story starts moving forward and the save work normally.

But what needs to be returned in order to just twist the time ...

Ingword
Newbie
Posts: 20
Joined: Tue May 23, 2017 2:16 pm
Contact:

Re: Why isn't the variable updated?

#7 Post by Ingword »

Half the night without sleep and I found the answers to my questions ^^

Post Reply

Who is online

Users browsing this forum: snotwurm