[Solved] Syntax for menu error, please help

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
Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

[Solved] Syntax for menu error, please help

#1 Post by Andwiiger »

I'm trying to write something that allows the game to ask the player to accept or change the name of a character.
Here's the code I have

Code: Select all

 
class Name_Accept(store.object):
    def __init__(self, current_name="", whatCharacter=""):
          self.current_name = current_name
          self.whatCharacter = whatCharacter

     def accepting(self, current_name):
         "Do you want to name [self.whatCharacter] [current_name]?"

menu:
    "Yes":
        return

    "No":
        $ old_name = current_name
        $ current_name = renpy.input("What do you want to change the name to?")
        $ current_name = current_name.strip()
        if(current_name == old_name):
            "If you didn't want to change the name you could have just said no"
            "The name has been reset to [old_name]"
            $ current_name = old_name
            return
        else:
            call Name_Accept.accepting(current_name): 
This gives me an error on syntax of menu. Could someone please tell me why it gives me the error?
Last edited by Andwiiger on Tue May 29, 2018 5:22 pm, edited 3 times in total.

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: Syntax for menu error, please help

#2 Post by Milkymalk »

You are mixing renpy script (the menu) with python.

You can only use menus inside regular renpy script:

Code: Select all

label askforname:
    menu:
        "Yes":
            pass # necessary because empty blocks are not allowed
        "No":
            $ old_name = current_name
            $ current_name = renpy.input("What do you want to change the name to?")
            $ current_name = current_name.strip()
            if(current_name == old_name):
                "If you didn't want to change the name you could have just said no"
                "The name has been reset to [old_name]"
                $ current_name = old_name
            else:
                $ Name_Accept.accepting(current_name)
You also don't "call" functions, only screens and labels.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

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

Re: Syntax for menu error, please help

#3 Post by kivik »

I'm not sure if the above code would work since 1) it'd require an instance of the object to work, 2) the original logic doesn't make sense anyway


OP:
You have created a condition where if someone puts in the same name, THEN choose no to renaming the character, would trigger a reset of new name and old name (even though the new name is already the same as the old name), and then stop allowing player to change the name. That makes no sense - if player chooses No to accepting the the name change, the should be given the opportunity to try again. So the logic doesn't make sense to me. It'd make more sense if you put that condition in the Yes choice if someone chooses the exact same name.

Also you're trying to use recursion (calling your own function again) if player chooses No, that's a bad idea for something that could in theory goes on infinitely. Recursion should only be done to things that as a finite depth / bottom or it can crash your program when the computer runs out of resources remembering all the instances of a recursion being run.

So, may I recommend you use a label in place of a class to do the whole thing, and use a loop instead of recursion? I don't know how you actually hold your character's names, so I've created a Char() class that holds it for this example, you need to change it so it's relevant to your own character classes:

Code: Select all

label name_change(character):
    $ renpy.dynamic("new_name", "loop") # declare local variables
    $ loop = True
    $ new_name = "" # prevent key error on quit
    while loop:
        $ new_name = renpy.input("What do you want to change [character.name]'s name to?").strip()
        menu:
            "Do you want to rename [character.name] to [new_name]?"
            "Yes":
                $ loop = False # this ends the loop
                "[character.name]'s name is now [new_name]"
                $ character.name = new_name
            "No":
                pass # this will repeat the loop
    return

init python:
    class Char:
        def __init__(self, name):
            self.name = name

label start:
    $ eileen = Char("Eileen")
    call name_change(eileen)
    "End"
    return

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#4 Post by Andwiiger »

Milkymalk and Kivik: Thanks alot to you both, I managed to make it work the way I wanted. The only problem I have is that I get an exception error required parameter current_name has no value at the end. The error only occurs when the rest of the code is done running, and it's the last thing before it goes back to main menu.
Here's what I have:

Code: Select all

label name_accept(current_name, whatCharacter, capitalCharacter):
    $ new_name = ""
    "Do you want to name [whatCharacter] [current_name]?"

    menu:
        "Yes ":
            pass

        "No":
            $ new_name = renpy.input("What do you want to change [current_name] to?")
            $ new_name = new_name.strip()
            "[captialCharacter]'s name has been changed to [new_name]"
            $ current_name = new_name

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

Re: Syntax for menu error, please help

#5 Post by kivik »

So this is to do with variable scopes. There's this concept of global and local variables. Global variables are the variables that you declare in your game at the start typically - they can be referenced anywhere and be manipulated. Local variables on the other hand only exists within the block of code they're created for, so this will be a label block, a screen, a function etc.

