NameError: name "inventory" is not defined

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
HiddenLife
Newbie
Posts: 13
Joined: Wed Sep 09, 2020 8:10 pm
Contact:

NameError: name "inventory" is not defined

#1 Post by HiddenLife » Fri Dec 18, 2020 7:49 am

Hi everyone. I'm making a fantasy themed visual novel and I wanna implement an inventory system.
I used the second example of this tutorial: https://www.renpy.org/wiki/renpy/doc/co ... ney_System and as long as I only pasted, copied and modified the code, everything worked. But, when I tried to move some piece of the tutorial's modified code in order to fit it in my code, the game, every time, and only when I try to use some button that use the inventory, send this error:
[issue]NameError: name 'inventory' is not defined[/issue]
I don't know what to do. I tried something, like moving the tutorial's code in other points of my code, but nothing changed.
Thank to everyone.

Here's what matter of my code.

Code: Select all

init:
    image white = Solid((255, 255, 255, 255))
    # MC Variables
    $ strength = 1
    $ intelligence = 1
    $ charisma = 1
    $ level = 1
    # Time
    $ day = 1
    $ daytime = 0
    # Utility
    $ random = 0
    transform transformTop:
        xpos 0
        ypos -2000
        easein 10 xpos 0 ypos 0

    python:
        class Item:
            def __init__(self, name, cost):
                self.name = name
                self.cost = cost
        class Inventory:
            def __init__(self, money=0):
                self.money = money
                self.items = []
            def buy(self, item):
                if self.money >= item.cost:
                    self.money -= item.cost
                    self.items.append(item)
                    return True
                else:
                    return False
            def earn(self, amount):
                self.money += amount
            def hasitem(self, item):
                    if item in self.items:
                        return True
                    else:
                        return False
            def additem(self, item):
                self.items.append(item)

################################################################################

label map_menu:
    scene map
    menu:
        "Go find some loot.":
            jump loot

################################################################################
# loot system

label loot:
    scene map
    menu:
        "Where I go to gather some loot?"

        "Zone 1":
            $ random = renpy.random.randint(1,20)
            jump zone1_loot
        "Zone 2":
            $ random = renpy.random.randint(1,20)
            jump zone2_loot
        "Zone 3":
            $ random = renpy.random.randint(1,31)
            jump zone3_loot
        "Zone 4":
            $ random = renpy.random.randint(1,20)
            jump zone4_loot
        "Zone 5":
            $ random = renpy.random.randint(1,20)
            jump zone5_loot
        "Go back":
            jump map_menu

label zone1_loot:
    scene zone1_loot
    if level < 1:
        "Your level is too low for this floor."
        jump loot
    else:
        if random >= 15:
            "You found a Dire Wolf!"
            $ inventory.additem(wolffur)
            "1 wolf's fur added to your inventory."
            if daytime >= 3:
                $ daytime = 0
            else:
                $ daytime = daytime + 1
            $ random = 0
            jump amp_menu
        if random >= 8:
            "You found a Thicket Spider!"
            $ inventory.additem(spiderleg)
            "1 spider's leg added to your inventory."
            if daytime >= 3:
                $ daytime = 0
            else:
                $ daytime = daytime + 1
            $ random = 0
            jump map_menu
        if random >= 0:
            "You found a Frenzy Boar!"
            $ inventory.additem(boarfur)
            "1 boar's fur added to you inventory."
            if daytime >= 3:
                $ daytime = 0
            else:
                $ daytime = daytime + 1
            $ random = 0
            jump map_menu

################################################################################
# The game starts here.
label start:

#settingupitems
    $ money = inventory.money
    python:
        inventory = Inventory()
        wolffur = Item("Wolf's Fur", 3)
        spiderleg = Item("Spider's Leg", 2)
        boarfur = Item("Boar's Fur", 1)
    $ wolffurcost = wolffur.cost
    $ spiderlegcost = spiderleg.cost
    $ boarfurcost = boarfur.cost

    “Here’s the story”
    jump map_menu

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

Re: NameError: name "inventory" is not defined

#2 Post by Alex » Fri Dec 18, 2020 9:08 am

The issue is that Ren'Py executes your code line by line in order.
So, in this part you must declare 'inventory' before using it for declaring 'money' variable.

Code: Select all

# The game starts here.
label start:

#settingupitems
    $ money = inventory.money
    python:
        inventory = Inventory()
Should be

Code: Select all

# The game starts here.
label start:
    $ inventory = Inventory()

#settingupitems
    $ money = inventory.money

HiddenLife
Newbie
Posts: 13
Joined: Wed Sep 09, 2020 8:10 pm
Contact:

Re: NameError: name "inventory" is not defined

#3 Post by HiddenLife » Fri Dec 18, 2020 3:13 pm

Alex wrote:
Fri Dec 18, 2020 9:08 am
The issue is that Ren'Py executes your code line by line in order.
So, in this part you must declare 'inventory' before using it for declaring 'money' variable.
Should be

Code: Select all

# The game starts here.
label start:
    $ inventory = Inventory()
#settingupitems
    $ money = inventory.money
Thank for the support. I did just as you said, putting money after inventory. Sadly nothing changed. The error happen when i try to execute "inventory.additem()", so, in the case of my code, after I click on one of the zones.
In case it can be useful. After clicking, appear the message "You found a..." and, after I click, the error happen.

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

Re: NameError: name "inventory" is not defined

#4 Post by Alex » Fri Dec 18, 2020 3:44 pm

Try to start game from the beginning (don't use previously made saves).
Also, try to declare the 'inventory' like

Code: Select all

default inventory = Inventory()
# The game starts here.
label start:
#settingupitems
    $ money = inventory.money
https://www.renpy.org/doc/html/python.h ... -statement

HiddenLife
Newbie
Posts: 13
Joined: Wed Sep 09, 2020 8:10 pm
Contact:

Re: NameError: name "inventory" is not defined

#5 Post by HiddenLife » Fri Dec 18, 2020 5:39 pm

Alex wrote:
Fri Dec 18, 2020 3:44 pm
Try to start game from the beginning (don't use previously made saves).
Also, try to declare the 'inventory' like

Code: Select all

default inventory = Inventory()
# The game starts here.
label start:
#settingupitems
    $ money = inventory.money
Thank you. Now everything work just fine. It was the old save the problem.

Post Reply

Who is online

Users browsing this forum: CharlieFuu69, Google [Bot], Ocelot