Global variables - Set max and min values

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.
Message
Author
cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Global variables - Set max and min values

#1 Post by cardium »

Hello everyone,

I am very new on renpy and python so excuse me if this question is a little newbie:
I want to set a variable, for example the energy on a dating sim kind of game. In the script.rpy file I set:

Code: Select all

label start:
    $ energy = 80

label sleep:
    $ energy = energy + 80
    # energy now is 160

label die:
    $ energy = energy -170
    # energy now is -10
This will produce an energy of 160 and -10 respectively. I want to limit the energy variable between 0 and 100.
Probably there is an easy solution like "energy(value,setmax,setmin)" but I can't seem to find the command in the documentation.

Thank you very much!

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Global variables - Set max and min values

#2 Post by Alex »


cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#3 Post by cardium »

Thank you for your quick response! I have already read this but maybe I have missed the solution.
I will read it more carefully and get back to you!

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Global variables - Set max and min values

#4 Post by philat »

There isn't a built-in solution afaik, but you can define a python function for that yourself pretty simply. Just as a barebones structure that you can build off of (not guaranteeing that it will work as is, I haven't tested it in Renpy and sometimes Renpy doesn't do exactly what I expect it to with python):

Code: Select all

init python:
    def change_stat(stat, amount):
        stat += amount
        if stat > 100:
            stat = 100
        elif stat < 0:
            stat = 0
        return stat

#script example

$ energy = 0
$ energy = change_stat(energy, 150)
$ energy = change_stat(energy, -150)
If you have a lot of variables with different max/mins, you could structure a whole class and use methods, etc., but depending on the complexity of your game and your level of comfort with python, you could just go for something simpler.

You could also look to the DSE: http://www.renpy.org/wiki/renpy/DSE

Some of the DSE code is outdated, but it's still a useful framework if it fits what you're trying to do. It also has a stat system in place that could be a good reference if you're trying to build your own.
Last edited by philat on Tue Nov 25, 2014 5:45 pm, edited 1 time in total.

cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#5 Post by cardium »

I can't thank you enough philat! You saved me a lot of time with a very quick and easy solution!! It works like a charm!
This kind of code can help in other issues I had, so I really appreciate your help! Also the DSE tutorial will be helpful!
Cheers!

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Global variables - Set max and min values

#6 Post by PyTom »

There's actually an easier way to do this.

Code: Select all

init python:
     def limit_stats():
           try:
               if store.energy < 0:
                    store.energy = 0
               elif store.energy > 100:
                    store.energy = 100
          except:
               pass

    config.python_callbacks.append(limit_stats)          
This runs limit_stats after each python block, letting you run your original code and have the variable limited - at the end of each python block - to the [0, 100] range.

The pokémon exception handling (gotta catch 'em all) is there to take care of startup, when energy is not yet defined.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#7 Post by cardium »

As I was testing philat answer (I know each a little ugly but it works :P ):

Code: Select all

init python:
    def change_energy(stat,function,amount):
        if function == "+":
            stat =+ amount
            if stat > 100:
                stat = 100
            elif stat < 0:
                stat = 0
            return stat
        elif function == "-":
            stat =- amount
            if stat > 100:
                stat = 100
            elif stat < 0:
                stat = 0
            return stat
I saw PyTom answer. Just tested it and it works flawless! I think I am gone use this solution and build my script with that!
Thank you everyone I learned a lot in just a few minutes because of you!

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Global variables - Set max and min values

#8 Post by Saltome »

Umm... why do you need to limit the energy?

Code: Select all

menu:
     "Choose action!"
     "Get a date.":
          if energy>20:
               $energy-=20
               "Went on a date."
     "Sleep.":
          $energy=100
          "Slept in bed."
Unless you wanna be able to increase the max health during the game.
then you need to define a variable that holds the maximum energy and then set energy to the variable instead.
Not that I see anything inherently wrong with having a bed which only recovers a set amount of energy, instead of recovering completely.

Anyhow, I can offer another solution.

Code: Select all

$energy =max(min(energy, 100), 0)
But of course hard coding values like that is considered bad practice soo..:

Code: Select all

$energy =max(min(energy, max_energy), min_energy)
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#9 Post by cardium »

Yeah this is what I initially have done thank you. But I want to change and check my value in every action, so PyTom solution was more appropriate for my problem. Now I am trying to find how to Jump after the if in that code for example:

Code: Select all

init python:
    def limit_stats():
        try:
            if store.energy < 0:
                store.energy = 0
                renpy.jump("need_sleep")
            elif store.energy > 100:
                store.energy = 100
renpy.jump(label) or renpy.jump_out_of_context(label) are not working, but I will think of something. Thanks again!

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Global variables - Set max and min values

#10 Post by philat »

Actually for PyTom's code, I don't believe there is any need to jump. It will run after each python block (e.g. when you change the variable energy) and then run the next line of your script. So if you simply write the script without regard for the limiting function, it should work fine.

cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#11 Post by cardium »

Yes I know it works, I have just tried it.

The thing is that I will need to create an if statement -> energy == 0 then jump to label need_sleep for every action in the game (which is a lot!).
If I could do it inside the limit_stats python code it would be better I think.

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Global variables - Set max and min values

#12 Post by Saltome »

I suppose my post got completely ignored...
Anyway, I'm getting the feeling that you are trying the wrong approach.
So let me recommend a little tutorial to you.
https://www.youtube.com/watch?v=4Mf0h3H ... 17E1E5C0DA

As a matter of fact, why don't you just open the script file of The Question, the game which comes with renpy. Then look how it does things. I guess that's the best way you can learn about how to structure the code.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

cardium
Newbie
Posts: 12
Joined: Mon Nov 24, 2014 2:41 pm
Contact:

Re: Global variables - Set max and min values

#13 Post by cardium »

Mmmm Saltome I did not ignored your comment, this is really not the case. I am really sorry if I have upset you, by bad.
I was just searching to solve for a very specific problem and PyTom provided me the best solution. Your solution was really great but I needed write an if statement in each action because I use a lot of screens and jumps.

Thanks also for the link of the tutorial it may come in handy next time.

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: Global variables - Set max and min values

#14 Post by trooper6 »

PyTom, in your code, why do you write "store.energy" rather than just "energy"? What is the practical difference between those two things? Are they not the same thing said in two different ways?
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

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Global variables - Set max and min values

#15 Post by PyTom »

Code: Select all

     def limit_stats():
           global energy        

           try:
               if energy < 0:
                    energy = 0
               elif energy > 100:
                    energy = 100
          except:
               pass
Would work too - there isn't a deep reason for picking one over the other.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: No registered users