#Ozone Inc. Inventory System
1 messages · Page 1 of 1 (latest)
Hello
public void ManageInventory()
{
foreach (Item in inventory.InventoryList)
{
}
}
I'm trying to manage the inventory UI
and I need to instantiate an Item Object for every item in the list
and for certain types of Items in the list, it will either stack, or not.
make a prefab that u spawn inside of a layout group or something to display the items, and for some items instead of creating u add +1 in the already created. this might not help much but gl ig
would this be correct?
public void ManageInventory()
{
for (int i = 0; i < inventory.InventoryList.Count; i++)
{
Item item = inventory.InventoryList[i];
GameObject addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
}
}
yes
doesnt works because they have different icons depending on the item right
the prefab is only 1
u want to add the sprite through code
actually
better than getting the component
u :
Instantiate the prefab
but instead of instantiating it as gameObject u instantiate it as a Script that it has.
Or u simply get the script
then the script will have a reference to the image,
and a function to give a sprite to the image, that u will pass through the function.
and then u can also have a bool for the stackable or not and a lot of other stuff
huh
why would I do that
can i just instantiate the gameobject
private MyScript prefab;
MyScript newItem = Instantiate(prefab)
newItem.SetImage(image)
newItem.stackable = true
something like that
the items have an isStackable bool already
ye
i need to add the item to the list
then set all the texts
and if the Item is a currency, it will stack
if its a resource it will stack
and if its a weapon it wont
and I have scriptable Object Item that has those types derived from it
like this
mhm
Currency derives from a scriptable object "Item"
so how do i set the image of that addedItem
in this part
instead of extracting the GameObject
extract some script the object has
do u know how to do that?
like, instead of declaring the prefab : GameObject prefab;
declare it MyScript prefab;
and then when u spawn it
u can acess functions inside it
public void ManageInventory()
{
for (int i = 0; i < inventory.InventoryList.Count; i++)
{
Item item = inventory.InventoryList[i];
GameObject addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
Sprite itemImage = item.itemIcon;
addedItem.GetComponent<Image>().overrideSprite = itemImage;
}
}
like this?
that works
but now u need to do all of that for evertyhing
for the text
for the attributes
for the overlay
script is a better method for code cleaness
no
can u give me a sample
yes
so i understand what it would look like
let me create a script
alr
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMpro;
public class a : MonoBehaviour
{
public Item itemScriptableObject;
[SerializeField] private Image image;
[SerializeField] private TEXT_pro title;
// etc
public void Initialize()
{
SetImage();
SetText();
// etc
}
private void SetImage()
{
image.sprite = itemScriptableObject.sprite;
}
// same for text and other attributes
}
So
Sprite itemImage = item.itemIcon;
addedItem.GetComponent<Image>().overrideSprite = itemImage;
}
}
instead of doing this
u can just do 2 lines of codes
u do :
newItem.itemScriptableObject = listScriptableObject;
newitem.Initialize();
u assign the correct scriptable object to the item
then u call a function
that takes the information from the scriptable object and does everything
huh
can u put that in code format
its hurting my eyes
hmmm
where would that part go
this one
right after u Instantiate the object
u can even make it 1 line of code actually
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMpro;
public class a : MonoBehaviour
{
private Item itemScriptableObject;
[SerializeField] private Image image;
[SerializeField] private TEXT_pro title;
// etc
private void Initialize(Item item)
{
itemScriptableObject = item;
SetImage();
SetText();
// etc
}
private void SetImage()
{
image.sprite = itemScriptableObject.sprite;
}
// same for text and other attributes
}
this way u only need 1 line after the instantiate
where does it instantiate the object
the prefab instantiate
private ScriptInThePrefab prefab;
ScriptInThePrefab newPrefab = Instantiate(prefab);
newPrefab.Initialize(itemScriptableObject);
can u explain what u dont understand?
where do i put the script?
in the prefab
wtf is private Item itemScriptableObject
a reference to your scriptable object
in your script where u manage your inventory u have a list of scriptable objects right?
there are a ton of different items
how am I supposed to get the image
nvmd i figured it out
i dont need private Item
i can delete it
...
cuz the Inventory UI references it anyway
it gives this prefab script the item needed to edit
@autumn axle
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class ItemObjectManager : MonoBehaviour
{
[SerializeField] private GameObject itemIcon;
[SerializeField] private TMP_Text itemName;
[SerializeField] public GameObject itemObjectTemplate;
// etc
public void Initialize(Item item)
{
SetImage(item);
SetText(item);
}
private void SetImage(Item item)
{
itemIcon.GetComponent<Image>().sprite = item.itemIcon;
}
private void SetText(Item item)
{
itemName.text = item.itemName;
}
}
ItemObjectManager addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
addedItem.Initialize(item);
@autumn axle
that works
no way it actually worked
lol
it actually worked
i didnt think it was gonna cuz it was my first try
good job 
NOW...
i need to check the type of class the ITem is
if its Currency : Item
if its Weapon :Item
how would I do that
i actually dont know, i think i did it once but dont remember
waiy
how do i check the number of the same items
like if i have 5 Iron is my inventory list
let me think
how do i check that they are all Iron and then stack them
ok u do this
u have your list of items
while u are spawning them
u create a second list and u put the items created in there
if a item is stackable
and is already in the list
u dont create a new one
and uhm
u dont want to create another list
theres really no issue with having multiple lists
u can also
hm
idk
theres many things ig
if (item is Currency)
{
}
this is how u check for derived class btw
what else could I do
for stacking items
oh cool
maybe a dictionary
a dictionary is a list but basically a double array
so
u can give a value to the items
so if u have a stone
instead of having multiple stones
u have a stone with a value of 57
dictionaries sound nice for it ye
how would I do that
never used dictionaries before
ive heard of it
but never used it
public Dictionary<Item, int> ItemDictionary= new Dictionary<Item, int>();
// add item to dictionary, check if is stackable, and how many u have, then add it
ItemDictionary.Add(item, numberOfItem);
but u still need to get the items from somewhere
so u might want to transform the list into a dictionary
nah
Does Dictionary have a .add
But Tysm for the help
i will go to bed too now
I really appreciate it
gl tomorrow!
Thanks
@autumn axle
can u help me with turning my inventory into a dictionary
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public Dictionary<Item, int> InventoryList;
public void AddItem(Item item, int itemAmount)
{
InventoryList.Add(item, itemAmount);
}
}
this is the inventory list
and i made it a dictionary
public void ManageInventory()
{
foreach (Item item in InventoryList.Keys)
{
Item item = inventory.InventoryList[i];
//GameObject addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
ItemObjectManager addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
addedItem.Initialize(item);
Debug.Log($"Added Item to Inventory");
if (item is Currency)
{
}
}
}
and now this function is messed up
i tried fixing it
What does messed up mean ?
You can remove this line
this is the error
error CS1503: Argument 1: cannot convert from 'int' to 'Item'
what line
Because you already get the item ref from the foreach
more errors
when I commented out the line
those two lines where below the line u told me to delete
Did you keep the foreach or did you switch to a for loop as your last screenshot suggests ?
its just for
go back to the foreach
was wut i had correct?
Yes
Put .Keys
okay
You typed faster than i did
but what about the value
cuz foreach item in that inventory
there is also an amount
Do you plan on using the int value for each item in this foreach loop ?
well the value is gonna be the amount of the item
like for stacking purposes
and the dictionary isnt in the inspector anymore
OK. If you need it :
foreach(var pair in inventory.InventoryList)
{
Item item = pair.key;
int amount = pair.value;
// do stuff
}
Dictionnaries are not serialized by unity by default, so you won't be able to see it in the inspector without making some arrangement, but I'll leave google make its work on this subject
Do you know how a dictionnary work ?
not really
That's the problem
i watched a tutorial
A dictionnary is a collection of key-value pairs
keys are unique, values are not
if you want to parse a dictionary, you have to parse each pair
pair contains Key and Value
That's why I named it that way, you'll find kvp online, but you can name it as you want actually
But let me ask you, why do you want a dictionary in the first place if you don't know how it works ?
because someone told me to do it for stacking
like stacking items of the same type on each other
and if I had the item in the dictionary: Iron, i could put the amount aswell
Well, when someone suggest you something, your 1st reflex, if you want to be a good developer, should be to search for it on Google or whatever search engine you use
i did search it up
That because you put Item instead of var
wait it needs to be var?
You dictionnary doesn't contain Item
It contains keyvalue pairs of type <Item, int>
item.key
okay
how do I make it serialized
or can i not do that
cuz i wanna see what it looks like in the inspector
You can
But it's not "beginner" friendly
You can follow their explanations https://odininspector.com/tutorials/serialize-anything/serializing-dictionaries
If I were you, I would not use a dictionary in the 1st place
For a simple reason
It won't support mutiple stacks of the same item
Like, you won't be able to have two stacks of iron
i dont want multiple stacks
of the same item
since its a trading game
i dont want there to be a stack limit
What kind of event do you want to do ?
like whenever an Item is added it calls a function
I'm not sure the syntax is the same, but yes, you can define your own event which can be subscribed in some other part of your code
Here
wait
i can just call the function whenever an item is added
cuz its gonna use the Item anyway
You can, it depends on you architecture
ye
my inventory UI isnt working now
public void ManageInventory()
{
foreach (var item in inventory.InventoryList)
{
//GameObject addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
ItemObjectManager addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);
addedItem.Initialize(item.Key);
int itemAmount = item.Value;
Debug.Log($"Added Item to Inventory");
}
}
this is the function that updates the inventory UI
and this is the script that assigns the text based on the stuff
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class ItemObjectManager : MonoBehaviour
{
[SerializeField] private GameObject itemIcon;
[SerializeField] private TMP_Text itemName;
[SerializeField] public GameObject itemObjectTemplate;
// etc
public void Initialize(Item item)
{
SetImage(item);
SetText(item);
}
private void SetImage(Item item)
{
itemIcon.GetComponent<Image>().sprite = item.itemIcon;
}
private void SetText(Item item)
{
itemName.text = item.itemName;
}
}
im having a small issue but i gtg for now
ill tell u later
uh
