#Ozone Inc. Inventory System

1 messages · Page 1 of 1 (latest)

barren bane
#

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.

autumn axle
barren bane
#
    public void ManageInventory()
    {
        for (int i = 0; i < inventory.InventoryList.Count; i++)
        {
            Item item = inventory.InventoryList[i];

            GameObject addedItem = Instantiate(itemObjectTemplate, inventoryListObject.transform);

        }
    }
autumn axle
#

looks correct yes

#

and now u add the sprite of the item to the "addedItem" image

barren bane
#

ye

#

do i need to get the component

#

or no

autumn axle
#

yes

barren bane
#

but what if i assign the itemIcon in the inspector

#

can't i just grab that?

#

or no

autumn axle
#

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

barren bane
#

why would I do that

#

can i just instantiate the gameobject

autumn axle
#

private MyScript prefab;

MyScript newItem = Instantiate(prefab)
newItem.SetImage(image)
newItem.stackable = true

something like that

barren bane
autumn axle
#

ok what are u trying to do now

#

to load the image to the prefab?

barren bane
#

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

autumn axle
#

mhm

barren bane
#

Currency derives from a scriptable object "Item"

#

so how do i set the image of that addedItem

autumn axle
#

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

barren bane
#
    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?

autumn axle
#

that works

barren bane
#

im not doing the script method

#

i dont want to do that

autumn axle
#

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

barren bane
#

ye

#

would I need to change a ton of stuff

#

to do script

autumn axle
#

no

barren bane
#

can u give me a sample

autumn axle
#

yes

barren bane
#

so i understand what it would look like

autumn axle
#

let me create a script

barren bane
#

alr

autumn axle
#
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

barren bane
#

huh

#

can u put that in code format

#

its hurting my eyes

#

hmmm

#

where would that part go

autumn axle
#

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

barren bane
#

where does it instantiate the object

autumn axle
#

wdym

#

the prefab instantiate or the image and etc?

barren bane
#

the prefab instantiate

autumn axle
#
private ScriptInThePrefab  prefab;

ScriptInThePrefab newPrefab = Instantiate(prefab);
#

newPrefab.Initialize(itemScriptableObject);

barren bane
#

huh

#

idk if i wanna do that

autumn axle
#

do it the way u want

#

with time u might change ideas

barren bane
#

maybe

#

i might do that actually

#

but i dont really get it still

autumn axle
#

can u explain what u dont understand?

barren bane
#

where do i put the script?

autumn axle
#

in the prefab

barren bane
#

okay

#

lemme try it

barren bane
autumn axle
#

a reference to your scriptable object

barren bane
#

ye but now Im confused

#

like really confused

autumn axle
#

in your script where u manage your inventory u have a list of scriptable objects right?

barren bane
#

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

autumn axle
#

...

barren bane
#

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

autumn axle
#

that works

barren bane
#

no way it actually worked

#

lol

#

it actually worked

#

i didnt think it was gonna cuz it was my first try

autumn axle
#

good job pat

barren bane
#

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

autumn axle
#

i actually dont know, i think i did it once but dont remember

barren bane
#

waiy

#

how do i check the number of the same items

#

like if i have 5 Iron is my inventory list

autumn axle
#

let me think

barren bane
#

how do i check that they are all Iron and then stack them

autumn axle
#

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

barren bane
#

u dont want to create another list

autumn axle
#

theres really no issue with having multiple lists

#

u can also

#

hm

#

idk

#

theres many things ig

barren bane
#
            if (item is Currency)
            {

            }
#

this is how u check for derived class btw

#

what else could I do

#

for stacking items

autumn axle
#

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

barren bane
#

how would I do that

#

never used dictionaries before

#

ive heard of it

#

but never used it

autumn axle
#

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

barren bane
#

Huh

#

Will that ruin stuff

#

Or no

autumn axle
#

nah

barren bane
#

Does Dictionary have a .add

autumn axle
#

yes

#

like i showed there

barren bane
#

Oh Alr

#

I’m not on my PV

#

PC

#

