#Dictionary Confusion

1 messages · Page 1 of 1 (latest)

glad ore
#

This adds it in a foreach loop:

        {
           foreach(Item item in items)
            {
                if (slot.slotName == item.itemName)
                {
                    //we've found the right item for the right slot
                    slot.slotObject.GetComponentInChildren<TextMeshProUGUI>().text = item.uses.ToString();
                    slot.slotObject.GetComponentInChildren<Image>().sprite = item.icon;

                    itemDictionary.Add(slot.slotName, item);
                    print(itemDictionary.GetValueOrDefault(slot.slotName, items[1]).itemName);
                }
            }
        }
#

And this supposedly accesses the information:

        Item currentItem = itemDictionary.GetValueOrDefault(slotName, items[0]);
        int newUses = currentItem.uses--;
#

I'm not sure I implemented correctly tho

hidden jewel
#

and why do you think something is wrong?

glad ore
#

Because it's not working

#

Wait I'm gonna do some tests

hidden jewel
#

what does "not working" mean?

glad ore
#

I want when I press a button to decrease the number of uses of a certain item but in this code snipped it doesn't seem to do that

#
    {
        //gets the anme, gets the item, decreases the item uses and sets it to the new uses
        string slotName = activeSlot.name;
        Item currentItem = itemDictionary.GetValueOrDefault(slotName, items[0]);
        currentItem.uses--;
        int newUses = currentItem.uses;

        activeSlot.GetComponentInChildren<TextMeshProUGUI>().text = newUses.ToString();
    }
hidden jewel
#

i would start by getting rid of all these string comparisons and GetComponent calls

#

make the Slot directly reference an Item

glad ore
#

Yeah I think my system isn't great

#

ooh that sounds good tho

#

thing is

#

this is the slot

#

It's the "Gravity Well" one

#

I need the slot to know itself

#

I'm gonna make the slot item thing one struct

#

Because they're going to be separable anyways

glad ore
#

Hey @hidden jewel

#

I have this function here:

    {
        //Gets the itemslot from the slot name and decreases the number of uses
        string slotName = slot.name;
        ItemSlot desiredItemSlot = nameSlotDictionary.GetValueOrDefault(slotName, slots[0]);
        desiredItemSlot.itemUses--;
        int currentUses = desiredItemSlot.itemUses;

        slot.GetComponentInChildren<TextMeshProUGUI>().text = currentUses.ToString();
    }
#

But when I run it, it only decreases the value once then stops

#

You have any idea why?

hidden jewel
#

well, yeah, there's no looping or recursion here

#

you'd have to run the function multiple times

glad ore
#

No

#

I do run it multiple times

#

I press a button

#

In the game

hidden jewel
#

is ItemSlot a struct

glad ore
#

Yes

hidden jewel
#

structs are value types. when you do a = b, you make a copy

#

a and b do not refer to the same data

#

so, you aren't actually changing the itemUses field on the struct that's stored in the dictionary

glad ore
#

ahh

hidden jewel
#

you can fix this by assigning the modified ItemSlot -- you called it desiredItemslot -- back into the dictionary

glad ore
#

should I make it a class then

hidden jewel
#

you could also do that

glad ore
#

ooh

#

btw

#

The dictionary isn't being populated properly

#

Because when I do a default value that's not in the array, it throws an exception

#

so it's just using the default

hidden jewel
#

then you ought to fix that...

glad ore
#
        //Sets up the string, itemslot dictionary
        foreach(ItemSlot itemSlot in slots)
        {
            nameSlotDictionary.Add(itemSlot.itemName, itemSlot);
        }
hidden jewel
#

you should definitely get away from using GameObjects everywhere

#

you want to use specific things

#

a Slot, perhaps

glad ore
#

How do you mean

#

The slot gameobject is the only one I use here

glad ore
#
        nameSlotDictionary.Add(slotName, desiredItemSlot);
hidden jewel
#

yes, because you can't use Add if the key already exists

#

dict[key] = value

#

this can both add and update a key-value pair

glad ore
#

ooh alright

hidden jewel
#

even if it makes no sense

glad ore
#

oooh you're right

hidden jewel
#

a good rule of thumb is to minimize the number of ways you can make a mistake

glad ore
#

yes I'm trying to do that

hidden jewel
#

I used to use GameObject all over the place

#

now I prefer to use specific component types

glad ore
#

good architecture and all that

#

thing is

#

the actual slot gameobject

#

doesn't have anything identifying it

#

everything is on the inventoryspawner

hidden jewel
glad ore
#

No I removed that

#

It's now an ItemSlot type

#
public struct ItemSlot
{
    [Header("Item Variables")]
    public string itemName;
    public Sprite itemIcon;
    public int itemUses;

    [Header("Slot Variables")]
    public GameObject slotObject;

}
#

Ok so I'm definitely populating the dictionary improperly

hidden jewel
#

i would keep the slot and item types separate

#

a slot holds an item

#

you can probably have items that aren't currently in a slot

glad ore
#

Yes I thought about that but in this prototype

#

Only one slot will hold only one item

#

They will be inseparable

#

Much like unlocking skills if you get what I mean

#

Ok

#

I have fixed all the issues I currently have

#

Thank you very much man

hidden jewel
#

ah, okay, then it makes sense to put them together