Non-Negative Variables
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.
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.
Non-Negative Variables
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.
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."
- 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
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
Then you can use it like
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....
$ 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)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 0Also "value" is now a funny word, after repeating it so often....
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)
Re: Non-Negative Variables
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.
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."
- 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
if you don't want to write a whole class for it, just write a custom subtract function:
then anytime you needed to do a subtraction that won't go past 0, just type:
restrictedSubtract(xyz, 5)
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)
restrictedSubtract(xyz, 5)
The more you know
- 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
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 ;)
Who is online
Users browsing this forum: Google [Bot], _ticlock_

