Page 1 of 1

updating variables that rely on other variables

Posted: Sat Sep 08, 2018 6:30 pm
by j417
Hello, I'm very new to ren'py, sorry if this is a simple question.

I have several defense values that combine into one master defense value. simplified version:

Code: Select all

$ dArmor = 10
$ dNatural = 20
$ dTotal = dArmor + dNatural
So dTotal relies entirely on values provided by dArmor and dNatural.

I have a Stats screen that displays all of these values. simplified version:

Code: Select all

screen stats:
	vbox xalign 0:
		text "Armor Defense: [dArmor]"
		text "Natural Defense: [dNatural]"
		text "Total Defense: [dTotal]"
This displays as expected when the Stats screen is called.
Armor Defense: 10
Natural Defense: 20
Total Defense: 30

I have a menu system where one of the options adds 1 to dNatural

Code: Select all

$ dNatural += 1
The Problem: after that point is added to dNatrual, the Natural Defense value on my Stats screen updates as expected, but the Total Defense value does not.

The screen displays as:
Armor Defense: 10
Natural Defense: 21
Total Defense: 30

How can I make the dTotal variable update with the new dNatural value?
Or, is there a better way to do this? As I've said, I'm very new to ren'py, so everything I have is mostly the result of trial and error.

Re: updating variables that rely on other variables

Posted: Sun Sep 09, 2018 2:11 am
by Per K Grok
j417 wrote:
Sat Sep 08, 2018 6:30 pm

---
I have a menu system where one of the options adds 1 to dNatural

Code: Select all

$ dNatural += 1
The Problem: after that point is added to dNatrual, the Natural Defense value on my Stats screen updates as expected, but the Total Defense value does not.

The screen displays as:
Armor Defense: 10
Natural Defense: 21
Total Defense: 30

How can I make the dTotal variable update with the new dNatural value?
---
One way to do this is that every time you change the armor or the natural value, you also add the line

$ dTotal = dArmor + dNatural

so that the total value is updated too.

Re: updating variables that rely on other variables

Posted: Sun Sep 09, 2018 3:24 am
by IrinaLazareva
Add to your script (above label start):

Code: Select all

init python:
    def sumt():
        global dArmor, dNatural, dTotal
        dTotal = dArmor + dNatural
    config.overlay_functions.append(sumt)
For example:

Code: Select all

default dArmor = 0
default dNatural = 0
default dTotal = 0

init python:
    def sumt():
        global dArmor, dNatural, dTotal
        dTotal = dArmor + dNatural
    config.overlay_functions.append(sumt)

screen stats:
    vbox xalign 0:
        text "Armor Defense: [dArmor]"
        text "Natural Defense: [dNatural]"
        text "Total Defense: [dTotal]"

label start:
    show screen stats
    'Bla bla' 
    $ dNatural +=10
    '+10 natural'
    $ dArmor +=5
    '+5 armor'
    'etc' 
    return