Help with class and functions (Dict, List, random)

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
KingsCard
Newbie
Posts: 14
Joined: Sat Feb 17, 2018 8:58 am
Contact:

Help with class and functions (Dict, List, random)

#1 Post by KingsCard »

Hi,

I'm hitting a wall since forever now and it starts to get on my nerves :p. I'm a beginner in the art of coding, so I picked things here and there, but my results are ... slim to say the least.

So, let's say I have 24 hours and I can eat one fruit every hour, randomly picked in a list divided per hour.

At the start, I have only one choice of fruit per hour, then, I add some other fruits and change the odds

I'm using this function to randomize the odds (viewtopic.php?f=8&t=48265&p=478399&hili ... er#p478399):

Code: Select all

    def weighted_random(pairs):
      total_weight = sum(n for _, n in pairs)
      rvalue =  renpy.random.randint(1, total_weight)
      for (value, weight) in pairs:
          rvalue -= weight
          if rvalue <= 0:
              return value
This function needs a dict with hours for keys and a list per [fruits, value] for values like this one:

Code: Select all

default basic_list = {1:[["apple",1],["cranberry",2]],2:[["biscuit",3],["chocolat",4]]}
I know, it would make more sense to have a dict in a dict, but I couldn't code my class for that.

So, I need to make a class because there will be more than one list and these will have to be often modified during the game :

Code: Select all

init python:
    class Food( object ):
        global hour
        def __init__( self ):
            self._dict = {}
            self._cur_hour = -1

        def new (self, it , val ):
            for k, v in self._dict.items():
                if k==hour:
                    test = False
                    for elem in v:
                        if elem[0] == it:
                            test= True
                    if test==False:
                        v += [[it , val]]
                        return True
            return False

        def new_key(self , key):
            test = False
            for k,v in self._dict.items():
                if k == key:
                    test = True
                    break
            if test == False:
                self._dict[key] = []

        def modify (self, it , val ):
            for k, v in self._dict.items():
                if k==hour:
                    for elem in v:
                        if elem[0] == it:
                            elem[1]+= val      
                            return True

        def change (self, it , val ):
            for k, v in self._dict.items():
                if k==hour:
                    for elem in v:
                        if elem[0] == it:
                            elem[1] = val
                            return True
            return False
Now the basic test game I draw is :

Code: Select all

default class_list = Food()
default hour = 1
default choices = ["apple","biscuit","kiwi","cranberry","banana", "strawberry"]

default item_chosen = ""

screen fridge:
    frame:
        xalign 1.0
        yalign 0.5
        has vbox
        for item in class_list._dict:
            if class_list._dict[item] > 0:
                $ amount = class_list._dict[item]
                text ("[item] : [amount]")

screen controls:
    frame:
        has hbox
        vbox:
            for i in range(0,24):
                textbutton "[i]" action SetVariable("hour",i)
        vbox:
            for item in choices:
                textbutton ("[item]") action SetVariable("item_chosen",item)
        vbox:
            textbutton "new hour" action Function( class_list.new_key, hour )
            textbutton "add item" action Function( class_list.new  , item_chosen , 10 )
            textbutton "set item to 1" action Function( class_list.change, item_chosen, 1 )
            textbutton "increase item by 1" action Function( class_list.modify, item_chosen, 1 )
            
label start:
    show screen controls
    show screen fridge

    "hello"

    jump start
I can now tweak my lists in my dict, I'm very happy :). But my intellect seems to be limited to that.
  • How can I create directly a full set off fruits ?
I tried to add this function then a default list :

Code: Select all

    def start_fruits(fruits_class, start_list):
        temp_hour = 0

        for it in start_list:
           fruits_class.new_key(temp_hour)
            for k, v in fruits_class._dict.items():
                if k==temp_hour:
                    test = False
                    for elem in v:
                        if elem[0] == it:
                            test= True
                    if test==False:
                        v += [[it , 1]]
                        return True
            temp_hour += 1
            return False

# add a start list
default apples_start_fruits =["apple" * 24] #24 x apples, one apple per hour to start 

# add a textbutton ... who doesn't work ... obviously :p
textbutton "start fruits" action Function( start_fruits, class_list, apples_start_fruits )
I tried many random things with differents errors. Can someone please help me do that before I hang myslef with my cordless mouse's cord ? :)

Post Reply

Who is online

Users browsing this forum: Google [Bot]