How to get the name of a character from a class?

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
Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

How to get the name of a character from a class?

#1 Post by Zherot »

I want to define my characters as classes and set the name directly when i create an instance of that character class, but i have trouble getting the name from that instance and then passing it to the method that creates characters in renpy:

default MC = Characters("Jhon")
default char2nm=[MC.name] <------MC is an instance of the character class and name is a parameter from that class
define m = Character(char2nm, color="#00ff00")

This gives me an error, says char2nm is not defined...

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: How to get the name of a character from a class?

#2 Post by kivik »

define comes before default so you can't do it that way. Have a read of what's actually going on with define and default here: https://www.renpy.org/doc/html/python.h ... -statement

Also, is Characters your new class? Just making sure it's not a typo for Character (Renpy's Character class also has .name as attribute). Assuming your new class is Characters, you can replace the define with a default:

Code: Select all

default MC = Characters("Jhon")
default char2nm=MC.name
default m = Character(char2nm, color="#00ff00")
I don't think you want to define m anyway, as it will cause problems down the line when you save and load the game.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: How to get the name of a character from a class?

#3 Post by Zherot »

I just wanted to stablish names for characters since the object creation,to make the process easier or more "efficient", is that really a bad thing that can cause problems with saves?

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

Re: How to get the name of a character from a class?

#4 Post by Ocelot »

kivik wrote: Fri May 04, 2018 6:45 pmI don't think you want to define m anyway, as it will cause problems down the line when you save and load the game.
Characters are usually immutable, so in reality it will help with savegame bloat.
BUt you are correct. default is "init-after-start" when define works at init time.

Do you want to ever change MC and char2nm during the game? If not, you can safely chnge definition to use define
Another way is to make m to use interpolation in your Character object:

Code: Select all

define m = Character("[char2nm]", color="#00ff00")
< < insert Rick Cook quote here > >

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: How to get the name of a character from a class?

#5 Post by kivik »

Characters are usually immutable, so in reality it will help with savegame bloat.
Oh god I hadn't considered that - I change the characters after starting the game - like which base image they use (path changes) and their name colours. Maybe I oughta use interpolation as well which I always forget you can do!

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: How to get the name of a character from a class?

#6 Post by Zherot »

Sorry for not answering anything yet but i have been bussy with other stuff, i will check your answers as soon as i can.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: How to get the name of a character from a class?

#7 Post by Zherot »

Ocelot wrote: Sat May 05, 2018 2:23 am
kivik wrote: Fri May 04, 2018 6:45 pmI don't think you want to define m anyway, as it will cause problems down the line when you save and load the game.
Characters are usually immutable, so in reality it will help with savegame bloat.
BUt you are correct. default is "init-after-start" when define works at init time.

Do you want to ever change MC and char2nm during the game? If not, you can safely chnge definition to use define
Another way is to make m to use interpolation in your Character object:

Code: Select all

define m = Character("[char2nm]", color="#00ff00")
What is interpolation and how it relates to my problem?

I think what you suggested actually was what i wanted to do, still i don't really get what is "interpolation", also i just reduced the code to this:

default MC = Characters("Tony")
define mc = Character("[MC.name]", color="#00ff00")

It was redundant to have another variable to take the value i think.
Last edited by Zherot on Fri May 11, 2018 5:59 pm, edited 1 time in total.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: How to get the name of a character from a class?

#8 Post by kivik »

It's basically Renpy reading your string / text first, looking for any variables that needs to be substituted inside: https://www.renpy.org/doc/html/text.htm ... ating-data

So "[char2nm]" means when Renpy tries to display it, it'll check what the variable char2nm contains, and display that instead.

The only tricky thing is, interpolation isn't done everywhere within Renpy, so you need to know where it's done. It's done in text, by this example character names, image filenames, screen text, labels, textbuttons etc.

But usually when you try to use a lower level function, those may not do interpolation and it'll treat [char2nm] as a literal string. You probably don't have to worry about this too much though, just read the examples in the link above and try it in different places to see if it works. :)

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: How to get the name of a character from a class?

#9 Post by Zherot »

kivik wrote: Fri May 11, 2018 5:54 pm It's basically Renpy reading your string / text first, looking for any variables that needs to be substituted inside: https://www.renpy.org/doc/html/text.htm ... ating-data

So "[char2nm]" means when Renpy tries to display it, it'll check what the variable char2nm contains, and display that instead.

The only tricky thing is, interpolation isn't done everywhere within Renpy, so you need to know where it's done. It's done in text, by this example character names, image filenames, screen text, labels, textbuttons etc.

But usually when you try to use a lower level function, those may not do interpolation and it'll treat [char2nm] as a literal string. You probably don't have to worry about this too much though, just read the examples in the link above and try it in different places to see if it works. :)
Ah, ok, so I understand now, i reduced the code to this then.

default MC = Characters("Tony")
define mc = Character("[MC.name]", color="#00ff00")

It was redundant to have another variable to take the value i think.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: How to get the name of a character from a class?

#10 Post by kivik »

That's perfect! You've got it already :)

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: How to get the name of a character from a class?

#11 Post by Zherot »

Thanks for your help guys.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]