[SOLVED] How to make renpy.say() use the name (or other variables) from inside a class' method?

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
User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

[SOLVED] How to make renpy.say() use the name (or other variables) from inside a class' method?

#1 Post by Chekhov »

I'm still learning ren'py as well as relearning python. I have some difficulty with getting renpy.say() to work with a class.

Code: Select all

define p = Character(_("Player"), color="#c8c8ff")

init python:
    class Skill(object):
        def __init__(self, name="Skill_naming_error", start = 0):
            self.name = name
            self.value = start
            
        def challenge(self, difficulty = "medium", emotion = "anger"):

            renpy.say(p, "Challenge of [self.name] with a skill of [self.value]")
            
Smoothtalking = Skill("Smoothtalking", 5)
and later:

Code: Select all

$ smoothtalking.challenge("easy", "anger")
I was expecting the renpy.say to work and give an identical result as the following would have:

Code: Select all

p "Challenge of smoothtalking with a skill of 5"
Instead I get a keyerror of u 'self' from exactly the line of the renpy.say statement.

I'm probably misunderstanding something fundamental.

How can I make renpy.say() spit out the name or other variables from inside a class' method?

Of course it works without using the [self.name] and [self.value], but I'd like to dynamically be able to use these.
Last edited by Chekhov on Fri Jul 13, 2018 7:29 pm, edited 1 time in total.

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: How to make renpy.say() use the name (or other variables) from inside a class' method?

#2 Post by MaydohMaydoh »

Should be

Code: Select all

renpy.say(p, "Challenge of %s with a skill of %i" % (self.name, self.value))

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: How to make renpy.say() use the name (or other variables) from inside a class' method?

#3 Post by Chekhov »

That fixes it! Thanks!

But I can't figure out how it works

What even is %s and %i? Why those letters specifically?

If I use

Code: Select all

renpy.say(p, "Challenge of %i to test" % (self.name))
I get a type error.

But

Code: Select all

renpy.say(p, "Challenge of %s to test" % (self.name))
works fine.

Wait, is it because %s signals a string and %i signals an integer?

Is there a list of types that I can see somewhere?

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: How to make renpy.say() use the name (or other variables) from inside a class' method?

#4 Post by MaydohMaydoh »

Yes. s is string and i is integer, f is float etc

https://www.tutorialspoint.com/python/p ... trings.htm

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: How to make renpy.say() use the name (or other variables) from inside a class' method?

#5 Post by Chekhov »

MaydohMaydoh wrote: Fri Jul 13, 2018 11:04 am Yes. s is string and i is integer, f is float etc

https://www.tutorialspoint.com/python/p ... trings.htm
Thank you very much!

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: [SOLVED] How to make renpy.say() use the name (or other variables) from inside a class' method?

#6 Post by trooper6 »

Just a note.
The more modern way to do string interpolation in Python is using format rather than %
So

Code: Select all

renpy.say(p, "Challenge of {0} to test".format(self.name))
Here is some documentation:
https://pyformat.info
https://docs.python.org/2/library/string.html
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]