Can I use def: for all 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
rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Can I use def: for all variables

#1 Post by rayminator »

Can I use def: for all variables that have the same variables

so that all characters that have same variable like love,hate,money,power

so that I won't have big list something like this

Code: Select all

default anna_money = 0
default david_money = 0
etc
etc

Code: Select all

def __init__(self, char):  
            self.char = char
            self.money = 0
            self.money.limit = 5000
and how to do so?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2376
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Can I use def: for all variables

#2 Post by Ocelot »

Do you want something like:

Code: Select all

init python:
    class CharacterData:
        def __init__(self, character_name, money=0, money_limit=5000, love=0, power=0):
            self.name = character_name
            self. money = money
            self.money_limit = money_limit
            self.love = love
            self.power = power
        # Other methods

default david = CharacterData("David")
default anna = CharacterData("Anna", money_limit=10000)
default bethany = CharacterData("Bethany", money=100, love=10)
< < insert Rick Cook quote here > >

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Can I use def: for all variables

#3 Post by rayminator »

that's give me an error

when I try to do this I have tried two ways

it's saying 'self' is not defined or AttributeError: 'ADVCharacter' object has no attribute 'lust'

Code: Select all

bar:
                value self.anna_lust
                range 300
                xysize(270,100)
                left_bar "gui/lust_empty_bar.png"
                right_bar "gui/lust_full_bar.png"

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 44, in script
    d "Hey [son_name] you going to have baby sister..."
  File "game/Porfile/anna_profile.rpy", line 1, in execute
    screen Anna_profile():
  File "game/Porfile/anna_profile.rpy", line 1, in execute
    screen Anna_profile():
  File "game/Porfile/anna_profile.rpy", line 62, in execute
    vbox:
  File "game/Porfile/anna_profile.rpy", line 65, in execute
    hbox:
  File "game/Porfile/anna_profile.rpy", line 66, in execute
    bar:
  File "game/Porfile/anna_profile.rpy", line 66, in keywords
    bar:
  File "game/Porfile/anna_profile.rpy", line 67, in <module>
    value self.anna_lust
NameError: name 'self' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 44, in script
    d "Hey [son_name] you going to have baby sister..."
  File "R:\renpy\renpy-7.4.2-sdk/renpy/ast.py", line 715, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/exports.py", line 1417, in say
    who(what, *args, **kwargs)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/character.py", line 1219, in __call__
    self.do_display(who, what, cb_args=self.cb_args, dtt=dtt, **display_args)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/character.py", line 871, in do_display
    **display_args)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/character.py", line 621, in display_say
    rv = renpy.ui.interact(mouse='say', type=type, roll_forward=roll_forward)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 3276, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, pause=pause, pause_start=pause_start, **kwargs)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 3693, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 567, in visit_all
    d.visit_all(callback, seen)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 567, in visit_all
    d.visit_all(callback, seen)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 567, in visit_all
    d.visit_all(callback, seen)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/screen.py", line 432, in visit_all
    callback(self)
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/core.py", line 3693, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/screen.py", line 443, in per_interact
    self.update()
  File "R:\renpy\renpy-7.4.2-sdk/renpy/display/screen.py", line 631, in update
    self.screen.function(**self.scope)
  File "game/Porfile/anna_profile.rpy", line 1, in execute
    screen Anna_profile():
  File "game/Porfile/anna_profile.rpy", line 1, in execute
    screen Anna_profile():
  File "game/Porfile/anna_profile.rpy", line 62, in execute
    vbox:
  File "game/Porfile/anna_profile.rpy", line 65, in execute
    hbox:
  File "game/Porfile/anna_profile.rpy", line 66, in execute
    bar:
  File "game/Porfile/anna_profile.rpy", line 66, in keywords
    bar:
  File "game/Porfile/anna_profile.rpy", line 67, in <module>
    value self.anna_lust
NameError: name 'self' is not defined

Windows-10-10.0.19041
Ren'Py 7.4.6.1693
Progerialust Family 1.0
Thu Jul 22 12:48:33 2021

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2376
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Can I use def: for all variables

#4 Post by Ocelot »

You didn't show your code and how you are using it. And value self.anna_lust is suspect. It should probably be something else.
< < insert Rick Cook quote here > >

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Can I use def: for all variables

#5 Post by rayminator »

I have figure it out you use anna.lust

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2376
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Can I use def: for all variables

#6 Post by Ocelot »

For quick reference, here is the example of using this code:

Code: Select all

init -100 python:
    class CharacterData:
        def __init__(self, character_name, money=0, money_limit=5000, love=0, power=0):
            self.name = character_name
            self. money = money
            self.money_limit = money_limit
            self.love = love
            self.power = power

default david = CharacterData("David")
default anna = CharacterData("Anna", money_limit=10000)
default bethany = CharacterData("Bethany", money=100, love=10)

screen profile_select(profiles=profile_list):
    style_prefix "profile_select"
    vbox:
        for profile in profiles:
            textbutton profile.name action Show("profile_view", profile=profile)

style profile_select_vbox is vbox:
    spacing 10

style profile_select_button is button:
    xsize 200
    ysize 50
    background Solid("#00a")

screen profile_view(profile):
    modal True
    add Solid('#000')
    vbox:
        label profile.name xalign 0.5 text_size 32
        hbox:
            label "Money: "
            text str(profile.money) + '/' + str(profile.money_limit)
        hbox:
            label "Love: "
            text str(profile.love)
        textbutton "Return" action Hide("profile_view")

label start:
    show screen profile_select([david, anna, bethany])
    "Test"
    return
< < insert Rick Cook quote here > >

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Can I use def: for all variables

#7 Post by rayminator »

I have figured out a easier way of doing what I wanted thanks for the help

here is my code

Code: Select all

init python:
    class lists():
        def __init__(self, char, money, lust, pervert, pregnancy_chance, kids, females, love_david, love_anna, love_dad, love_mother):
            self.char = char
            self.lust = 0
            self.money = 0
            self.pervert = 0
            self.pregnancy_chance = 0
            self.kids = 0
            self.females = 0
            self.males = 0
            self.char = char
            self.love_david = 0
            self.love_mother = 0
            self.love_dad = 0
            self.love_anna = 0


label girls_list:# this list will get bigger over time
    python:
        anna = lists(m, None, None, None, None, None, None, None, None, None, None)
        mom = lists(m, None, None, None, None, None, None, None, None, None, None)
        david = lists(m, None, None, None, None, None, None, None, None, None, None)
        dad = lists(m, None, None, None, None, None, None, None, None, None, None)
        lists = [anna, mom, dad, david]

Post Reply

Who is online

Users browsing this forum: Google [Bot]