Re: Local variable referenced before assignment [SOLVED]

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
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: Local variable referenced before assignment [SOLVED]

#1 Post by HotMango »

I have an inventory system with mixing function.
When I select two items for the mixing process and click on "Start mixing" a function is called (MixItems)which checks if there is any combination between the two.
If there is, then a new item is added to the inventory while both used items are removed .
But I also want to reset variables that define which item is selected as "mix1", "mix2", selected_item
Check this:

Code: Select all

init python:
    class Item:
        def __init__(self, name, img, hover_img):
            self.name = name
            self.img = img
            self.hover_img = hover_img



    def addItem(theitem):
        inventory.append(theitem)

    def MixItems():
        if mix1 == iScissors:
            if mix2 == iHair:
                inventory.append(iEye)
                inventory.remove(iHair)
                inventory.remove(iScissors)
                mix1 = None
                mix2 = None
                selected_item = None


    def MInv():
        mix1 = None
        mix2 = None
        selected_item = None
I get an error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 52, in script
    "Halo"
  File "renpy/common/00action_other.rpy", line 537, in __call__
    rv = self.callable(*self.args, **self.kwargs)
  File "game/inventory_screen.rpy", line 20, in MixItems
    if mix1 == iScissors:
UnboundLocalError: local variable 'mix1' referenced before assignment

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

Full traceback:
  File "game/script.rpy", line 52, in script
    "Halo"
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\ast.py", line 708, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\exports.py", line 1345, in say
    who(what, *args, **kwargs)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\character.py", line 1142, in __call__
    self.do_display(who, what, cb_args=self.cb_args, **display_args)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\character.py", line 842, in do_display
    **display_args)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\character.py", line 591, in display_say
    rv = renpy.ui.interact(mouse='say', type=type, roll_forward=roll_forward)
  File "C:\Users\Business laptop\Downloads\Programs\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\Business laptop\Downloads\Programs\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\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 3518, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\screen.py", line 714, in event
    rv = self.child.event(ev, x, y, st)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 244, in event
    rv = d.event(ev, x - xo, y - yo, st)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\behavior.py", line 962, in event
    return handle_click(self.clicked)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\behavior.py", line 897, in handle_click
    rv = run(action)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\behavior.py", line 313, in run
    new_rv = run(i, *args, **kwargs)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\behavior.py", line 320, in run
    return action(*args, **kwargs)
  File "renpy/common/00action_other.rpy", line 537, in __call__
    rv = self.callable(*self.args, **self.kwargs)
  File "game/inventory_screen.rpy", line 20, in MixItems
    if mix1 == iScissors:
UnboundLocalError: local variable 'mix1' referenced before assignment

Windows-8-6.2.9200
Ren'Py 7.3.5.606
inventory tst 5 1.0
Wed Jan 22 13:05:44 2020
When I change the MixItem() function to something like:

Code: Select all

    def MixItems():
        if mix1 == iScissors:
            if mix2 == iHair:
                inventory.append(iEye)
                inventory.remove(iHair)
                inventory.remove(iScissors)
                MInv
    def MInv():
      mix1 = None
      mix2 = None
      selected_item = None
No items are deselected.
I appreciate any help.
Last edited by HotMango on Thu Jan 23, 2020 9:35 am, edited 1 time in total.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Local variable referenced before assignment

#2 Post by RicharDann »

If you want different functions to affect the same variables, you'll want to make them global instead of local, by creating them with default statement.

Code: Select all

# initialize/create the variables
default mix1 = None
default mix2 = None
default selected_item = None

init python:

    def MixItems():
        global mix1, mix2, selected_item #import the variables so the function can check and change them
        if mix1 == iScissors:
            if mix2 == iHair:
                inventory.append(iEye)
                inventory.remove(iHair)
                inventory.remove(iScissors)
                mix1 = None
                mix2 = None
                selected_item = None
The most important step is always the next one.

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: Local variable referenced before assignment

#3 Post by HotMango »

Thank you so much! This worked!
BTW, I had to add "global bla bla bla" to every function.
Another thing: What is the difference between local and global variable? and from what I understand, you make global variables by defining them using "default" statement, but then what is the point of using "global" statement?
Thanks again!

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: Local variable referenced before assignment

#4 Post by trooper6 »

You do make variables global by using the default statement, the problem comes with the functions. Functions will create brand new local variables by default. If you want the function to use one of your global variables you've already defined rather than create a local one, you have to tell it by saying "Hey! global mix1, mix2!"
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
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: Local variable referenced before assignment

#5 Post by HotMango »

Now I understand!
Thank you both for your help, much appreciated!

Post Reply

Who is online

Users browsing this forum: No registered users