Invalid syntax with naming player?

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
dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Invalid syntax with naming player?

#1 Post by dIgItayel »

I'm trying to do a thing for when my player names themself. If they name theirself the same name as a character in the game, they'll get a message. It's kind of like in Undertale. Basically, I have my code for naming the player, which works well, but I don't know how to do the same-name thing.

Code: Select all

 python:
        povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.")
        povname = povname.strip()
        if not povname:
            povname = "Alex"
        "hahaha...no?" if povname = "Sera"
Everything up until the last line works fine on it's own. The error I get is:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 46: invalid syntax
    "hahaha...no?" if povname = "Sera"
                               ^
    

Ren'Py Version: Ren'Py 6.99.13.2919
How should I make this work?

User avatar
erickcire95
Regular
Posts: 33
Joined: Tue Dec 26, 2017 7:38 pm
Contact:

Re: Invalid syntax with naming player?

#2 Post by erickcire95 »

A single "=" is used to set something, if you want to check for equalty you should use double, i.e. "=="

Try something like this:

Code: Select all

 
 python:
        povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.")
        povname = povname.strip()
        
        if povname == "Sera":
        	"Message about someone else having that name or anything"
        
        elif povname == "Character2":
        	"Message about someone else having that name or anything"
	
	#Add as many "elif" as needed
        
        elif not povname:
            povname = "Alex"
        "hahaha...no?" 
        
You might also avoid using the "elif not povname:" if you add 'or "Alex" ' in the first line of python, i.e.:

Code: Select all

 povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex" 
this way, if the input is empty (None) then it will be set to "Alex"

dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Re: Invalid syntax with naming player?

#3 Post by dIgItayel »

erickcire95 wrote: Thu Dec 28, 2017 1:28 am A single "=" is used to set something, if you want to check for equalty you should use double, i.e. "=="

Try something like this:

Code: Select all

 
 python:
        povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.")
        povname = povname.strip()
        
        if povname == "Sera":
        	"Message about someone else having that name or anything"
        
        elif povname == "Character2":
        	"Message about someone else having that name or anything"
	
	#Add as many "elif" as needed
        
        elif not povname:
            povname = "Alex"
        "hahaha...no?" 
        
You might also avoid using the "elif not povname:" if you add 'or "Alex" ' in the first line of python, i.e.:

Code: Select all

 povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex" 
this way, if the input is empty (None) then it will be set to "Alex"
I tried this and I was able to run the game, but it doesn't show the message when I name the character Sera, nor does it take the player back to the naming question. It just runs as it would had they put an "allowed" name. The name being blank and being put to "Alex" does still work though.

User avatar
erickcire95
Regular
Posts: 33
Joined: Tue Dec 26, 2017 7:38 pm
Contact:

Re: Invalid syntax with naming player?

#4 Post by erickcire95 »

Yeah, I just noticed the mistake, we are inside a python block so it is shown in the console but not in ren'py.
The $ symbol is used for sigle-line python statements, which is super helpful.

I didn't know you wanted the game to make you select another name :P .

Try with this one, I just tested it and it works fine

Code: Select all

    label naming_character:
        $ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
        $ povname = povname.strip()
        
        if povname == "Sera":
            "There is already a character named Sera. Please chose another name."
            jump naming_character
            
        elif povname == "Character2":
            "MESSAGE"
            jump naming_character
            
        elif povname == "Character3":
            "MESSAGE"
            jump naming_character
            
            #add as many "elif" as needed
            
        #Continue here with your sript
        "blah blah blah"
However, you must be aware that ren'py (as well as python) is case sensitive, i.e. "Sera" is not the same as "sera" or "SERA", so the player is still able to chose "SERA" as their name if you don't exclude it too.

dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Re: Invalid syntax with naming player?

#5 Post by dIgItayel »

erickcire95 wrote: Thu Dec 28, 2017 6:37 pm Yeah, I just noticed the mistake, we are inside a python block so it is shown in the console but not in ren'py.
The $ symbol is used for sigle-line python statements, which is super helpful.

I didn't know you wanted the game to make you select another name :P .

Try with this one, I just tested it and it works fine

Code: Select all

    label naming_character:
        $ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
        $ povname = povname.strip()
        
        if povname == "Sera":
            "There is already a character named Sera. Please chose another name."
            jump naming_character
            
        elif povname == "Character2":
            "MESSAGE"
            jump naming_character
            
        elif povname == "Character3":
            "MESSAGE"
            jump naming_character
            
            #add as many "elif" as needed
            
        #Continue here with your sript
        "blah blah blah"
However, you must be aware that ren'py (as well as python) is case sensitive, i.e. "Sera" is not the same as "sera" or "SERA", so the player is still able to chose "SERA" as their name if you don't exclude it too.
Ahhh thank you so much! It works perfectly. I appreciate the help :D

dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Re: Invalid syntax with naming player?

#6 Post by dIgItayel »

erickcire95 wrote: Thu Dec 28, 2017 6:37 pm Yeah, I just noticed the mistake, we are inside a python block so it is shown in the console but not in ren'py.
The $ symbol is used for sigle-line python statements, which is super helpful.

I didn't know you wanted the game to make you select another name :P .

Try with this one, I just tested it and it works fine

Code: Select all

    label naming_character:
        $ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
        $ povname = povname.strip()
        
        if povname == "Sera":
            "There is already a character named Sera. Please chose another name."
            jump naming_character
            
        elif povname == "Character2":
            "MESSAGE"
            jump naming_character
            
        elif povname == "Character3":
            "MESSAGE"
            jump naming_character
            
            #add as many "elif" as needed
            
        #Continue here with your sript
        "blah blah blah"
However, you must be aware that ren'py (as well as python) is case sensitive, i.e. "Sera" is not the same as "sera" or "SERA", so the player is still able to chose "SERA" as their name if you don't exclude it too.

Nevermind to my last comment haha, I came across an issue. Now, when I put any of the "not allowed" names, I only get the message you're supposed to get for putting your name as Sera. Here's my code, maybe I messed up:

Code: Select all

label naming:

    $ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
    $ povname = povname.strip()
        
    if povname == "Sera", "sera", "SERA":
        "???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
    elif povname == "Emery", "emery", "EMERY":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
    elif povname == "Kurou", "kurou", "KUROU":
        "???" "Aww, that's cute!"
        "???" "But we're going to need to change that sweetheart."
        "Please choose a different name."
        jump naming
    elif povname == "Albina", "albina", "ALBINA":
        "???" "oh! hahaha... that name's already taken, babe."
        "Please choose a different name."
        jump naming
    elif povname == "Gloria", "gloria", "GLORIA":
        "???" "Whoa! Someone wanted my name???"
        "???" "Bahaha, that's hilarious!"
        "???" "But there can only be ONE!"
        "Please choose a different name."
        jump naming
    elif povname == "Edward", "edward", "EDWARD":
        "???" "No."
        "Please choose a different name."
        jump naming
    elif povname == "Vihaan", "vihaan", "VIHAAN":
        "???" "Lolololololplololol"
        "Please choose a different name."
        jump naming
    elif povname == "Chaybree", "chaybree", "CHAYBREE":
        "???" "You already know about this easter egg?"
        "???" "I see."
        "Please choose a different name."
        jump naming
        

User avatar
erickcire95
Regular
Posts: 33
Joined: Tue Dec 26, 2017 7:38 pm
Contact:

Re: Invalid syntax with naming player?

#7 Post by erickcire95 »

The problem there is the way you are trying to exclude several names at the same time, instead of listing the names like ' Name, name, NAME ' try adding "or" conditions, something like this:

Code: Select all

if povname == "Sera" or povname == "sera" or povname == "SERA":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname == "Emery" or povname == "emery" or povname == "EMERY":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
and so on.

dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Re: Invalid syntax with naming player?

#8 Post by dIgItayel »

erickcire95 wrote: Thu Dec 28, 2017 8:13 pm The problem there is the way you are trying to exclude several names at the same time, instead of listing the names like ' Name, name, NAME ' try adding "or" conditions, something like this:

Code: Select all

if povname == "Sera" or povname == "sera" or povname == "SERA":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname == "Emery" or povname == "emery" or povname == "EMERY":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
and so on.
Thank you! That fixed it :)

User avatar
erickcire95
Regular
Posts: 33
Joined: Tue Dec 26, 2017 7:38 pm
Contact:

Re: Invalid syntax with naming player?

#9 Post by erickcire95 »

Even so, the player is still allowed to input the name in a weird case like "SerA" or "sERA" since those names are not listed. While it's not very likely that someone will write their name in such a weird way, there is that possibility.

HOWEVER

I just realized there is a faster way of fixing this, using the capitalize() method from Python, this method returns a copy of the string with its first character capitalized and the rest lowercased. ("nAMe" -> "Name")

So you can try changing it to

Code: Select all

if povname.capitalize() == "Sera":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname.capitalize() == "Emery":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
        
etc, which is a lot shorter since you no longer need to worry about names like "emery" or "EMERY". (Sorry for bringing this solution until now)

Another option is to add this method right after the name input:

Code: Select all

$ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
$ povname = povname.strip()
$ povname = povname.capitalize()
This last option is useful if you want to "generalize" the name of the player (making the first letter uppercase and the rest lowercased)
If you decide to use this after the name input, then your code after the input should look like

Code: Select all

if povname == "Sera":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname == "Emery":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
        
