<Solved> Is it possible to set a max and min value for each value in a list and the list as a whole?

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
Xasrai
Newbie
Posts: 11
Joined: Wed Dec 14, 2016 5:27 am
Contact:

<Solved> Is it possible to set a max and min value for each value in a list and the list as a whole?

#1 Post by Xasrai »

I'm trying to write a piece of code that increments a value in desire_list by the amount contained in exposure_list, and then wipe exposure_list clean to be used again the next day. However, I never want any single desire_list value to go above 5(while also being able to raise the minimum above 0 if I choose), AND I don't want the total of desire_lists values to go above, say, 10. Is this possible or am I dreaming? The below code works for me, I just can't work out how to limit the values.

Code: Select all

define exposure_list = [0,0,0,0,0,0,0,0,0]
define desire_list =   [0,0,0,0,0,0,0,0,0]

label sleep_stuff:
$ i = 0
while i < len(exposure_list):
    $ desire_list[i] += exposure_list[i]
    $ exposure_list[i] = 0
    $ i += 1
Last edited by Xasrai on Fri Oct 20, 2017 9:14 am, edited 1 time in total.

User avatar
Scribbles
Miko-Class Veteran
Posts: 636
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island, As We Know It
Projects: In Blood
Organization: Jaime Scribbles Games
Deviantart: breakfastdoodles
itch: scribbles
Location: Ohio
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#2 Post by Scribbles »

viewtopic.php?f=51&t=44793

^ that has some functions for keeping values within a range- it might help
Image - Image -Image

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#3 Post by Remix »

You likely just want python min( *args ) and max( *args ), maybe with some list comprehension (or just iterate indices in a for or while loop as you are doing)

Code: Select all

default num_options = 9

# list of 9 0's
default exposure_list = [0] * num_options
default desire_list =   [0] * num_options

# list of 9 [0,10]'s
default desire_min_max = [ [0, 10] ] * num_options

label a:
    # rebuild list using mins and maxes from above
    $ desire_list = [ max( desire_min_max[i][0], min( desire_min_max[i][1], v + exposure_list[i] ) ) for i,v in enumerate( desire_list ) ]
Fly typed, so might need tweaks

Note: changed define to default as the values are mutable (changeable after being set)
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#4 Post by Remix »

Sub-note:
I don't want the total of desire_lists values to go above, say, 10
You could test that after building the list, then use your own logic to decide which value(s) to lower/raise to fit your bounds

if sum( desire_list ) > 10: do something
Frameworks & Scriptlets:

User avatar
Belgerum
Regular
Posts: 110
Joined: Thu Nov 06, 2014 12:24 am
Skype: belgerum09
Soundcloud: Belgerum
itch: Belgerum
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#5 Post by Belgerum »

If you just want a simple min/max value that covers all values in both lists, you could do something like this:

Code: Select all

$ exposure_list = [0,0,0,0,0,0,0,0,0]
$ desire_list =   [0,0,0,0,0,0,0,0,0]
$ exposure_list_max = 5
$ exposure_list_min = 0
$ desire_list_max = 10
$ desire_list_min = 0

label sleep_stuff:
$ i = 0
while i < len(exposure_list):
    $ exposure_list[i] = max(min(exposure_list[i], exposure_list_max), exposure_list_min)
    $ desire_list[i] += exposure_list[i]
    $ desire_list[i] = max(min(desire_list[i], desire_list_max), desire_list_min)
    $ exposure_list[i] = 0
    $ i += 1

Xasrai
Newbie
Posts: 11
Joined: Wed Dec 14, 2016 5:27 am
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#6 Post by Xasrai »

I've edited the code a bit, having been unable to make the above suggestions work for me. This allows me to set a max value of 10 for the whole set of data, a max of 5 for any individual value but will still reset each value down to 0 instead of a minimum individual value.

Code: Select all

while sum(desire_list) < 10 and sum(exposure_list) >0 and i < len(exposure_list):
    $ desire_list[i] += exposure_list[i]
    while sum(desire_list) >10:
        $ desire_list[i] -= 1
    if desire_list[i] >5:
        $ desire_list[i] = 5
    $ exposure_list[i] = 0
    $ i += 1
It's not perfect, but it will do for now. To answer Belgerum's "question" I really want to be able to set a simple max value to cover the sum of desire_list, and a max value of 5 for each value of the desire_list(as I have been able to do in the dodgy code above.

However I want to be able to 'decay' the desire values over time down to a minimum value that can change over time. Something like this:

Code: Select all

 if day > desire_days[i]:
	$ desire_list[i] -= 1
	if desire_list[i] < min_list[i]:
		$ desire_list[i] = min_list[i] 

Xasrai
Newbie
Posts: 11
Joined: Wed Dec 14, 2016 5:27 am
Contact:

Re: Is it possible to set a max and min value for each value in a list and the list as a whole?

#7 Post by Xasrai »

So, I've gone and written this piece of code that does what I need:

Code: Select all

 $ i = 0
while i < len(desire_days):
    if day > desire_days[i] and if desire_list[i] > min_list[i]:
        $ desire_list[i] -= 1
    $ i += 1


This part checks to see if a the subject has been exposed to each activity in the past 7 days and if the desire value is higher than the minimum then decays the value of the desire by one if it meets the conditions.

Code: Select all

$ i = 0
while sum(desire_list) < 10 and sum(exposure_list) >0 and i < len(exposure_list):
    $ desire_list[i] += exposure_list[i]
    $ desire_days[i] = day + 7
    while sum(desire_list) >10:
        $ desire_list[i] -= 1
    if desire_list[i] >5:
        $ desire_list[i] = 5
    $ exposure_list[i] = 0
    $ i += 1

$ day += 1 
This is much the same as before. I've checked, and each variable increases as expected, the maximums cannot be exceeded and the minimums remain above 0 on an individual basis as defined by min_list.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]