displaying user defined class on a text

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
searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

displaying user defined class on a text

#1 Post by searchwindows »

Hello,
I'm trying to show the output of total affection on a text with this Actor class.
But it gives me an error on line 22.
Anyone mind teaching me how to fix this?

Code: Select all

init python:
1    class Actor:
2       def __init__(self, character, name, affection):
3            self.c = character
4            self.name = name
5            self.affection = affection
6
7        def affection_up(self, amount):
8            self.affection += amount
9           
10
11       def affection_down(self, amount):
12            self.affection -+ amount
13            
14        
15        def output(self):
16            return self.affection
17
18 $ total_affection = Actor.affection
19
20 define Hai = Actor(Character("Hailey"), "Hailey", 0)
21 $ Hai.affection_up(5)
22 "Your affection score is: [total_affection].

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: displaying user defined class on a text

#2 Post by Per K Grok »

searchwindows wrote: Tue Jan 11, 2022 3:23 am Hello,
I'm trying to show the output of total affection on a text with this Actor class.
But it gives me an error on line 22.
Anyone mind teaching me how to fix this?

Code: Select all

init python:
1    class Actor:
2       def __init__(self, character, name, affection):
3            self.c = character
4            self.name = name
5            self.affection = affection
6
7        def affection_up(self, amount):
8            self.affection += amount
9           
10
11       def affection_down(self, amount):
12            self.affection -+ amount
13            
14        
15        def output(self):
16            return self.affection
17
18 $ total_affection = Actor.affection
19
20 define Hai = Actor(Character("Hailey"), "Hailey", 0)
21 $ Hai.affection_up(5)
22 "Your affection score is: [total_affection].

This would work

Code: Select all

init python:
    class Actor:
        def __init__(self, character, name, affection):
            self.c = character
            self.name = name
            self.affection = affection

        def affection_up(self, amount):
            self.affection += amount


        def affection_down(self, amount):
            self.affection -+ amount


        def output(self):
            return self.affection


    Hai = Actor(Character("Hailey"), "Hailey", 0)



label start:

    ### $ total_affection = Actor.affection   ### removed from script
    $ Hai.affection_up(5)

    "Your affection score is: [Hai.affection]."


It is showing Hai.affection
I'm not sure what your intention with total_affection is, but that line of code is not doing whatever it is you want it to do.

searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

Re: displaying user defined class on a text

#3 Post by searchwindows »

This would work

Code: Select all

init python:
    class Actor:
        def __init__(self, character, name, affection):
            self.c = character
            self.name = name
            self.affection = affection

        def affection_up(self, amount):
            self.affection += amount


        def affection_down(self, amount):
            self.affection -+ amount


        def output(self):
            return self.affection


    Hai = Actor(Character("Hailey"), "Hailey", 0)



label start:

    ### $ total_affection = Actor.affection   ### removed from script
    $ Hai.affection_up(5)

    "Your affection score is: [Hai.affection]."


It is showing Hai.affection
I'm not sure what your intention with total_affection is, but that line of code is not doing whatever it is you want it to do.
[/quote]

Oh I just derped with total_affection.
But for me, [Hal.affection] is returns 0 and not 5 even after add 5 points of affection.

searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

Re: displaying user defined class on a text

#4 Post by searchwindows »

searchwindows wrote: Wed Jan 12, 2022 4:06 am This would work

Code: Select all

init python:
    class Actor:
        def __init__(self, character, name, affection):
            self.c = character
            self.name = name
            self.affection = affection

        def affection_up(self, amount):
            self.affection += amount


        def affection_down(self, amount):
            self.affection -+ amount


        def output(self):
            return self.affection


    Hai = Actor(Character("Hailey"), "Hailey", 0)



label start:

    ### $ total_affection = Actor.affection   ### removed from script
    $ Hai.affection_up(5)

    "Your affection score is: [Hai.affection]."


It is showing Hai.affection
I'm not sure what your intention with total_affection is, but that line of code is not doing whatever it is you want it to do.
Oh I just derped with total_affection.
But for me, [Hal.affection] is returns 0 and not 5 even after add 5 points of affection.
[/quote]

Oh it returned the totol after I defined Hai inside init python block instead of outside of it.
But reloading the the script resets the affection score back to 0.
Is there a way to keep it saved but not persistent so it can stil reset when a new game's started?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: displaying user defined class on a text

#5 Post by Per K Grok »

searchwindows wrote: Wed Jan 12, 2022 4:16 am
------


Oh it returned the totol after I defined Hai inside init python block instead of outside of it.
But reloading the the script resets the affection score back to 0.
Is there a way to keep it saved but not persistent so it can stil reset when a new game's started?


Try

default = Actor(Character("Hailey"), "Hailey", 0)


That would make it a variable and Renpy should save it at it's value at the point of saving.

This page could be worth taking a look at
https://www.renpy.org/doc/html/save_load_rollback.html

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

