Menu Error

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
User avatar
TheOneAndOnly-K
Regular
Posts: 78
Joined: Mon Apr 07, 2014 10:33 am
Contact:

Menu Error

#1 Post by TheOneAndOnly-K »

Is something wrong here? Can someone explain?

Code: Select all

label morning:
        stop music fadeout 0.5
        pause 0.5
        if Monday == True:
            $ Monday = False
            $ Tuesday = True
            
        if Sunday == True:
            $ Sunday = False
            $ Monday = True
            
        if Saturday == True:
            $ Saturday = False
            $ Sunday = True
            
        if Friday == True:
            $ Friday = False
            $ Saturday = True
            
        if Thursday == True:
            $ Thursday = False
            $ Friday = True
            
        if Wedensday == True:
            $ Wedensday = False
            $ Thursday = True
            
        if Tuesday == True:
            $ Tuesday = False
            $ Wedensday = True
Brings up the error

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 1076, in script
  File "game/script.rpy", line 1076, in python
NameError: name 'Monday' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "C:\Users\Mam\Downloads\Fish\Renpy\renpy\execution.py", line 294, in run
    node.execute()
  File "C:\Users\Mam\Downloads\Fish\Renpy\renpy\ast.py", line 1547, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\Mam\Downloads\Fish\Renpy\renpy\python.py", line 1416, in py_eval
    return eval(py_compile(source, 'eval'), globals, locals)
  File "game/script.rpy", line 1076, in <module>
    if Monday == True:
NameError: name 'Monday' is not defined

Windows-7-6.1.7601-SP1
Ren'Py 6.17.4.409

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Menu Error

#2 Post by Asceai »

You need to initialise variables before they can be used.

somewhere after label start: do this:

Code: Select all

$Monday = False
$Tuesday = False
$Wednesday = False
$Thursday = False
$Friday = False
$Saturday = False
$Sunday = False
Except set the starting day to True.

User avatar
TheOneAndOnly-K
Regular
Posts: 78
Joined: Mon Apr 07, 2014 10:33 am
Contact:

Re: Menu Error

#3 Post by TheOneAndOnly-K »

I've done that. It's still bringing up the error. It's doing this to a lot of my $ lately...

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Menu Error

#4 Post by Asceai »

After renaming 'Wedensday' to 'Wednesday' the code does work in a minimal test case:

Code: Select all

label start:
    $Monday = False
    $Tuesday = False
    $Wednesday = False
    $Thursday = False
    $Friday = False
    $Saturday = False
    $Sunday = False
    jump morning

label morning:
    stop music fadeout 0.5
    pause 0.5
    if Monday == True:
        $ Monday = False
        $ Tuesday = True
       
    if Sunday == True:
        $ Sunday = False
        $ Monday = True
       
    if Saturday == True:
        $ Saturday = False
        $ Sunday = True
       
    if Friday == True:
        $ Friday = False
        $ Saturday = True
       
    if Thursday == True:
        $ Thursday = False
        $ Friday = True
       
    if Wednesday == True:
        $ Wednesday = False
        $ Thursday = True
       
    if Tuesday == True:
        $ Tuesday = False
        $ Wednesday = True

User avatar
TheOneAndOnly-K
Regular
Posts: 78
Joined: Mon Apr 07, 2014 10:33 am
Contact:

Re: Menu Error

#5 Post by TheOneAndOnly-K »

I ran it again and it failed again ¬¬

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Menu Error

#6 Post by Asceai »

Okay, I'll explain. The error is because your variables have not been initialised.

That is the only cause of this error.

The solution is to initialise the variables.

That is what the code I have given you does.

Without more information nobody can help because the error is caused by not initialising variables, so saying you initialised the variables and you're still getting the error doesn't help, because the error exists because you didn't initialise the variables.

You need to initialise the variables.

That is all you need to do.

Initialise the variables.

