A question involving stat variables and changing backgrounds

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
ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

A question involving stat variables and changing backgrounds

#1 Post by ProxyPawaa » Fri Jan 28, 2011 8:20 pm

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!

User avatar
Megaman Z
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

#2 Post by Megaman Z » Fri Jan 28, 2011 8:46 pm

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.
~Kitsune Zeta

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#3 Post by ProxyPawaa » Fri Jan 28, 2011 9:27 pm

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?

User avatar
Megaman Z
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

#4 Post by Megaman Z » Fri Jan 28, 2011 9:44 pm

meh, I need to refresh myself on some stuff anyways, so

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.
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:

Code: Select all

$ a = min(max(a, -10), 10)
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.
~Kitsune Zeta

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#5 Post by ProxyPawaa » Fri Jan 28, 2011 10:22 pm

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?

User avatar
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

#6 Post by SleepKirby » Fri Jan 28, 2011 11:15 pm

"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.)

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
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)

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
Changing your stats:
Whenever you change the value like this, it's checked against the min and max.

Code: Select all

    charisma.up(1)
    magic.up(-2)
Checking your stats:

Code: Select all

    if agility.value > 5:
        ...
About changing backgrounds: that sounds like something you could do with ConditionSwitch, maybe.

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#7 Post by ProxyPawaa » Fri Jan 28, 2011 11:41 pm

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
Okay, I used this code and got....

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)
                   ^
Did I do something wrong? or....

User avatar
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

#8 Post by SleepKirby » Sat Jan 29, 2011 12:27 am

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)

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#9 Post by ProxyPawaa » Sat Jan 29, 2011 12:33 am

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?

Code: Select all

    $ disposition = stat(startingvalue=0, min=-30, max=30)
And I put this above that, just in case you need to see it again or there's a problem in my code here...

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

User avatar
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

#10 Post by SleepKirby » Sat Jan 29, 2011 12:38 am

Which line is line 53?

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#11 Post by ProxyPawaa » Sat Jan 29, 2011 12:45 am

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
"%(disposition)d" is the line its talking about.

User avatar
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

#12 Post by SleepKirby » Sat Jan 29, 2011 12:56 am

Ah. Try

Code: Select all

    "%(disposition.value)d"
The "value" is where the actual number is stored.

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#13 Post by ProxyPawaa » Sat Jan 29, 2011 12:58 am

TypeError: %d format: a number is required, not unicode

Said not unicode, instead of not instance this time...

User avatar
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

#14 Post by SleepKirby » Sat Jan 29, 2011 1:35 am

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...

Code: Select all

    dispositionValue = disposition.value    # dispositionValue is just a dummy variable, so we can display the value.
    "%(dispositionValue)d"
...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.

ProxyPawaa
Newbie
Posts: 23
Joined: Sun Jan 16, 2011 5:43 pm
Contact:

Re: A question involving stat variables and changing backgro

#15 Post by ProxyPawaa » Sat Jan 29, 2011 1:58 am

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...
Last edited by ProxyPawaa on Sat Jan 29, 2011 2:22 am, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: Google [Bot]