Showing a class argument within another argument in 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
Cazad0ra
Newbie
Posts: 22
Joined: Wed May 19, 2021 3:53 pm
Contact:

Showing a class argument within another argument in a text

#1 Post by Cazad0ra »

Hi! To explain myself better, I'm trying to show a text with an argument from a class: a description that includes another argument from that same class, in this case the value of damage done by an ability.

This is the class set up and the ability

Code: Select all

class Ability:
        def __init__(self, img, name, desc, adamage):
            self.img = img
            self.name = name
            self.desc = desc
            self.adamage = adamage
            
default slam = Ability("slam", "Slam","Slams an opponent, causing [slam.adamage] damage.", 5000)           
         
And in my screen, I'm calling that description using

Code: Select all

text "[slam.desc]"
It does return the description, but with a literal [slam.adamage] instead of 5000.
Is this something doable? As always, I hope I managed to make sense :D

SiegeWizard
Newbie
Posts: 6
Joined: Wed Jun 23, 2021 4:50 am
Github: Siege-Wizard
Contact:

Re: Showing a class argument within another argument in a text

#2 Post by SiegeWizard »

Probably the substitution (`[slam.damage]` -> `5000`) is done before the python execution that returns that format, so you will have to format it yourself:

Code: Select all

class Ability(object):
    def __init__(self, img, name, desc, adamage):
        self.img = img
        self.name = name
        self.desc = desc
        self.adamage = adamage

    @property
    def description(self):
        return self.desc.format(**self.__dict__)


slam = Ability("slam", "Slam","Slams an opponent, causing {adamage} damage.", 5000)
slam.description  # 'Slams an opponent, causing 5000 damage.'
And please, use new style classes always (inheriting from object), always. There is no reason not to do so.

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

Re: Showing a class argument within another argument in a text

#3 Post by Ocelot »

https://www.renpy.org/doc/html/text.htm ... ating-data
The !i flag will make additional interpolate for the interpolated string:

Code: Select all

define earned_points_info = _("[points]{image=points.png} earned points")
g "I'm happy to see you you have [earned_points_info!ti]."
This should be used to substitute the text that has a substitution inside.
< < insert Rick Cook quote here > >

Cazad0ra
Newbie
Posts: 22
Joined: Wed May 19, 2021 3:53 pm
Contact:

Re: Showing a class argument within another argument in a text

#4 Post by Cazad0ra »

I'm afraid both return the same result :(

SiegeWizard
Newbie
Posts: 6
Joined: Wed Jun 23, 2021 4:50 am
Github: Siege-Wizard
Contact:

Re: Showing a class argument within another argument in a text

#5 Post by SiegeWizard »

Did you write it like this?

Code: Select all

text "[slam.description]"
And provided the description between curly brackets like shown?

Code: Select all

default slam = Ability("slam", "Slam","Slams an opponent, causing {adamage} damage.", 5000)

Cazad0ra
Newbie
Posts: 22
Joined: Wed May 19, 2021 3:53 pm
Contact:

Re: Showing a class argument within another argument in a text

#6 Post by Cazad0ra »

Ahh I saw the issue, I had loaded an unmodified ability set ^^"
It works, thanks :D

Post Reply

Who is online

Users browsing this forum: Google [Bot], munni