Need help with custom name coding

TheTypist

Active Member
Donor
Game Developer
Sep 7, 2017
670
3,994
Hey guys, I’m working on my game called “Because I Love Her” and just released the demo yesterday.
I tried so hard to figure this out before release, but released the demo without the feature of allowing players to name the main characters.
Please, I need help with that code in the script!
Something like choosing the main characters names, as well as replacing all the times the characters say the names with the custom name chosen. So if the name is John now, and the other character Claire says “hey john” but the player chose “bill” Claire will now say “hey bill” in all of the script where the name is said.
Thank you ahead of time for your help.
 

the66

beware, the germans are cumming
Modder
Respected User
Donor
Jan 27, 2017
7,607
23,570
let say the var mc_name holds your custom name
with
Code:
init:
    define mc = DynamicCharacter("mc_name")
can you create your sayer

to get your custom name
Code:
    $ mc_name = renpy.input("What is your name?")
    $ mc_name = mc_name.strip()
    if mc_name == "":
        $ mc_name = "John"
and if you want to reference to your custom name in the dialogue, use
Code:
mc "...my name is [mc_name], bla bla..."
 
  • Like
Reactions: TheTypist

Rich

Old Fart
Modder
Respected User
Donor
Game Developer
Jun 25, 2017
2,469
6,936
Alternately, as opposed to using DynamicCharacter, you can use
Code:
define mc = Character("[mc_name]", dynamic=True)
DynamicCharacter is just a shortcut for the above
 

TheTypist

Active Member
Donor
Game Developer
Sep 7, 2017
670
3,994
Alternately, as opposed to using DynamicCharacter, you can use
Code:
define mc = Character("[mc_name]", dynamic=True)
DynamicCharacter is just a shortcut for the above
Thank you for the reply, I was able to do so using:

Code:
scene downtown
    $ n = renpy.input("What is your name?")
    $ n = n.strip()
    if n == "":
        $ n="John"
    $ c = renpy.input("What is your fiances name?")
    $ c = c.strip()
    if c == "":
        $ c="Claire"
In order to change names that were already in the script to save time, I had to create a dictionary word replace type of code I found on another forum:
Code:
init python:
    import re
    def word_replace(text):
        """
        take a text and replace words that match the key in a dictionary
        with the associated value, return the changed text
        """

        def translate(match):
            word = match.group(0)
            return replace_dict.get(word, word)
      
        return re.sub(r"\b[A-Za-z_]\w*\b", translate, text)

# create a dictionary of key_word:replace_with pairs
    replace_dict = {
        "John" : "[n]",
        "JOHN" : "[n]",
        "john" : "[n]",
        "Claire" : "[c]",
        "CLAIRE" : "[c]",
        "claire" : "[c]",
        
    }

define config.say_menu_text_filter =  word_replace
The first code MUST be under label: start in order to work, whilst the second must be above label:start
 

TheTypist

Active Member
Donor
Game Developer
Sep 7, 2017
670
3,994
let say the var mc_name holds your custom name
with
Code:
init:
    define mc = DynamicCharacter("mc_name")
can you create your sayer

to get your custom name
Code:
    $ mc_name = renpy.input("What is your name?")
    $ mc_name = mc_name.strip()
    if mc_name == "":
        $ mc_name = "John"
and if you want to reference to your custom name in the dialogue, use
Code:
mc "...my name is [mc_name], bla bla..."
Also thank you for posting the correct code, appreciate your fast response earlier
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,145
14,829
I'll never understand why so many people bother with this external variable :/

Code:
default mc = Character( "John" )

label start:
    $ n = renpy.input("What is your name ?\nKeep blank for the default name (John).").strip()
    if n != "":
        # Ensure that the first letter is upper case and the other lower case.
        $ mc.name = n[:1].upper() + n[1:].lower()
    "The main character will be named [mc.name]"
 

the66

beware, the germans are cumming
Modder
Respected User
Donor
Jan 27, 2017
7,607
23,570
because, if you want to change the name later, you have to
Code:
$ n = 'New Name'
$ mc.name = n
but when using
Code:
init:
    define mc = DynamicCharacter("mc_name")
you only have to
Code:
$ mc_name = 'New Name'
and
Code:
n[:1].upper() + n[1:].lower() -> n.title()
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,145
14,829
but when using
Code:
init:
    define mc = DynamicCharacter("mc_name")
Then don't use define which don't save the variable, and prefer the default statement, which make the variable savable.
Oh, and also, both statements create their own init context. They absolutely don't need to be put in an init block.