Set Max and Min for variables

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
lindsay-jb
Regular
Posts: 61
Joined: Tue Aug 25, 2020 1:05 am
Contact:

Set Max and Min for variables

#1 Post by lindsay-jb » Wed Oct 20, 2021 5:11 pm

Hello! I'm trying to set 100 as the max and 0 for the min of some variables I have. I found some topics on here that helped me make this code:

Code: Select all

default rowan_nerve = 50

init python:
    def limit_nerve_rowan():
        try:
            if rowan_nerve < 0:
                rowan_nerve = 0
            elif rowan_nerve > 100:
                rowan_nerve = 100
        except:
            pass

    config.python_callbacks.append(limit_nerve_rowan)
However, while the game will play it doesn't actually limit anything. It'll go all the way down to -150 lol. I'm thinking that it might be because I need to set this as a global variable? If that's the case, that would make sense. However I'm not exactly certain how I would go about setting this all up as a global variable. As of now, it's just set up as you can see in the code. Any help would be appreciated!

User avatar
HEXdidnt
Regular
Posts: 61
Joined: Tue Aug 11, 2020 1:25 pm
Projects: A Night at the Office, Embracing Christmas, The Masquerade Benefit
Deviantart: HEXdidnt
Location: Harrow, London, UK
Contact:

Re: Set Max and Min for variables

#2 Post by HEXdidnt » Wed Oct 20, 2021 5:37 pm

I can see a couple of potential reasons... I think when querying a variable with an if, it needs to be formatted as <= or >=, not just < or >. Then, setting a variable to a new value, it has to be preceeded with a $, so:

Code: Select all

if rowan_nerve <= 0:
	$ rowan_nerve = 0
elif rowan_nerve >= 100:
	$ rowan_nerve = 100
That's probably not all of it, though - just the bit that keeps tripping me up :wink:
As ever, dropping litter in the zen garden of your mind...

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

Re: Set Max and Min for variables

#3 Post by Alex » Wed Oct 20, 2021 5:50 pm

lindsay-jb wrote:
Wed Oct 20, 2021 5:11 pm
... I'm thinking that it might be because I need to set this as a global variable? If that's the case, that would make sense. However I'm not exactly certain how I would go about setting this all up as a global variable....
Try

Code: Select all

default rowan_nerve = 50

init python:
    def limit_nerve_rowan():
        global rowan_nerve # <---
        try:
            if rowan_nerve < 0:
                rowan_nerve = 0
            elif rowan_nerve > 100:
                rowan_nerve = 100
        except:
            pass

    config.python_callbacks.append(limit_nerve_rowan)

lindsay-jb
Regular
Posts: 61
Joined: Tue Aug 25, 2020 1:05 am
Contact:

Re: Set Max and Min for variables

#4 Post by lindsay-jb » Wed Oct 20, 2021 6:12 pm

Thanks so much for the responses! Alex, that worked perfectly. I was just missing defining the variable as a global nerve and it was simpler than I realized haha.

Post Reply

Who is online

Users browsing this forum: enaielei, Google [Bot]