Timer Stops Variable From Working

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
goldenphoenyyx
Newbie
Posts: 12
Joined: Thu Jun 20, 2019 6:47 pm
Contact:

Timer Stops Variable From Working

#1 Post by goldenphoenyyx »

Hi! I have a timer that when it goes gets to zero, it lowers a variable number. However that stops the variable from working. It continues to decrease when the timer resets and gets to zero again, but it can't be affected by anything else if I try to increase or decrease it through the script. Other variables are working fine.

Code: Select all

screen countdown:
    timer 1 repeat True action If(time > 0, true=SetVariable('time', time - 1), false=SetVariable('time', time + 5))
    
screen mainHUD:
    if time == 0 and hungertaken == False:
        $ hungerbar -= 5
        $ hungertaken = True
    if time == 5:
        $ hungertaken = False
if I remove the '$ hungerbar -= 5' line, it starts working but the variable doesn't decrease with the timer. Am I going about this wrong?

User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Timer Stops Variable From Working

#2 Post by isobellesophia »

Have you ever hide the timer screen in your code?
I am a friendly user, please respect and have a good day.


Image

Image


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: Timer Stops Variable From Working

#3 Post by trooper6 »

This is really convoluted. So it looks like all you really want to do is have a number decrease by 5 every 5 seconds. So you can just do that directly.
Here is some sample code. Make a new project with just this code and see how it works.

Code: Select all

screen hungerbar():
    frame:
        has vbox
        bar value hunger range 100
        text "Hunger: [hunger]"

    timer 5.0 repeat True action SetVariable("hunger", hunger-5)

default hunger = 50

# The game starts here.
label start():
    show screen hungerbar()
    "Let's test this thing."
    $hunger += 25
    "Adding 25 hunger."
    $hunger -= 15
    "Removing 15 hunger."
    "Test over."

    return

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

goldenphoenyyx
Newbie
Posts: 12
Joined: Thu Jun 20, 2019 6:47 pm
Contact:

Re: Timer Stops Variable From Working

#4 Post by goldenphoenyyx »

trooper6 wrote: Fri Dec 20, 2019 5:06 am This is really convoluted. So it looks like all you really want to do is have a number decrease by 5 every 5 seconds. So you can just do that directly.
Here is some sample code. Make a new project with just this code and see how it works.

Code: Select all

screen hungerbar():
    frame:
        has vbox
        bar value hunger range 100
        text "Hunger: [hunger]"

    timer 5.0 repeat True action SetVariable("hunger", hunger-5)

default hunger = 50

# The game starts here.
label start():
    show screen hungerbar()
    "Let's test this thing."
    $hunger += 25
    "Adding 25 hunger."
    $hunger -= 15
    "Removing 15 hunger."
    "Test over."

    return

Thank you!! This works. I tend to overthink programming. If you have the time, would you mind looking at the new timer I tried with your solution?

Code: Select all

    timer 5.0 repeat True action SetVariable("hungerbar", hungerbar - 10), If(hungerbar <= 0, true=SetVariable("hungerbar", 0))
    timer 0.1 repeat True action If(hungerbar > 100, SetVariable("hungerbar", 100), false=Show('hungerbar_timer'))
The minimum on the first timer works but I was wondering if the second one would cause problems in the future as my 'solution' to having the variable have a maximum.

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: Timer Stops Variable From Working

#5 Post by Per K Grok »

goldenphoenyyx wrote: Fri Dec 20, 2019 12:06 am Hi! I have a timer that when it goes gets to zero, it lowers a variable number. However that stops the variable from working. It continues to decrease when the timer resets and gets to zero again, but it can't be affected by anything else if I try to increase or decrease it through the script. Other variables are working fine.

Code: Select all

screen countdown:
    timer 1 repeat True action If(time > 0, true=SetVariable('time', time - 1), false=SetVariable('time', time + 5))
    
screen mainHUD:
    if time == 0 and hungertaken == False:
        $ hungerbar -= 5
        $ hungertaken = True
    if time == 5:
        $ hungertaken = False
if I remove the '$ hungerbar -= 5' line, it starts working but the variable doesn't decrease with the timer. Am I going about this wrong?

The problem with your original solution is global vs local variables. SetVariable will change the global variable, but when you do;
$ hungerbar -=5
in screen mainHUD that will create a local variable used only in that screen. Use the code below to see how you have two versions of hungerbar.

The global one will be counted up using mousebutton-clicks and the local one will be counted down with the timer.

The variable in mainHUD will be the global one until the first time 'time' becomes '0'. Then the local version is created. So if you start mouseclicking before the timer goes down to 0 the first time, both hungerbar values will go up.

Code: Select all

default time=5
default hungerbar=5
default hungertaken=False

screen countdown:
    text str(time) + ":" + str(hungerbar) + " global"
    timer 1 repeat True action If(time > 0, true=SetVariable('time', time - 1), false=SetVariable('time', time + 5))

screen mainHUD:
    text str(time) + ":" + str(hungerbar) + " local" ypos 20
    if time == 0 and hungertaken == False:
        $ hungerbar -= 5
        $ hungertaken = True
    if time == 5:
        $ hungertaken = False

label start:
    show screen countdown
    show screen mainHUD

label hungerup:
    pause
    $ hungerbar += 10
    "Hello [hungerbar]"
    jump hungerup

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: Timer Stops Variable From Working

#6 Post by trooper6 »

goldenphoenyyx wrote: Fri Dec 20, 2019 4:18 pm Thank you!! This works. I tend to overthink programming. If you have the time, would you mind looking at the new timer I tried with your solution?

Code: Select all

    timer 5.0 repeat True action SetVariable("hungerbar", hungerbar - 10), If(hungerbar <= 0, true=SetVariable("hungerbar", 0))
    timer 0.1 repeat True action If(hungerbar > 100, SetVariable("hungerbar", 100), false=Show('hungerbar_timer'))
The minimum on the first timer works but I was wondering if the second one would cause problems in the future as my 'solution' to having the variable have a maximum.
This is not how I would do this.
There doesn't need to be a timer that is checking every second to see if the variable goes above 100 or below 0...that is a lot of computing for something that may not come up often.

There are a bunch of different ways to ensure that your variable doesn't go above or below a certain range. Here is one that I think is pretty elegant.
Rather than a variable, you create a class with just the variable you want. Then you create properties for that variable. So anytime you add or subtract from that variable, the class will check to make sure you aren't above or below the range you want. So check out this code:

Code: Select all

screen hungerbar():
    frame:
        has vbox
        bar value hunger.amount range 100
        text "Hunger: [hunger.amount]"

    timer 5.0 repeat True action SetVariable("hunger.amount", hunger.amount-5)

init python:
    class Hunger(object):
        def __init__(self, value = 0):
            self._amount = value

        @property #Getter
        def amount(self):
            return self._amount

        @amount.setter
        def amount(self, value):
            self._amount = value
            if self._amount < 0:
                self._amount = 0
            if self._amount > 100:
                self._amount = 100

default hunger = Hunger(50)

# The game starts here.
label start():
    show screen hungerbar()
    "Let's test this thing."
    $hunger.amount += 25
    "Adding 25 hunger."
    $hunger.amount -= 15
    "Removing 15 hunger."
    "Now to test the extremes."
    $hunger.amount += 150
    "Adding 150 hunger, it should leave us at 100."
    $hunger.amount -= 200
    "Subtracting 200, it should leave us at 0"
    $hunger.amount += 25
    "Adding another 25."
    "Test over."

    return
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

Post Reply

Who is online

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