Rn

#

I got off of it for tonight

barren bane
#

But Tysm for the help

autumn axle
#

i will go to bed too now

barren bane
#

I really appreciate it

autumn axle
#

gl tomorrow!

barren bane
#

Thanks

barren bane
#

@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

lean fulcrum
#

What does messed up mean ?

barren bane
#

this is what happened when I changed the list to a dictionary

lean fulcrum
#

You can remove this line

barren bane
#

this is the error

#

error CS1503: Argument 1: cannot convert from 'int' to 'Item'

#

what line

lean fulcrum
#

The entire line you just sent me

#

Item item = ...

barren bane
#

why tho

#

will that mess stuff up?

#

or no

lean fulcrum
#

Because you already get the item ref from the foreach

barren bane
#

oh

#

one moment

barren bane
#

when I commented out the line

#

those two lines where below the line u told me to delete

lean fulcrum
#

Did you keep the foreach or did you switch to a for loop as your last screenshot suggests ?

barren bane
#

its just for

lean fulcrum
#

go back to the foreach

barren bane
#

was wut i had correct?

lean fulcrum
#

Well, yes

#

You "can't" loop through a dictionary using a for loop

barren bane
lean fulcrum
#

It's not meant for it

#

remove the .Count

barren bane
#

and put .Keys?

#

or leave it

lean fulcrum
#

Yes

barren bane
#

what does yes mean

#

leave it or put .keys

lean fulcrum
#

Put .Keys

barren bane
#

okay

lean fulcrum
#

You typed faster than i did

barren bane
#

but what about the value

#

cuz foreach item in that inventory

#

there is also an amount

lean fulcrum
#

Do you plan on using the int value for each item in this foreach loop ?

barren bane
#

well the value is gonna be the amount of the item

#

like for stacking purposes

#

and the dictionary isnt in the inspector anymore

lean fulcrum
#

OK. If you need it :

foreach(var pair in inventory.InventoryList)
{
  Item item = pair.key;
  int amount = pair.value;
  // do stuff
}
barren bane
#

why pair

#

can u do the script without changing a ton of stuff so it confuses me?

lean fulcrum
lean fulcrum
barren bane
#

not really

lean fulcrum
#

That's the problem

barren bane
#

i watched a tutorial

lean fulcrum
#

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 ?

barren bane
#

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

lean fulcrum
#

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

barren bane
lean fulcrum
#

That because you put Item instead of var

barren bane
#

wait it needs to be var?

lean fulcrum
#

You dictionnary doesn't contain Item

barren bane
#

then what about the Item

#

yes it does

lean fulcrum
#

It contains keyvalue pairs of type <Item, int>

barren bane
#
    public Dictionary<Item, int> InventoryList;
#

it does contain Item

lean fulcrum
#

sigh

#

Listen,

#

it doesn't

#

it contains pairs

#

pairs contain an item

barren bane
#

okay

lean fulcrum
#

item.key

barren bane
#

okay

#

okay im starting to get this now

#

its like python sort of

lean fulcrum
#

Read this

barren bane
#

okay

#

how do I make it serialized

#

or can i not do that

#

cuz i wanna see what it looks like in the inspector

lean fulcrum
#

You can

#

But it's not "beginner" friendly

#

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

barren bane
#

i dont want multiple stacks

#

of the same item

#

since its a trading game

#

i dont want there to be a stack limit

lean fulcrum
#

So it's not an inventory like Monster Hunter or Minecraft

#

Okay

barren bane
#

ye

#

can i do something similar to a python event

lean fulcrum
#

What kind of event do you want to do ?

barren bane
#

like whenever an Item is added it calls a function

lean fulcrum
#

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

barren bane
#

wait

#

i can just call the function whenever an item is added

#

cuz its gonna use the Item anyway

lean fulcrum
#

You can, it depends on you architecture

barren bane
#

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;
    }
    
}
barren bane
#

ill tell u later

barren bane
#

@lean fulcrum

#

r u available?

#

@autumn axle

#

what about u

autumn axle
#

uh