Help with color variable

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
RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Help with color variable

#1 Post by RebootLove » Thu Sep 15, 2022 7:59 pm

Hi, I've been using variables for names in my game so far (In the dialogue box).

So when a player activates the color, he will read something like this:
Image
Code: fo "Hi, my name is [fo]!"

But when I give the player the choice to rename her, I'm not able to color the new name again, leaving something like this:

Image

Code:

Code: Select all

$ fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.")
    $ fo = fo.strip()
    if not fo:
        $ fo = "Foxy"
    if colorednames == 0:
        $ fo = fo
    else:
        $ fo = Character(fo, color="#F38219", who_color ="#F38219")

    fo "Hi, my name is [fo]!"
I'm not being able to give color to the variable fo... I know this must be something really stupid, but after a lot of searching I had no luck with this... Can someone help me?

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#2 Post by enaielei » Thu Sep 15, 2022 11:30 pm

Try...

Code: Select all

label start:
    python:
        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
        props = dict(name=fo or "Foxy")
        if colorednames: props.update(color="#F38219", who_color ="#F38219")
        fo = Character(**props)

    fo "Hi, my name is [fo]!"

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#3 Post by RebootLove » Fri Sep 16, 2022 7:54 am

enaielei wrote:
Thu Sep 15, 2022 11:30 pm
Try...

Code: Select all

label start:
    python:
        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
        props = dict(name=fo or "Foxy")
        if colorednames: props.update(color="#F38219", who_color ="#F38219")
        fo = Character(**props)

    fo "Hi, my name is [fo]!"
Hi, no luck, still no color in dialogue box.

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#4 Post by enaielei » Fri Sep 16, 2022 8:26 am

I just realized you were talking about a coloring a certain text in the dialogue instead of the say name above the dialogue.
Well that will not really work. You need to use text tags to achieve that. You can store the colored string in a variable and use that differently from the Character object.

Code: Select all

label start:
    python:
        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
        foc = fo = fo or "Foxy"
        if colorednames: foc = "{color=#F38219}" + fo + "{/color}"
        fo = Character(fo, who_color ="#F00")

    # foc variable here is the colored text tag, fo's who_color is different.
    fo "Hi, my name is [foc]!"
The who_color sets the color for the say text, the one found above the dialogue text. So its not technically how you would color a certain portion of a text. Use text tags.
Last edited by enaielei on Fri Sep 16, 2022 9:48 am, edited 2 times in total.

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#5 Post by RebootLove » Fri Sep 16, 2022 9:19 am

enaielei wrote:
Fri Sep 16, 2022 8:26 am
I just realized you were talking about a coloring a certain text in the dialogue instead of the say name above the dialogue.
Well that will not really work. You need to use text tags to achieve that. You can store the colored string in a variable and use that differently from the Character object.

Code: Select all

label start:
    python:
        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
        foc = fo = fo or "Foxy"
        if colorednames: foc = "{color=#F38219}" + fo + "{/color}"
        fo = Character(fo, who_color ="#F00")
    
    # foc variable here is the colored text tag, fo's who_color is different.
    fo "Hi, my name is [foc]!"
The who_color sets the color for the say text, the one found above the dialogue text. So its not technically how you would color a certain portion of a text. Use text tags.
```
I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/2/Scripts/Maps/Menus/gamesettings.rpy", line 95: end of line expected.
if colorednames: foc = "{color=#F38219}" + fo + "{/color}"
^

Ren'Py Version: Ren'Py 7.5.0.22062402
Fri Sep 16 10:19:03 2022
```
I get this when trying it.

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#6 Post by enaielei » Fri Sep 16, 2022 9:45 am

I tested the code and it works. Perhaps trying to retype everything and not copy pasting it would solve the inconsistent characters etc.
What I mean by that is make sure your code is properly indented, and indented with space not tabs. In short, type it by hand.
I re-edited the code in my last post, just to make sure also I'm not using tabs.

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#7 Post by RebootLove » Fri Sep 16, 2022 11:05 am

enaielei wrote:
Fri Sep 16, 2022 9:45 am
I tested the code and it works. Perhaps trying to retype everything and not copy pasting it would solve the inconsistent characters etc.
What I mean by that is make sure your code is properly indented, and indented with space not tabs. In short, type it by hand.
I re-edited the code in my last post, just to make sure also I'm not using tabs.
THANKS!! It worked now!!!

I just changed to retain the same variable, so fo can work as a speaker, and also as a string variable.

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#8 Post by RebootLove » Fri Sep 16, 2022 11:49 am

Okay, now I'm having another problem.

I also have an option to enable/disable colored names in dialogue box.
I'm trying to figure out how to modify this code color, but if I don't use the

Code: Select all

fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
Game won't work.

What I need is just to somehow modify the color to neutral or color when the option's clicked.

What I actually have:

Code: Select all

                "Name Color DISABLED":
                    $ colorednames = 0
                    $ ka = "Kate"
                    $ am = "Amanda"
                    $ ha = "Hanah"                   
                    python:
                        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
                        fo = fo = fo or "Foxy"
                        fo = Character(fo, who_color ="#F38219")
                    fo "[fo]!"
                    re "Colors disabled... The names will show now like this: [ka], [er], [ve]..."
                "Name Color ENABLED":
                    $ colorednames = 1
                    $ ka = "{color=#EF4F36}Kate{/color}"
                    $ am = "{color=#CFCFCF}Amanda{/color}"
                    $ ha = "{color=#90AAD5}Hanah{/color}"
                    python:
                        fo = renpy.input("How do you want to call her? If empty, she will be 'Foxy'.").strip()
                        fo = fo = fo or "Foxy"
                        if colorednames: fo = "{color=#F38219}" + fo + "{/color}"