Local variables are discarded at the end of the code block.

The confusing thing is, you can have a local variable the same name as a global variable - but they're actually different variables. And this is what happened here:

Parameters are local variables - so when you pass current_name to your name_accept label, a local version of the variable current_name is created and used, and thus your global version of current_name is not affected.


Now before suggesting solutions, we need to know exactly what you're trying to do with your code, and by that I mean, do you have multiple characters you allow renaming of? How are the character's stored in your game if there're multiple? Without the full scope of the problem and an understanding of exactly what you're trying to do - we can be giving you wrong suggestions.


Your code currently doesn't make any sense to me based on the above, you've removed the loop that was there to allow you to change your mind about renaming the character - so it's very confusing to me why you've done that and what you're trying to achieve.

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#6 Post by Andwiiger »

Ok so what I do is define a character and call the name_accept:

Code: Select all

$ player_name = renpy.input("Feel free to give him a name, or hit enter for default name \"Michael\"")
    $ player_name = player_name.strip()

    if (player_name == ""):
        $ player_name = "Michael"

    call name_accept(player_name, "the main character", "The Main Character")
Then there's basically the same with other character.

Code: Select all

$ friend_name = renpy.input("Give the friend a name")
          
         call name_accept(friend_name, "the best friend", "The Best Friend")
Here's the name_accept just for the sake of simplicity:

Code: Select all

label name_accept(current_name, whatCharacter, capitalCharacter):
    $ new_name = ""
    "Do you want to name [whatCharacter] [current_name]?"

    menu:
        "Yes ":
            pass

        "No":
            $ new_name = renpy.input("What do you want to change [current_name] to?")
            $ new_name = new_name.strip()
            "[captialCharacter]'s name has been changed to [new_name]"
            $ current_name = new_name

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

Re: Syntax for menu error, please help

#7 Post by kivik »

Okay, so to expand on what I was saying in my last reply about the scope of variables: passing a variable into a function / label doesn't mean at the end of it the variable will be changed by the function - objects are different, you can pass an object it, and the object's attributes can be changed - it's all to do with how programming languages use pointers.


If you're just storing names in variables, we need to pass the variable names as string to your function, and a special function that dynamically sets a variable by their name:

Code: Select all

label name_accept(current_name, whatCharacter, capitalCharacter, variable_name):
    $ new_name = ""
    "Do you want to name [whatCharacter] [current_name]?"

    menu:
        "Yes ":
            $ globals()[variable_name] = current_name
            pass

        "No":
            $ new_name = renpy.input("What do you want to change [current_name] to?")
            $ new_name = new_name.strip()
            "[captialCharacter]'s name has been changed to [new_name]"
            $ globals()[variable_name] = new_name
Then use the label like this:

Code: Select all

call name_accept(player_name, "the main character", "The Main Character", "player_name")
call name_accept(friend_name, "the best friend", "The Best Friend", "friend_name")
The way it works is it accesses the global variable using the globals() function - which returns a dictionary of all global variables by name - and you set it that way.

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#8 Post by Andwiiger »

It still does the same thing, unfortunately. Goes through the code without problems, and then it gives me the exception of current_name has no value.

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

Re: Syntax for menu error, please help

#9 Post by kivik »

Can you show us the actual error message and the associated lines?

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#10 Post by Andwiiger »

https://imgur.com/a/OJPVpUE
Here's an image of the exception I get.

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: Syntax for menu error, please help

#11 Post by Milkymalk »

Did you call the label name_accept with parameters? There should be 4.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#12 Post by Andwiiger »

Code: Select all

call name_accept(player_name, "the main character", "The Main Character", "player_name")
This is how I call it

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

Re: Syntax for menu error, please help

#13 Post by kivik »

Do you end your label blocks with a return statement? I found the same error message in another post in this forum and the offending code came from the lack of a return statement for a previous label block directly before the problem label block.

So I'm guessing you've put your name_accept label right at the end of your code, and whatever label it was beforehand doesn't have a return statement, therefore instead of ending the game, it follows through to the next label block - which is the name_accept label, but since it's not being called and is being executed directly by game flow, no parameters are passed and thus you get the error message.

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Syntax for menu error, please help

#14 Post by Andwiiger »

Adding the return statement to the previous label block worked! Now everything works as intended, until the next mistake appears :P

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], trailsiderice