Non-Negative 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
User avatar
Aedin
Regular
Posts: 137
Joined: Fri Sep 07, 2012 11:53 pm
Contact:

Non-Negative Variables

#1 Post by Aedin » Sun Oct 21, 2012 12:51 am

I've been struggling for a full week now, trying to get this code to work. It's late, and I'm tired, so it's probably really obvious overlooking it.

Is there a way to keep a variable from going negative without having to type
$ xyz -= 5
if xyz <= 0:
$ x = 0
after every single point subtraction. Something that I can just plug in once and not have to worry about it again? I've been trying to find or figure out a godcode or something, but I've yet to get anything to work.
"There’s no shame in pleasure, Mr. Gray. You see, man just wants to be happy, but society wants him to be good. And when he’s good, man is rarely happy, and when he’s happy he is always good."

User avatar
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Re: Non-Negative Variables

#2 Post by Ayutac » Sun Oct 21, 2012 1:36 am

If I understood it right, the third line is supposed to be
$ xyz = 0

and no, there isn't such a way in the first place. But if you're clever, you write a class like

Code: Select all

python:
    class Unsigned:
        def __init__(self, value = 0):
            self.setValue(value)

        def getValue(self):
            return self.__value

        def setValue(self, value):
            if value < 0:
                self.__value = 0
            else:
                self.__value = value

        Value = property(getValue, setValue)
Then you can use it like

Code: Select all

python:
    xyz = Unsigned() ## starts with 0
    xyz.Value -= 5 ## after this schould still be 0
    xyz = Unsigned(3) ## starts with 3
    xyz.Value = -3 ## after this should be 0
Being a class method, the check is indeed called everytime, but you only have to write it one time. On the other hand you are forced to use classes, but being a programmer myself I don't see this as a bad thing :-)

Also "value" is now a funny word, after repeating it so often....
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

User avatar
Aedin
Regular
Posts: 137
Joined: Fri Sep 07, 2012 11:53 pm
Contact:

Re: Non-Negative Variables

#3 Post by Aedin » Sun Oct 21, 2012 1:54 am

Yeah, sorry about that. It should be xyz, but like I said, I'm really tired and out of it ugghhhh.

Well, poo. Godcodes are so much easier. Anyhow, thank you for your help. I've yet to get a good grasp on classes, but I'll play around with them and how they work.
"There’s no shame in pleasure, Mr. Gray. You see, man just wants to be happy, but society wants him to be good. And when he’s good, man is rarely happy, and when he’s happy he is always good."

User avatar
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: Non-Negative Variables

#4 Post by pucedragonlord » Mon Oct 22, 2012 6:36 pm

if you don't want to write a whole class for it, just write a custom subtract function:

Code: Select all

python:
    def restrictedSubtract(input, subtractBy):  #or just name it something like resSub for shorthand
        if (input -= subtractBy) < 0:
            return 0
        else:
            return (input -= subtractBy)
then anytime you needed to do a subtraction that won't go past 0, just type:

restrictedSubtract(xyz, 5)
The more you know

User avatar
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Re: Non-Negative Variables

#5 Post by Ayutac » Tue Oct 23, 2012 6:02 am

totally forgot python can do something like that, I'm still thinking much in classes from Java xD
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

Post Reply

Who is online

Users browsing this forum: Google [Bot], _ticlock_