[SOLVED]Dynamic Data for a class attribute

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
GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

[SOLVED]Dynamic Data for a class attribute

#1 Post by GoldenD »

Hy guys,
sorry for the subject, hope my code will be clearer than title.

I give you the code and I think you'll understand which problem i want to solve. Let's go

Code: Select all

define  idSpeaker           = "player.jpg"

default Var_Globale01       = "fr"

init python:

    class clsTest01:
        def __init__(self, name=""):
            self.name       = name
            self.dialog     = "[Dialogue003]"

    class clsTest02:
        def __init__(self, id=0, name=""):
            self.id         = id
            self.name       = name
            self.dialog     = "Dialogue_" + "[Var_Globale01]" + "_" + str(self.id).zfill(3)

    class clsTest03:
        def __init__(self, id=0, name=""):
            self.id         = id
            self.name       = name
            self.dlg        = "Dialogue_" + "[Var_Globale01]" + "_" + str(self.id).zfill(3)
        def dialog(self):
            return getattr(store, self.dlg)
#--------------------------------------------------------------------------------------------------
#                                           START
#--------------------------------------------------------------------------------------------------
label start:

    define speaker      = Character("", image = "[PICTURES_CHARACTERS][idSpeaker]")

    call loadDialogues

    $ listOfclsTest01   = [( clsTest01( name="Name 01"))]
    $ listOfclsTest02   = [( clsTest02( id=1, name="Name 02"))]
    $ listOfclsTest03   = [( clsTest03( id=1, name="Name 03"))]
# TEST1
    $ speaker ( listOfclsTest01[0].dialog )     # OK OF COURSE but so simple

# TEST2
    $ test = listOfclsTest02[0].dialog
    $ speaker ( listOfclsTest02[0].dialog )     # return "Dialogue_fr_001" (name of variable, not datas)
    $ speaker ( "[test]" )                      # return "Dialogue_[Var_Globale01]_001"

# TEST3
    $ speaker ( listOfclsTest03[0].dialog )     
    # Exception: Character expects its what argument to be a string, got <bound method clsTest03.dialog of <store.clsTest03 object at 0x000000000a33ba50>>

    return

label loadDialogues:
    $ Dialogue_fr_001   = "Dialogue001 Dialogue001 Dialogue001 Dialogue001 ...\n{i}- Dialogue001  ? Dialogue001 .{/i}\nDialogue001 Dialogue001  ..."
    $ Dialogue003       = "Dialogue003 Dialogue003 Dialogue003 Dialogue003 ...\n{i}- Dialogue003  ? Dialogue003 .{/i}\nDialogue003 Dialogue003  ..."
As you see, i tried many code (i don't give the more stupid i tried).

And of course what I want is assign Dialog attribute the value of my dynamic variable when i declare my class.

An idea, a dream, a cup of tea, a smile, i take all your suggests !!!!!
Last edited by GoldenD on Sat Jan 16, 2021 4:13 am, edited 1 time in total.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Dynamic Data for a class attribute

#2 Post by _ticlock_ »

GoldenD wrote: Fri Jan 15, 2021 1:22 pm
Hi, GoldenD,

I am not sure what exactly you are trying to do but I can see a couple of mistakes:

1)

Code: Select all

...
    class clsTest03:
        def dialog(self):
            return getattr(store, self.dlg)
$ speaker ( listOfclsTest03[0].dialog )  
# Exception: Character expects its what argument to be a string, got <bound method clsTest03.dialog of <store.clsTest03 object at 0x000000000a33ba50>>

You need either to put decorator @property to the method dialog or add brackets when you are trying to call it:

Code: Select all

...
    class clsTest03:
        @property
        def dialog(self):
            return getattr(store, self.dlg)

Code: Select all

$ speaker ( listOfclsTest03[0].dialog() )  
2)Python doesn't interpret "[Var_Globale01]" as a value of string Var_Globale01. Thus when you try:

Code: Select all

getattr(store, self.dlg)
you will get an error since there is no such variable in store as Dialogue_[Var_Globale01]_001
Instead you should just use either global Var_Globale01 or store.Var_Globale01:

Code: Select all

class clsTest03:
        def __init__(self, id=0, name=""):
            self.id         = id
            self.name       = name
            self.dlg        = "Dialogue_" + store.Var_Globale01 + "_" + str(self.id).zfill(3)
I also recommend changing the other python code with similar syntax to store.<global_variable>, otherwise it is really hard to understand what is going on.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Dynamic Data for a class attribute

#3 Post by xavimat »

I guess this is an XY problem.

It seems that you are using renpy without using renpy.
I mean, you want to do something (that renpy can do in its own way) but using python classes and functions.

I'd suggest you to explain what do you want to do and probably there will be a way to do it in renpy.

Assuming for the use of the "fr" string, you want to do a multilingual game. Is this so? Well, renpy has its own translations system. Do you know it?
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

[SOLVED]Dynamic Data for a class attribute

#4 Post by GoldenD »

Ok guys and thanks for your replies,
sorry you don't understand what I want.
In one sentence : just want to construct the name of a VARiable with others variables values and assign this VARiable to a class attribute. (clearer !! not sure)

So, reading yours answers, i (and you) found the solution :

Code: Select all

define  idSpeaker           = "player.jpg"
default Var_Globale01       = "fr"

init python:
    class clsTest03:
        def __init__(self, id=0, name=""):
            self.id         = id
            self.name       = name
            self.dlg        = "Dialogue_" + store.Var_Globale01 + "_" + str(self.id).zfill(3)
        def dialog(self):
            return getattr(store, self.dlg)
#--------------------------------------------------------------------------------------------------
#                                           START
#--------------------------------------------------------------------------------------------------
label start:

    define speaker      = Character("", image = "[PICTURES_CHARACTERS][idSpeaker]")
    call loadDialogues

    $ listOfclsTest03   = [( clsTest03( id=1, name="Name 03"))]
    $ speaker ( listOfclsTest03[0].dialog() )     

    return

label loadDialogues:
    $ Dialogue_fr_001   = "Dialogue001 Dialogue001 Dialogue001 Dialogue001 ...\n{i}- Dialogue001  ? Dialogue001 .{/i}\nDialogue001 Dialogue001  ..."

Assuming for the use of the "fr" string, you want to do a multilingual game. Is this so? Well, renpy has its own translations system. Do you know it?
- yeah, I know but I don't use it for now. For now, I prefer control my code, it's my way for code portability.


A great THANKS all guys.

Post Reply

Who is online

Users browsing this forum: No registered users