Doing this, it works, but everytime the colors are enabled or disabled, the player needs to input the name again.
If I don't use the renpy.input the code won't work.

Any ideas?

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#9 Post by enaielei » Fri Sep 16, 2022 1:15 pm

Here. I made it simpler for you.

Code: Select all

# the global variable that you can control to activate/deactivate colored mode.
default colored = False

init python:
    class Char():
        def __init__(self, name, color=None, colored=False):
            self.name = name
            self.color = color
            self.colored = colored

        def __str__(self):
            if ((self.colored and colored) or colored) and self.color:
                return "{color=%s}%s{/color}" % (self.color, self.name)
            return self.name

# default your Char, giving the default name as well as the color to show when colored mode is enabled.
default fo = Char("Foxy", "#F38219")
# set the name to "variable.name" then add dynamic=True
define character.fo = Character("fo.name", dynamic=True, who_color="#F00")

# another example
default ka = Char("Kate", "#F38219")
define character.ka = Character("ka.name", dynamic=True, who_color="#F00")

# a helper label that you can re-use
label rename_char(char):
    # remove white leading/trailing white spaces and if empty use the default name set earlier.
    $ char.name = renpy.input("Type the name for '[char]':").strip() or char.name
    return

label set_colored():
    menu:
        "Colored":
            $ colored = True
        "Non Colored":
            $ colored = False

    return

label start:
    # call the helper label and pass the 'defaulted' character earlier, in this case, fo
    call rename_char(fo)
    "Make the character names colored?"
    $ colored = True
    "A colored character name looks like this: [fo]"
    $ colored = False
    "A non colored character name looks like this: [fo]"
    call set_colored
    fo "Great! Welcome to me ([fo])!"
You can try and play with it and see if you can make adjustments according to your need.
Basically, I retained your initial idea of having a global variable which sets the colored mode on/off.
Now all you need to do is change `colored` value to `True` or `False` in order to on/off it.
Also, no need to create another variable like `foc` earlier and use it in dialogues, you can directly use the name `fo`.
This is a working example. You can copy and paste this whole code, but of course you need to replace your start label with the contents of this code's start label.

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#10 Post by RebootLove » Fri Sep 16, 2022 3:09 pm

Man, that works like charm!

I really thank you, I'll have to fix all my code, but it will be worth it!

I'm just unhapy that I don't really understand it, I really didn't get into phyton code, I barely know so little, but enough to make games.

Once again, Thanks, you've been super helpful!

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#11 Post by enaielei » Fri Sep 16, 2022 6:43 pm

I'm just unhapy that I don't really understand it, I really didn't get into phyton code, I barely know so little, but enough to make games.
Sorry if that was quite a lot. I didn't really bother cluttering the code with a lot of comments since most of the time ppl just want the easiest way possible making them disregard the fact that they don't fully understand how the code works. Also, it might sound condescending if I do that since I don't really know your level of understanding when it comes to code and stuff :).
But if you're happy to listen one more time, then I can probably spare the details and explanations on the things that you don't understand. Just list them out :)

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#12 Post by RebootLove » Fri Sep 16, 2022 7:24 pm

Well, my level is around...
I don't know yet the difference between default and define hehe
I'm developing some games since a year now, and even if I'm really noob at this, I got to learn what I needed to make the game. The part I don't understand the most is the phyton thing, but not sure if there's anything to understand really, I can imagine what it means most of it, but I really have a long way to learn to use renpy properly. But at least, for the moment "I just works" hehe

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Help with color variable

#13 Post by enaielei » Fri Sep 16, 2022 8:51 pm

Gotcha.
Here are the links that might help you.
I don't know yet the difference between default and define hehe
https://www.renpy.org/doc/html/python.h ... -statement
The part I don't understand the most is the phyton thing
If you're familiar with OOP, then you probably know most of the code.
__init__ is a constructor where you initialize stuff. __str__ a magic method that turns your object to a more user friendly text when represented in string.
"{color=%s}%s{/color}" % (self.color, self.name) is really just string interpolation.
Basically, the __str__ method here allows us to do [fo] instead of [fo.name].

RebootLove
Newbie
Posts: 8
Joined: Sat Oct 30, 2021 9:56 am
itch: https://rebootlove.i
Contact:

Re: Help with color variable

#14 Post by RebootLove » Fri Sep 16, 2022 10:37 pm

enaielei wrote:
Fri Sep 16, 2022 8:51 pm
Gotcha.
Here are the links that might help you.
I don't know yet the difference between default and define hehe
https://www.renpy.org/doc/html/python.h ... -statement
The part I don't understand the most is the phyton thing
If you're familiar with OOP, then you probably know most of the code.
__init__ is a constructor where you initialize stuff. __str__ a magic method that turns your object to a more user friendly text when represented in string.
"{color=%s}%s{/color}" % (self.color, self.name) is really just string interpolation.
Basically, the __str__ method here allows us to do [fo] instead of [fo.name].
Truth is, I know zero of programing, I just started from zero with renpy, no studies or even reading, just "trying" to make it work, google helped a lot to find solutions to my problems. Hehe. I should start learning since I'm doing my living from this, but... It just works, for now.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]