How to get the name of a character from a class?
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.
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.
How to get the name of a character from a class?
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...
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...
Re: How to get the name of a character from a class?
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:
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.
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")
Re: How to get the name of a character from a class?
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?
- 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?
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 > >
Re: How to get the name of a character from a class?
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!Characters are usually immutable, so in reality it will help with savegame bloat.
Re: How to get the name of a character from a class?
Sorry for not answering anything yet but i have been bussy with other stuff, i will check your answers as soon as i can.
Re: How to get the name of a character from a class?
What is interpolation and how it relates to my problem?Ocelot wrote: ↑Sat May 05, 2018 2:23 amCharacters 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")
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.
Re: How to get the name of a character from a class?
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.
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.
Re: How to get the name of a character from a class?
Ah, ok, so I understand now, i reduced the code to this then.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.
default MC = Characters("Tony")
define mc = Character("[MC.name]", color="#00ff00")
It was redundant to have another variable to take the value i think.
Re: How to get the name of a character from a class?
That's perfect! You've got it already
Re: How to get the name of a character from a class?
Thanks for your help guys.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot]