Page 1 of 1

[SOLVED]Dynamic Data for a class attribute

Posted: Fri Jan 15, 2021 1:22 pm
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 !!!!!

Re: Dynamic Data for a class attribute

Posted: Fri Jan 15, 2021 5:37 pm
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.

Re: Dynamic Data for a class attribute

Posted: Fri Jan 15, 2021 5:59 pm
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?

[SOLVED]Dynamic Data for a class attribute

Posted: Sat Jan 16, 2021 4:11 am
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.