#Dictionary Confusion
1 messages · Page 1 of 1 (latest)
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
and why do you think something is wrong?
what does "not working" mean?
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();
}
i would start by getting rid of all these string comparisons and GetComponent calls
make the Slot directly reference an Item
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
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?
well, yeah, there's no looping or recursion here
you'd have to run the function multiple times
is ItemSlot a struct
Yes
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
ahh
you can fix this by assigning the modified ItemSlot -- you called it desiredItemslot -- back into the dictionary
should I make it a class then
you could also do that
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
then you ought to fix that...
//Sets up the string, itemslot dictionary
foreach(ItemSlot itemSlot in slots)
{
nameSlotDictionary.Add(itemSlot.itemName, itemSlot);
}
you should definitely get away from using GameObjects everywhere
you want to use specific things
a Slot, perhaps
When I tried that, it gave me this error
nameSlotDictionary.Add(slotName, desiredItemSlot);
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
ooh alright
you can pass any game object into that function
even if it makes no sense
oooh you're right
a good rule of thumb is to minimize the number of ways you can make a mistake
yes I'm trying to do that
I used to use GameObject all over the place
now I prefer to use specific component types
good architecture and all that
thing is
the actual slot gameobject
doesn't have anything identifying it
everything is on the inventoryspawner
don't you have a Slot type?
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
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
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
ah, okay, then it makes sense to put them together