Making Fields from a Class Appear in 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
verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Making Fields from a Class Appear in Text

#1 Post by verysunshine »

In the example, I'm trying to get the field "cat" from the Item "japanese" to appear in my text. Everything is placeholders.

Code: Select all

python:
    class Item():
        def __init__(self, name, cat, hair):
            self.name = name
            self.cat = cat
            self.hair = hair
    japanese = Item('japan', 'neko', 'pigtails')
    french = Item('france', 'chat', 'short')
    punk = Item('post-apocalypse', 'Marge', 'mohawk')

label start:

    e "Thanks for testing the game!"
    e "[Item.japanese.cat]."
I'm petty sure this is just a silly formatting error, but I've tried many different variations, and nothing worked. I keep getting KeyErrors.

The internal search kept Error-500ing me, but I tried Google and most of those were improperly defined names.

Thank you for your help!

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Making Fields from a Class Appear in Text

#2 Post by DragoonHP »

I'm not sure if text interpolation works with class so yeah but you don't need the Item. bit in the text. It should just be [japanese.cat]

Also, in case it doesn't work, you can do this:

Code: Select all

    e "Thanks for testing the game!"
    $ item_name = japanese.cat
    e "[item_name ]."

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Making Fields from a Class Appear in Text

#3 Post by verysunshine »

It's still not working, but it's changed to a NameError.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 47, in script
    e "Thanks for testing the game!"
  File "game/script.rpy", line 47, in <module>
    e "Thanks for testing the game!"
NameError: name 'japanese' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 47, in script
    e "Thanks for testing the game!"
  Documents\renpy-6.99.12.4-sdk\renpy\ast.py", line 881, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  Documents\renpy-6.99.12.4-sdk\renpy\python.py", line 1913, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 47, in <module>
    e "Thanks for testing the game!"
NameError: name 'japanese' is not defined

Windows-8-6.2.9200
Ren'Py 7.1.3.1092
Ordinary High School Girl 1.0
Mon Jan 21 12:21:15 2019
The same occurs when I switch "Japanese" to "Punk". Having the first field the same as the variable shouldn't matter, right?

Code: Select all

python:
    class Item():
        def __init__(self, place, cat, hair):
            self.name = name
            self.cat = cat
            self.hair = hair
    japanese = Item('japanese', 'neko', 'pigtails')
    french = Item('french', 'chat', 'short')
    punk = Item('abcde, 'Marge', 'mohawk')

label start:

    e "Thanks for testing the game!"
    $ item_name = punk.cat
    e "[item_name ]."

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

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: Making Fields from a Class Appear in Text

#4 Post by trooper6 »

You have some small errors in your code that is messing things up.

1) Creating classes must be done in an init python block, not just a python block
2) To makes sure your classes participate properly in save and rollback, make sure it inherits from object
3) variables should be declared outside of any block (including a python block) using default if you plan on changing/updating those variables, define if you don't.

Make a fresh project and put this code in it. I've just tested it, it works:

Code: Select all

define e = Character("Eileen")

init python:
    class Item(object):
        def __init__(self, name, cat, hair):
            self.name = name
            self.cat = cat
            self.hair = hair

default japanese = Item('japan', 'neko', 'pigtails')
default french = Item('france', 'chat', 'short')
default punk = Item('post-apocalypse', 'Marge', 'mohawk')

label start:

    e "Thanks for testing the game!"
    e "[japanese.cat]."

    return

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

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Making Fields from a Class Appear in Text

#5 Post by verysunshine »

Thanks so much! This is so much help. It works perfectly. You even managed to give me hints towards the solution to my "next step".

I'll let the author of the cookbook tutorial I used know there is problems with their version.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

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: Making Fields from a Class Appear in Text

#6 Post by trooper6 »

One of the reasons I only have one post in the Cookbook is because I don’t want to post anything there unless I’ve had it double checked by a more experienced coder. I don’t want to put out incorrect code that people might learn poor things from.

Here in Q&A it’s cool because people will correct me if my code is messed up somehow. But that back and forth is less there in Cookbook—so I tend to stick here where I know someone will correct me if I give poor coding advice.
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], Google [Bot], Ocelot