A question involving stat variables and changing backgrounds
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.
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
A question involving stat variables and changing backgrounds
At this point, I do not have an artist for my game projects, and I'm really just trying a lot of things out right now. I've been checking the Ren'py wiki and there is one article thats helped me a lot.
http://www.renpy.org/wiki/renpy/doc/tut ... er_Choices
Okay, I have a few basic questions:
1: Do the variables register as a negative if you detract from them? or do they just stop at zero?
2: Is it possible to put a cap or max on variables? perhaps a lowest and highest they can go?
3: In my game I want a stat to show how nice of a person you are, and I've created backgrounds for everything from:
Good -- Good/Neutral -- Neutral -- Neutral/Bad -- Bad
Given this, I want to have a stat govern this that i can change. My major question would be: How would I go about setting the background to change depending on these stats? Would I have to repeat the code to check every time after a choice that may change it? Or is it possible to make a code that will automatically change it depending on this stat?
Help will be much appreciated! Thanks!
http://www.renpy.org/wiki/renpy/doc/tut ... er_Choices
Okay, I have a few basic questions:
1: Do the variables register as a negative if you detract from them? or do they just stop at zero?
2: Is it possible to put a cap or max on variables? perhaps a lowest and highest they can go?
3: In my game I want a stat to show how nice of a person you are, and I've created backgrounds for everything from:
Good -- Good/Neutral -- Neutral -- Neutral/Bad -- Bad
Given this, I want to have a stat govern this that i can change. My major question would be: How would I go about setting the background to change depending on these stats? Would I have to repeat the code to check every time after a choice that may change it? Or is it possible to make a code that will automatically change it depending on this stat?
Help will be much appreciated! Thanks!
-
- Miko-Class Veteran
- Posts: 829
- Joined: Sun Feb 20, 2005 8:45 pm
- Projects: NaNoRenO 2016, Ren'Py tutorial series
- Location: USA
- Contact:
Re: A question involving stat variables and changing backgro
I can answer the first two for certain:
1: Yes, an operation that would reduce a number to below 0 will return the correct number below 0. 5 - 9 will return -4, for example.
2: Caps and ranges for variables can be enforced with a few basic functions. the functions are min(a, b[, ...]) and max(a, b[, ...]). the min function returns the lowest value out of those you supply as parameters (I.E. min(50, 100) returns 50), and the max function, likewise, returns the highest value out of those you supply as parameters. Variables on their own, however, are not capped, it is up to you to make sure the variables are clamped within the bounds you want them to be.
as for #3, I don't know how to go about it.
1: Yes, an operation that would reduce a number to below 0 will return the correct number below 0. 5 - 9 will return -4, for example.
2: Caps and ranges for variables can be enforced with a few basic functions. the functions are min(a, b[, ...]) and max(a, b[, ...]). the min function returns the lowest value out of those you supply as parameters (I.E. min(50, 100) returns 50), and the max function, likewise, returns the highest value out of those you supply as parameters. Variables on their own, however, are not capped, it is up to you to make sure the variables are clamped within the bounds you want them to be.
as for #3, I don't know how to go about it.
~Kitsune Zeta
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
Okay... that makes a lot of sense. I just wanted to make sure that they could go into the negative, because for #3 I was wondering if I'd have to do something like, 1-10, with 5 being neutral, or if I would do -10/10 and have 0 be neutral.
I'm a noob at coding though, so... I don't fully know how I would implement what you just told me about maxes and min's into my coding... could you give me an example? Pretend the variable is
$ a = 0
and I want the min to be -10 and the max to be 10. How would I go about that? And also, would it be included below it in the init: section?
EDIT: I just saw in the guide I got this from that variables like this need to go in after the label start: section, so... the caps would too?
I'm a noob at coding though, so... I don't fully know how I would implement what you just told me about maxes and min's into my coding... could you give me an example? Pretend the variable is
$ a = 0
and I want the min to be -10 and the max to be 10. How would I go about that? And also, would it be included below it in the init: section?
EDIT: I just saw in the guide I got this from that variables like this need to go in after the label start: section, so... the caps would too?
-
- Miko-Class Veteran
- Posts: 829
- Joined: Sun Feb 20, 2005 8:45 pm
- Projects: NaNoRenO 2016, Ren'Py tutorial series
- Location: USA
- Contact:
Re: A question involving stat variables and changing backgro
meh, I need to refresh myself on some stuff anyways, so
also, note that you don't have to place the variable change statement right after the start label. instead, put it at the spot in the script where you want/need it to change. same deal for just about everything else.
as for capping the variable, here's what I would do for your example:
basically, this brings a to within the range of -10 to 10. the max(a, 10) will return a unless it is less than -10, in which case it will return -10, while the min(a, 10) will return a unless it is above 10, in which case it will return 10.
so if a is 5, max(5, -10) returns 5, and thus, min(5, 10) returns 5 as well.
if a is 15, max(15, -10) returns 15, but min(15, 10) returns 10 (instead of 15).
likewise, if a is -15, max(-15, -10) returns -10 (instead of -15), and min(-10, 10) returns -10.
Code: Select all
init python:
Date_N = 1 # declare variable. Remember your capitalization, variables are case-sensitive.
# etc
label start:
"Variable Date_N is currently set to %(Date_N)d"
$ Date_N -= 2 # Outside of a python block, you must use $ for each python statement.
"Variable Date_N was decreased by two, and is now %(Date_N)d" # should be -1 now.
as for capping the variable, here's what I would do for your example:
Code: Select all
$ a = min(max(a, -10), 10)
so if a is 5, max(5, -10) returns 5, and thus, min(5, 10) returns 5 as well.
if a is 15, max(15, -10) returns 15, but min(15, 10) returns 10 (instead of 15).
likewise, if a is -15, max(-15, -10) returns -10 (instead of -15), and min(-10, 10) returns -10.
~Kitsune Zeta
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
And.... a final clarification. What do you mean by return? I'm a bit confused on that...
Basically, that code will make it so that if say 5 is added to 9 and the limit is 10, it will add to 10 and stop and thats it. Same with the opposite direction, right?
Basically, that code will make it so that if say 5 is added to 9 and the limit is 10, it will add to 10 and stop and thats it. Same with the opposite direction, right?
- SleepKirby
- Veteran
- Posts: 255
- Joined: Mon Aug 09, 2010 10:02 pm
- Projects: Eastern Starlight Romance, Touhou Mecha
- Organization: Dai-Sukima Dan
- Location: California, USA
- Contact:
Re: A question involving stat variables and changing backgro
"x is returned" basically means that x is the result.
Also, you can use a Python class to automatically enforce min/max on your stats. This way, checking against the min and max wouldn't be any different from what Megaman Z said, except the checks would be done automatically whenever the stat values are changed.
DISCLAIMER: I haven't tested this, but hopefully this gets the general idea across. If you ever use this code, though, then go ahead and mention any errors. >_> (I'm planning on using something like this in my game.)
Or using Megaman Z's idea, the last two lines could be "elif self.min:" then "self.value = max(self.value, self.min)". That also works.
Note that "elif self.min:" is shorthand for "elif self.min is not None:".
Creating your stat variables:
(The way the code is written, all the parameters startingvalue, min, and max are optional)
Changing your stats:
Whenever you change the value like this, it's checked against the min and max.
Checking your stats:
About changing backgrounds: that sounds like something you could do with ConditionSwitch, maybe.
Also, you can use a Python class to automatically enforce min/max on your stats. This way, checking against the min and max wouldn't be any different from what Megaman Z said, except the checks would be done automatically whenever the stat values are changed.
DISCLAIMER: I haven't tested this, but hopefully this gets the general idea across. If you ever use this code, though, then go ahead and mention any errors. >_> (I'm planning on using something like this in my game.)
Code: Select all
init python:
class My_stat:
# This code runs when you create the stat.
# startingvalue, min, and max are optional parameters. startingvalue defaults to 0 if it's not given.
def __init__(self, startingvalue=0, min=None, max=None):
self.value = startingvalue
self.min = min
self.max = max
# up_amount may also be negative.
def up(self, up_amount):
self.value += up_amount
if self.max and (self.value > self.max):
self.value = self.max
elif self.min and (self.value < self.min):
self.value = self.min
Note that "elif self.min:" is shorthand for "elif self.min is not None:".
Creating your stat variables:
(The way the code is written, all the parameters startingvalue, min, and max are optional)
Code: Select all
agility = My_stat() # No max or min, starting value 0
magic = My_stat(startingvalue=0, min=-10, max=10)
charisma = My_stat(startingvalue=50, max=100) # No min
Whenever you change the value like this, it's checked against the min and max.
Code: Select all
charisma.up(1)
magic.up(-2)
Code: Select all
if agility.value > 5:
...
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
Code: Select all
init python:
class stat:
def __init__ (self, startingvalue=0, min =None, max =None):
self.value = startingvalue
self.min = min
self.max = max
def up(self, up_amount):
self.value += up_amount
if self.max and (self.value > self.max):
self.value = self.max
elif self.min and (self.value < self.min):
self.value = self.min
Code: Select all
On line 55 of C:\Users\Teresa\Desktop\Folders\Game Development\Prototype2/game/script.rpy: expected statement.
disposition.up(10)
^
On line 59 of C:\Users\Teresa\Desktop\Folders\Game Development\Prototype2/game/script.rpy: expected statement.
disposition.up(-30)
^
On line 63 of C:\Users\Teresa\Desktop\Folders\Game Development\Prototype2/game/script.rpy: expected statement.
disposition.up(100)
^
- SleepKirby
- Veteran
- Posts: 255
- Joined: Mon Aug 09, 2010 10:02 pm
- Projects: Eastern Starlight Romance, Touhou Mecha
- Organization: Dai-Sukima Dan
- Location: California, USA
- Contact:
Re: A question involving stat variables and changing backgro
Oh, right, you need a $ before each of those lines. They're Python lines, not standard Ren'Py lines.
Code: Select all
$ disposition.up(10)
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
I'm sorry, but an uncaught exception occurred.
TypeError: %d format: a number is required, not instance
While running game code:
- script at line 53 of C:\Users\Teresa\Desktop\Folders\Game Development\Prototype2/game/script.rpy
As you can see, another error occured. This is how I set up my stat, do you know why it isn't working?
And I put this above that, just in case you need to see it again or there's a problem in my code here...
TypeError: %d format: a number is required, not instance
While running game code:
- script at line 53 of C:\Users\Teresa\Desktop\Folders\Game Development\Prototype2/game/script.rpy
As you can see, another error occured. This is how I set up my stat, do you know why it isn't working?
Code: Select all
$ disposition = stat(startingvalue=0, min=-30, max=30)
Code: Select all
init python:
class stat:
def __init__ (self, startingvalue=0, min=None, max=None):
self.value = startingvalue
self.min = min
self.max = max
def up(self, up_amount):
self.value += up_amount
if self.max and (self.value > self.max):
self.value = self.max
elif self.min and (self.value < self.min):
self.value = self.min
- SleepKirby
- Veteran
- Posts: 255
- Joined: Mon Aug 09, 2010 10:02 pm
- Projects: Eastern Starlight Romance, Touhou Mecha
- Organization: Dai-Sukima Dan
- Location: California, USA
- Contact:
Re: A question involving stat variables and changing backgro
Which line is line 53?
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
Code: Select all
label start:
"Testing..."
"%(disposition)d"
$ disposition.up(10)
"%(disposition)d"
$ disposition.up(-30)
"%(disposition)d"
$ disposition.up(100)
"%(disposition)d"
scene bg opening
- SleepKirby
- Veteran
- Posts: 255
- Joined: Mon Aug 09, 2010 10:02 pm
- Projects: Eastern Starlight Romance, Touhou Mecha
- Organization: Dai-Sukima Dan
- Location: California, USA
- Contact:
Re: A question involving stat variables and changing backgro
Ah. Try
The "value" is where the actual number is stored.
Code: Select all
"%(disposition.value)d"
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
TypeError: %d format: a number is required, not unicode
Said not unicode, instead of not instance this time...
Said not unicode, instead of not instance this time...
- SleepKirby
- Veteran
- Posts: 255
- Joined: Mon Aug 09, 2010 10:02 pm
- Projects: Eastern Starlight Romance, Touhou Mecha
- Organization: Dai-Sukima Dan
- Location: California, USA
- Contact:
Re: A question involving stat variables and changing backgro
Er, oops. I didn't know that the %d thing couldn't take Python expressions...
Odd, I guess I never had to get around this problem before. Anyone know how?
...Well, actually, it would go like this...
...you'd need both lines every time you need to the display the value. I'm just kind of disappointed that it takes two lines of code to display it, but oh well.
Looks like a similar cookbook recipe needed the extra line of code too.
Odd, I guess I never had to get around this problem before. Anyone know how?
...Well, actually, it would go like this...
Code: Select all
dispositionValue = disposition.value # dispositionValue is just a dummy variable, so we can display the value.
"%(dispositionValue)d"
Looks like a similar cookbook recipe needed the extra line of code too.
-
- Newbie
- Posts: 23
- Joined: Sun Jan 16, 2011 5:43 pm
- Contact:
Re: A question involving stat variables and changing backgro
Okay! Thats fixed now! But...
new problem. The value isn't changing at all even with the whole
$ disposition.up(10)
code. Does that need to be changed too?
EDIT: No matter how I look at it, which code I look at... I can't think of why it isn't adding to the value...
new problem. The value isn't changing at all even with the whole
$ disposition.up(10)
code. Does that need to be changed too?
EDIT: No matter how I look at it, which code I look at... I can't think of why it isn't adding to the value...
Last edited by ProxyPawaa on Sat Jan 29, 2011 2:22 am, edited 1 time in total.
Who is online
Users browsing this forum: Majestic-12 [Bot]