The only difference from these two options is that in the first one your players will be allowed to set names like ALEX, eric, jOhN, MIchAEl, etc,
and in the second option the player's name will always look "normal" (like Alex, Erick, John, Michael, etc)
It's up to you what you decide to use.

Again, excuse me for bringing this until the last moment, my brain is not working too well today :?

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Invalid syntax with naming player?

#10 Post by Milkymalk »

Forgive me my interruption, but wouldn't

Code: Select all

$ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
just set povname to True because "or" in (something or "Alex") is a logical operator and "Alex" is always True because it's a non-empty string? I'm not able to try it for myself at the moment, but from all I know about Python, this should be the case.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
erickcire95
Regular
Posts: 33
Joined: Tue Dec 26, 2017 7:38 pm
Contact:

Re: Invalid syntax with naming player?

#11 Post by erickcire95 »

Milkymalk wrote: Thu Dec 28, 2017 11:20 pm Forgive me my interruption, but wouldn't

Code: Select all

$ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
just set povname to True because "or" in (something or "Alex") is a logical operator and "Alex" is always True because it's a non-empty string? I'm not able to try it for myself at the moment, but from all I know about Python, this should be the case.
No, it works fine, I tested it myself and also was already tested by OP.
Indeed even in the tutorial it's done this way.

dIgItayel
Regular
Posts: 32
Joined: Fri Mar 24, 2017 10:27 am
Contact:

Re: Invalid syntax with naming player?

#12 Post by dIgItayel »

erickcire95 wrote: Thu Dec 28, 2017 9:13 pm Even so, the player is still allowed to input the name in a weird case like "SerA" or "sERA" since those names are not listed. While it's not very likely that someone will write their name in such a weird way, there is that possibility.

HOWEVER

I just realized there is a faster way of fixing this, using the capitalize() method from Python, this method returns a copy of the string with its first character capitalized and the rest lowercased. ("nAMe" -> "Name")

So you can try changing it to

Code: Select all

if povname.capitalize() == "Sera":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname.capitalize() == "Emery":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
        
etc, which is a lot shorter since you no longer need to worry about names like "emery" or "EMERY". (Sorry for bringing this solution until now)

Another option is to add this method right after the name input:

Code: Select all

$ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
$ povname = povname.strip()
$ povname = povname.capitalize()
This last option is useful if you want to "generalize" the name of the player (making the first letter uppercase and the rest lowercased)
If you decide to use this after the name input, then your code after the input should look like

Code: Select all

if povname == "Sera":
	"???" "hahaha, um....please don't."
        "Please choose a different name."
        jump naming
        
elif povname == "Emery":
        "???" "T-Thank you!"
        "???" "..."
        "???" "Change it immediately."
        "Please choose a different name."
        jump naming
        
The only difference from these two options is that in the first one your players will be allowed to set names like ALEX, eric, jOhN, MIchAEl, etc,
and in the second option the player's name will always look "normal" (like Alex, Erick, John, Michael, etc)
It's up to you what you decide to use.

Again, excuse me for bringing this until the last moment, my brain is not working too well today :?
This works great, thank you :D

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

Re: Invalid syntax with naming player?

#13 Post by Ocelot »

just set povname to True because "or" in (something or "Alex") is a logical operator and "Alex" is always True because it's a non-empty string? I'm not able to try it for myself at the moment, but from all I know about Python, this should be the case.
In Python logical operators return last checked value (for or chain - first equivalent to True, for and i first equivalent to False, last one otherwise)

I would sugges against using capitalize for names. People know better how their name or nickname is written.

Instead use second variable, which holds name in all lowerase or uppercase for comparing. Do not mess with how player wants his name to be displayed.

Bonus: do you want to disallow names as Sеra, Serа or Sеrа too?
< < insert Rick Cook quote here > >

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Invalid syntax with naming player?

#14 Post by Milkymalk »

Ocelot wrote: Fri Dec 29, 2017 3:04 pmIn Python logical operators return last checked value (for or chain - first equivalent to True, for and i first equivalent to False, last one otherwise)
Ah, that's very good to know! Thank you!
I would sugges against using capitalize for names. People know better how their name or nickname is written.
The name is not really capitalized, only the return of name.capitalize() is used as a standardized spelling in order to compare it to the reserved names. It shouldn't change the string itself. Unless I am VERY wrong this time :D
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

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

Re: Invalid syntax with naming player?

#15 Post by Ocelot »

Milkymalk wrote: Sat Dec 30, 2017 2:18 am The name is not really capitalized
I was referrring to this post:
erickcire95 wrote: Thu Dec 28, 2017 9:13 pmAnother option is to add this method right after the name input:

Code: Select all

$ povname = renpy.input("Enter your name. If you don't put one, a name will be chosen automatically.") or "Alex"
$ povname = povname.strip()
$ povname = povname.capitalize()
I should have quoted it (or at least mentioned what exact post I am commenting on) to avoid confusion.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: No registered users