updating variables that rely on other 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
j417
Newbie
Posts: 1
Joined: Sat Sep 08, 2018 6:14 pm
Contact:

updating variables that rely on other variables

#1 Post by j417 » Sat Sep 08, 2018 6:30 pm

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.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 741
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1 & 2
itch: per-k-grok
Location: Sverige
Contact:

Re: updating variables that rely on other variables

#2 Post by Per K Grok » Sun Sep 09, 2018 2:11 am

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.

User avatar
IrinaLazareva
Veteran
Posts: 389
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: updating variables that rely on other variables

#3 Post by IrinaLazareva » Sun Sep 09, 2018 3:24 am

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
There is a light at the end of the tunnel, just pray it's not a train.

___
TODO: unresolved/нерешённое

Post Reply

Who is online

Users browsing this forum: No registered users