[Solved]Update sceen variables in realtime

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
abysswatcher
Regular
Posts: 42
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

[Solved]Update sceen variables in realtime

#1 Post by abysswatcher »

I have the following code:

Code: Select all

init python:
    x=0
    def GetMax(d):
        while x<1:
            try:
                return max(d, key=lambda key: d[key])
            except:
                raise Exception("value did not get returned.")

define MwBelief = {
    'atheism':2,
    'buddhism':3,
    'christian':1,
    'communism':-2,
    'islam':-1,
    'shintoism':-2,
    'taoism':1,
    'yiguandao':0
}
## The game starts here.

screen world:
    text GetMax(MwBelief) xalign 0.5 yalign 0.5 size 20

label start:

    show screen world

    "hello"
    $ MwBelief['atheism'] + 2
    "life good"
    $ MwBelief['christian'] + 5
    "ayyyyy"

    return
While it does function it stays on Buddhism and does not change when the other keys have the largest integers. How can I fix this to make it realtime?
Last edited by abysswatcher on Sat Oct 10, 2020 8:39 pm, edited 1 time in total.
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Update sceen variables in realtime

#2 Post by hell_oh_world »

Things i've noticed:
1. looks like you're trying to make the variable change overtime, default it instead of defining it.
2. In your code you did something like MwBelief['christian'] + 2, which just basically adds the value of Christian and 2, but the added value is not assigned at all so that's why the value does not change because what you did is only an arithmetic operation and not an assignment operation. What you really want is MwBelief['christian'] += 2 or MwBelief['christian'] = MwBelief['christian'] + 2, which adds 2 and the value of Christian and assign that value to the Christian.
3. You can get the maximum value without even creating a function.
Overall, this is how it should look...

Code: Select all

default MwBelief = { # default instead of define
    'atheism':2,
    'buddhism':3,
    'christian':1,
    'communism':-2,
    'islam':-1,
    'shintoism':-2,
    'taoism':1,
    'yiguandao':0
}

screen world:
  text str(max(MwBelief.values())) xalign 0.5 yalign 0.5 size 20 # MwBelief.values() returns all of the values of the dict
  
label start:
  show screen world
  "d1"
  $ MwBelief['christian'] += 2
  "d2"
  $ MwBelief['christian'] += 2
  "d3"

abysswatcher
Regular
Posts: 42
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Update sceen variables in realtime

#3 Post by abysswatcher »

hell_oh_world wrote: Fri Oct 09, 2020 3:47 am Things i've noticed:
1. looks like you're trying to make the variable change overtime, default it instead of defining it.
2. In your code you did something like MwBelief['christian'] + 2, which just basically adds the value of Christian and 2, but the added value is not assigned at all so that's why the value does not change because what you did is only an arithmetic operation and not an assignment operation. What you really want is MwBelief['christian'] += 2 or MwBelief['christian'] = MwBelief['christian'] + 2, which adds 2 and the value of Christian and assign that value to the Christian.
3. You can get the maximum value without even creating a function.
Overall, this is how it should look...

Code: Select all

default MwBelief = { # default instead of define
    'atheism':2,
    'buddhism':3,
    'christian':1,
    'communism':-2,
    'islam':-1,
    'shintoism':-2,
    'taoism':1,
    'yiguandao':0
}

screen world:
  text str(max(MwBelief.values())) xalign 0.5 yalign 0.5 size 20 # MwBelief.values() returns all of the values of the dict
  
label start:
  show screen world
  "d1"
  $ MwBelief['christian'] += 2
  "d2"
  $ MwBelief['christian'] += 2
  "d3"
Thanks a lot for this. This works really well and exactly like how I want.
The maximum function I made was to return the key of the largest integer not the largest value.
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Update sceen variables in realtime

#4 Post by hell_oh_world »

my bad, missed that part. if that's the case, then it looks like you can just use directly the function max(d, key=lambda key: d[key]).

Code: Select all

screen world:
  text max(MwBelief, key=lambda key: MwBelief[key]) xalign 0.5 yalign 0.5 size 20

Post Reply

Who is online

Users browsing this forum: Bing [Bot], LuckyT