[Solved] Variable losing definition upon using 'return' command

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
Palas
Newbie
Posts: 15
Joined: Thu Feb 14, 2013 3:19 pm
Soundcloud: palasvuash
itch: pequigames
Location: Brazil
Contact:

[Solved] Variable losing definition upon using 'return' command

#1 Post by Palas »

Hello! I have been getting this strange exception upon returning to the main menu.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
NameError: name 'robo_mesa' is not defined

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

Full traceback:
  File "renpy/common/_layout/screen_main_menu.rpym", line 28, in script
    python hide:
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "renpy/common/_layout/screen_main_menu.rpym", line 28, in <module>
    python hide:
  File "renpy/common/_layout/screen_main_menu.rpym", line 35, in _execute_python_hide
    ui.interact()
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\ui.py", line 297, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\core.py", line 2702, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\core.py", line 3194, in interact_core
    self.draw_screen(root_widget, fullscreen_video, (not fullscreen_video) or video_frame_drawn)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\core.py", line 2094, in draw_screen
    renpy.config.screen_height,
  File "renpy/display/render.pyx", line 490, in renpy.display.render.render_screen (gen\renpy.display.render.c:6805)
    rv = render(root, width, height, st, st)
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 722, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\transition.py", line 360, in render
    bottom = render(self.old_widget, width, height, st, at)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 722, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 722, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "accelerator.pyx", line 110, in renpy.display.accelerator.transform_render
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\image.py", line 500, in render
    return wrap_render(self.target, width, height, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\image.py", line 306, in wrap_render
    rend = render(child, w, h, st, at)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 336, in render
    surf = render(self.child, width, height, st, at)
  File "renpy/display/render.pyx", line 166, in renpy.display.render.render (gen\renpy.display.render.c:4075)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 259, in renpy.display.render.render (gen\renpy.display.render.c:3520)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 1283, in render
    self.update(st, at)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 1265, in update
    child, redraw = self.function(st, at, *self.args, **self.kwargs)
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 1341, in condition_switch_show
    return condition_switch_pick(switch), None
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\display\layout.py", line 1334, in condition_switch_pick
    if renpy.python.py_eval_bytecode(code):
  File "C:\Users\Acer\Documents\RenPy\renpy-7.3.5-sdk\renpy\python.py", line 2052, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "<none>", line 1, in <module>
NameError: name 'robo_mesa' is not defined

Windows-8-6.2.9200
Ren'Py 7.3.5.606
D08C02A 1.0
Mon Dec 02 20:25:43 2019
"robo_mesa" is a variable that determines which image should appear on a certain screen:

Code: Select all

image robomesa = ConditionSwitch(
        "robo_mesa == 'aranha'","Robos/h_mon_aranha.png",
        "robo_mesa == 'cachorro'","Robos/h_mon_cachorro.png",
        "robo_mesa == 'cavalo'","Robos/h_mon_cavalo.png",
        "robo_mesa == 'cobra'","Robos/h_mon_cobra.png",
        "robo_mesa == 'elefante'","Robos/h_mon_elefante.png",
        "robo_mesa == 'flamingo'","Robos/h_mon_flamingo.png",
        "robo_mesa == 'gato'","Robos/h_mon_gato.png",
        "robo_mesa == 'passaro'","Robos/h_mon_passaro.png",
        "robo_mesa == 'siri'","Robos/h_mon_siri.png",
        "robo_mesa == 'sucata'","Robos/h_mon_sucata.png",
        "robo_mesa == 'nada'","Robos/h_mon_nada.png",
        "True", "Robos/h_mon_nada.png" #Just, you know. To be sure
        )
Said variable is determined by a choice that happens every scene. The problem is, if I use "return" while in the screen in which the image "robomesa" appears, I get that exception. But the image shows up just fine and anything else I do before actually returning to the main menu works too. I'm puzzled - why would the main menu erase this particular variable's definitions or call it, but not any other? I haven't even touched the main menu's default code.

Now, sure, I can make it persistent, but that seems suboptimal. I'm also curious about what is happening, so if you guys could help me, I'd really appreciate it!

Also, the version used is 7.3.5 (the latest as of Dec 03)

EDIT after solving: All I had to do was to declare the variable with default. Thanks, trooper6 and Alex!
Last edited by Palas on Tue Dec 03, 2019 1:49 pm, edited 1 time in total.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Variable losing definition upon using 'return' command

#2 Post by trooper6 »

Have you initialized rob_mesa before the start label using default?

Code: Select all

default robo_mesa = "None"

label start:
    "Blah Blah"
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Palas
Newbie
Posts: 15
Joined: Thu Feb 14, 2013 3:19 pm
Soundcloud: palasvuash
itch: pequigames
Location: Brazil
Contact:

Re: Variable losing definition upon using 'return' command

#3 Post by Palas »

trooper6 wrote: Tue Dec 03, 2019 1:15 am Have you initialized rob_mesa before the start label using default?

Code: Select all

default robo_mesa = "None"

label start:
    "Blah Blah"
I hadn't, and now I did, and now it works! Thank you very much. But why does initializing with default works, but initializing/defining with $ doesn't?

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Variable losing definition upon using 'return' command

#4 Post by Alex »

Check this article about similar issues - https://www.renpy.org/doc/html/save_load_rollback.html

Test your code now, I presume that after 'return' your image bacame the default one... If so, try to do it like - https://www.renpy.org/doc/html/save_loa ... after-load

User avatar
Palas
Newbie
Posts: 15
Joined: Thu Feb 14, 2013 3:19 pm
Soundcloud: palasvuash
itch: pequigames
Location: Brazil
Contact:

Re: Variable losing definition upon using 'return' command

#5 Post by Palas »

Alex wrote: Tue Dec 03, 2019 1:11 pm Check this article about similar issues - https://www.renpy.org/doc/html/save_load_rollback.html

Test your code now, I presume that after 'return' your image bacame the default one... If so, try to do it like - https://www.renpy.org/doc/html/save_loa ... after-load
I see! Thanks a lot~ I'll mark this as solved now.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: [Solved] Variable losing definition upon using 'return' command

#6 Post by trooper6 »

define is for initializing variables that won't change ever, like:

define days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

default is for initializing variables that will change.

$ is just telling Renpy that you are giving it a line of python code. Use that to change variables after you've initialized it, but it won't initialize the variable.

Hope that helps.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [Solved] Variable losing definition upon using 'return' command

#7 Post by Imperf3kt »

Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: Google [Bot], Kocker