Once you initialise the variables this problem will go away, because it is caused by not initialising the variables and not something else.

Initialising variables is easy.

All you need to do is set them to something before you use them.

Before you check the value of Monday in an 'if' statement, set Monday to something.

It can be False.

It can be True.

However, it needs to be set to something.

You must also make sure that when you set Monday to something, you do this before 'label morning' is run.

If 'label morning' is run in the middle of the game, not during init, this is easy because putting it after 'label start:' will ensure it is run.

You could also do it in an 'init' block but this is not ideal because then your variables will not be tracked by rollback until they appear for the first time, which will cause problems with saving and loading.

However, this will at least stop this error from occurring.

Initialise the variables.

The error you got, "NameError: name 'Monday' is not defined" says that ren'py doesn't know what 'Monday' is.

This can be fixed by setting 'Monday' to something before you check it.

For example, this will give an error:

Code: Select all

label start:
    if Monday == True:
        "It's Monday!"
    return
But this won't give an error:

Code: Select all

label start:
    $Monday = True
    if Monday == True:
        "It's Monday!"
    return
This is because, in the first case, Monday has not been defined before it is first seen.

In the second case, Monday has been defined.

This does not mean you want Monday to be set to True (or False) when you run 'morning', because this code is intended to advance the current day.

This means you need to define Monday and set it to something before this code is ever run.

Somewhere after 'label start:' is ideal, because it means the current week will be reset every time.

By the way, variable names are case-sensitive.

If you are setting the variable 'monday' and then checking the variable 'Monday' that will not work.

You must also not first set 'Monday' inside a python function.

In other words, if you are doing this:

Code: Select all

init python:
    def InitialiseWeek():
        Monday = False
label start:
    $InitialiseWeek()
...that will _not_ work, because InitialiseWeek gets its own scope.

By the way, 'define' would also work, but this is not recommended as it's got the same problems as initialising the variables within an init block.

So, while this will get rid of your error:

Code: Select all

define Monday = False
it will have the same problems, so it's not really recommended.
If you're having trouble getting stuff to run early enough and putting it after 'label start:' isn't working, you could always put it in a start_callback. This ensures it will be run before 'label start' and anything like that:

Code: Select all

init python:
    def InitialiseWeek():
        renpy.store.Monday = False
        renpy.store.Tuesday = False
        renpy.store.Wednesday = False
        renpy.store.Thursday = False
        renpy.store.Friday = False
        renpy.store.Saturday = False
        renpy.store.Sunday = False

    config.start_callbacks.append(InitialiseWeek)
Now, if that still doesn't work, the issue is that 'label morning' is being run during init, which of course won't work.

But either way, that is the only issue with this code: 'Monday' is not being declared before use.

You need to set it to something before checking what it is set to.

That is the only issue. When fixed, that error will disappear.

Now, I go into this in excruciating detail because this is a common issue with your posts.
You post that you have a problem with something, there's a bug in your code.
Someone gives a solution.
You then post that it doesn't work.
There's no new information.
Often, the solution is _the_ solution to the problem you have.
Like with initialising variables- the uninitialised variable error can be eliminated by initialising variables.
There isn't another solution to the problem.
So if you just post that it doesn't work, without giving more information, nobody can help you. We can't see your code. We can sort of imagine hypotheticals that you could possibly have- maybe after initialising the day variables, you then go and delete them all, but the chances of actually working out what you did are random at best. You could just post your entire project every time you have a problem, but it's more efficient for you and for us if you posted the problem area and gave sufficient information to enable us to solve it. In the case of this, posting all your code would probably be easiest.

User avatar
TheOneAndOnly-K
Regular
Posts: 78
Joined: Mon Apr 07, 2014 10:33 am
Contact:

Re: Menu Error

#7 Post by TheOneAndOnly-K »

Right I understand...give me a moment to check it over.

Post Reply

Who is online

Users browsing this forum: No registered users