0 votes

I want to make a inventory system, however the elif ladder i have set up currently is not automated enough, and needlessly lengthy

Here's the current inventory system:

func addtoinv(item, value):#Inventory management, needs to be optimized better
    if item == "itemtest":
        if itemenabled.Itemtest == true:
            itemquantity.Itemtest += value
            itemenabled.Itemtest = true
    elif item == "coinage":
        if itemenabled.Coinage == true:
            itemquantity.Coinage += value
            itemenabled.Itemtest = true

As you can see everytime I add a collectible item to my game, I have to have a ladder with itemenabled.the_item_I_want . Instead of checking if that item is there.
Now with my new system I am using itemenabled.has(the_item_I_want)

But I've hit a roadblock, I can detect the item, but I can't add to the item. How can I add/change a specific structure variable, based on an outside variable.

The one i'm currently trying to make:

func addtoinv(item, value):
    if itemenabled.has(item)==true:#collect the item.
        itemquantity.values().has(item) += value #add to item
    elif itemenabled.has(item)==false:# enabled the new item.
        itemenabled = true#WIp change the item's stance

P.S. If your wondering why I have two structures, It's because I want a pop up to occur when the player picks up an item for the first time.

in Engine by (272 points)
edited by

You may save itemenabled etc. as arrays, e.g itemenabled = [item1, item2], so you may use Array.has(). If you want get property you may use get() but that isn't best way to do it.

1 Answer

0 votes
Best answer

Okay so there is dictionary.item, but i can just have dictionary[item] which allows for more creative room, including adding/subtracting variables in the dictionary. So my itemquantity now doesn't need any variables in it, it will just be added in game.

As long as I declare a value before I add to it like so.

func addtoinv(item, value):#Inventory management
    if itemquantity.has(item) && itemquantity[item] > 0:#If the item is apart of the structure and it's enabled, then collect the item.
        set_dialog("Picked up "+str(item))
        itemquantity[item] += value
    elif !itemquantity.has(item):#If the item is not apart of the structure, then play a text message
        set_dialog("I found a "+str(item)+" What Joy!")
        itemquantity[item] = 0
        itemquantity[item] += value
    #Else the Item is not apart of the structure, then it is ignored
    print(str(item) + str(value))
by (272 points)

Thank you Godot Docs.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.