Re: displaying user defined class on a text

#6 Post by Ocelot »

searchwindows wrote: Wed Jan 12, 2022 4:16 am But reloading the the script resets the affection score back to 0.
Use default to declare objects which would change during the game. Anything else is not guaranteed to be saved (unless you know exactly what you are doing).
< < insert Rick Cook quote here > >

searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

Re: displaying user defined class on a text

#7 Post by searchwindows »

Ocelot wrote: Wed Jan 12, 2022 7:07 am
searchwindows wrote: Wed Jan 12, 2022 4:16 am But reloading the the script resets the affection score back to 0.
Use default to declare objects which would change during the game. Anything else is not guaranteed to be saved (unless you know exactly what you are doing).
Per K Grok wrote: Wed Jan 12, 2022 7:06 am
searchwindows wrote: Wed Jan 12, 2022 4:16 am
------


Oh it returned the totol after I defined Hai inside init python block instead of outside of it.
But reloading the the script resets the affection score back to 0.
Is there a way to keep it saved but not persistent so it can stil reset when a new game's started?


Try

default = Actor(Character("Hailey"), "Hailey", 0)


That would make it a variable and Renpy should save it at it's value at the point of saving.

This page could be worth taking a look at
https://www.renpy.org/doc/html/save_load_rollback.html
Using

Code: Select all

default = Actor(Character("Hailey"), "Hailey", 0)
"Your affection score is: [Hailey.affection]."
gives me an error saying "Name 'Hailey' is not defined."

if I define it

Code: Select all

define Hailey = Actor(Character("Hailey"), "Hailey", 0)
this works. But if I press shift+R to refresh, score goes back to 0.

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

Re: displaying user defined class on a text

#8 Post by Ocelot »

Did you read link Per k Grok posted? There is an example of how to use default (same way as you use define):

Code: Select all

default Hailey = Actor(Character("Hailey"), "Hailey", 0)
< < insert Rick Cook quote here > >

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: displaying user defined class on a text

#9 Post by Per K Grok »

searchwindows wrote: Wed Jan 12, 2022 7:09 pm
Using

Code: Select all

default = Actor(Character("Hailey"), "Hailey", 0)
"Your affection score is: [Hailey.affection]."
gives me an error saying "Name 'Hailey' is not defined."

Sorry, I miss wrote and then failed to notice that when I posted,
It should be

default Hailey= Actor(Character("Hailey"), "Hailey", 0)


The difference between

define Hailey
and
default Hailey

is that 'define' gives you a constant and 'default' gives you variable.
A constant is supposed to have one value that is not changed during the game. You technically can change a constant in Renpy, but Renpy will not save the changed value.
A variable on the other hand is intended to have a value that is changed and Renpy will save the latest value.

searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

Re: displaying user defined class on a text

#10 Post by searchwindows »

Per K Grok wrote: Thu Jan 13, 2022 4:29 am
searchwindows wrote: Wed Jan 12, 2022 7:09 pm
Using

Code: Select all

default = Actor(Character("Hailey"), "Hailey", 0)
"Your affection score is: [Hailey.affection]."
gives me an error saying "Name 'Hailey' is not defined."

Sorry, I miss wrote and then failed to notice that when I posted,
It should be

default Hailey= Actor(Character("Hailey"), "Hailey", 0)


The difference between

define Hailey
and
default Hailey

is that 'define' gives you a constant and 'default' gives you variable.
A constant is supposed to have one value that is not changed during the game. You technically can change a constant in Renpy, but Renpy will not save the changed value.
A variable on the other hand is intended to have a value that is changed and Renpy will save the latest value.
Thanks for the explanation. I understand the difference now.

But default Hailey= Actor(Character("Hailey"), "Hailey", 0) (which I tried as well) gives me invalid syntax error./

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

Re: displaying user defined class on a text

#11 Post by Ocelot »

searchwindows wrote: Thu Jan 13, 2022 1:49 pm But default Hailey= Actor(Character("Hailey"), "Hailey", 0) (which I tried as well) gives me invalid syntax error./
Post more of the code. It should work. It is likely you are doing something wrong (like placing default into python block)
< < insert Rick Cook quote here > >

searchwindows
Regular
Posts: 86
Joined: Wed Dec 15, 2021 6:17 pm
Contact:

Re: displaying user defined class on a text

#12 Post by searchwindows »

Ocelot wrote: Thu Jan 13, 2022 1:54 pm
searchwindows wrote: Thu Jan 13, 2022 1:49 pm But default Hailey= Actor(Character("Hailey"), "Hailey", 0) (which I tried as well) gives me invalid syntax error./
Post more of the code. It should work. It is likely you are doing something wrong (like placing default into python block)
That's exactly what I did. i thought i had it out of python block too. My bad :(

Post Reply

Who is online

Users browsing this forum: Google [Bot]