custom character class-check for element in list

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:

custom character class-check for element in list

#1 Post by abysswatcher »

I have this code:

Code: Select all

class Char(object):
    def __init__(self, mood, bond, pol, traits):
        self.mood = mood
        self.bond = bond
        self.pol = pol
        self.traits = traits
     def large_dec(self):
        temp_val = renpy.random.randint(4,7)
        self.bond -= temp_val
        msg.msg("Relationship decreased by [temp_val] points.")
    def med_dec(self):
        temp_val = renpy.random.randint(2,5)
        self.bond -= temp_val
    def low_dec(self):
        temp_val = renpy.random.randint(1,3)
        self.bond -= temp_val
         
     fang = Char( 
     mood ="Anxious",
     bond = 0,
     pol = None,
     traits = FangJie_traits
     )
    FangJie_traits = [
       "Well-Informed",
       "Protestor",
       "Righteous",
       "Impulsive",
       "sensitive"
       ]
I was wondering is there a way to check if there is an element in the list which is part of an object? e.g check if this character has the trait "well informed" so they can talk about some recent news that has been going around.

I am also curious if I am implementing my bond manipulation functions correctly. Would there be a more efficient way?

I'm a bit nervous at experimenting code that might break something so I thought to ask here for some python help.
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: custom character class-check for element in list

#2 Post by hell_oh_world »

easy enough. use the `in` operator to check if an element exists in a list or iterables...

Code: Select all

default my_list = [1, 2, 3]
label start:
  if 1 in my_list:
    "1 is in the list."
in your case... default first your instance of the Char class, don't declare the fang inside init python since doing so will make fang a defined variable not a default variable, you can prefix store. in the fang or default it instead.

Code: Select all

define FangJie_traits = [
       "Well-Informed",
       "Protestor",
       "Righteous",
       "Impulsive",
       "sensitive"
       ]

default fang = Char( 
     mood ="Anxious",
     bond = 0,
     pol = None,
     traits = FangJie_traits
     )
    
label start:
  if "righteous" in fang.traits:
    "righteous is in the list."
you can also create a method in your class that checks whether that trait is present..

Code: Select all

init python:
  class Char(object):
    # your codes of the Char class...
    
    # the checker method...
    def hasTrait(self, trait):
      return train in self.traits
      

define FangJie_traits = [
       "Well-Informed",
       "Protestor",
       "Righteous",
       "Impulsive",
       "sensitive"
       ]

default fang = Char( 
     mood ="Anxious",
     bond = 0,
     pol = None,
     traits = FangJie_traits
     )
      
label start:
  if fang.hasTrait("righteous"):
    "fang is righteous"
you can also use count() and index() methods of the list, but using the `in` operator is the easiest and safest way.

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: custom character class-check for element in list

#3 Post by abysswatcher »

hell_oh_world wrote: Sun Jun 14, 2020 2:59 am easy enough. use the `in` operator to check if an element exists in a list or iterables...

Code: Select all

default my_list = [1, 2, 3]
label start:
  if 1 in my_list:
    "1 is in the list."
in your case... default first your instance of the Char class, don't declare the fang inside init python since doing so will make fang a defined variable not a default variable, you can prefix store. in the fang or default it instead.

Code: Select all

define FangJie_traits = [
       "Well-Informed",
       "Protestor",
       "Righteous",
       "Impulsive",
       "sensitive"
       ]

default fang = Char( 
     mood ="Anxious",
     bond = 0,
     pol = None,
     traits = FangJie_traits
     )
    
label start:
  if "righteous" in fang.traits:
    "righteous is in the list."
you can also create a method in your class that checks whether that trait is present..

Code: Select all

init python:
  class Char(object):
    # your codes of the Char class...
    
    # the checker method...
    def hasTrait(self, trait):
      return train in self.traits
      

define FangJie_traits = [
       "Well-Informed",
       "Protestor",
       "Righteous",
       "Impulsive",
       "sensitive"
       ]

default fang = Char( 
     mood ="Anxious",
     bond = 0,
     pol = None,
     traits = FangJie_traits
     )
      
label start:
  if fang.hasTrait("righteous"):
    "fang is righteous"
you can also use count() and index() methods of the list, but using the `in` operator is the easiest and safest way.
Thanks a lot for this! I really appreciate it! Just to clarify I declare my characters in their own files because they have other variables as well. Since I'm only using the python block and importing the character class this should be fine right?
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: custom character class-check for element in list

#4 Post by hell_oh_world »

abysswatcher wrote: Sun Jun 14, 2020 7:05 am Thanks a lot for this! I really appreciate it! Just to clarify I declare my characters in their own files because they have other variables as well. Since I'm only using the python block and importing the character class this should be fine right?
yeah, why I pointed that out because looking at your code the fang object is mutable (meaning that you plan to change it throughout the different points of the game). like fang's bond you want to add or deduct something to it, then defining it is a bad idea. just always remember this: default for mutable / changeable variables, objects etc. and define for those constants and values that will never change once declared. consider the fang traits that I did as an example, I just observed that it's fixed and you will not change it, like you wont add / remove another traits in that fang_traits variable.

Post Reply

Who is online

Users browsing this forum: camzgr8game, Google [Bot], munni