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.
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.
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.
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?)
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.
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.
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.
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.
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?
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:
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?)