#make a thread real quick

1 messages · Page 1 of 1 (latest)

frozen belfry
#

here

#
public void AddItem(ItemData item) //Adds item into Inventory
    {
        for (int i = 0; i < inventorySlots.Length; i++)
        {
            if (inventorySlots[i] == item)
            {
                continue;
            }
            if (inventorySlots[i] != item)
            {
                inventorySlots[i].itemInSlot = item;
                break;
            }
        }
    }
#

i believe this should work for adding items to the inventory

mortal flare
#

the continue part is unecessary (the whole first if statement)

#

and the other part will replace the first different item it seems

#

rather than finding an empty slot

frozen belfry
#

oh wait

#

it needs to check for null

mortal flare
#

you also want to handle the case where there aren't any empty slots found somehow

frozen belfry
#

yeah

grave horizon
#

uhh

#

slotv.transform.GetComponent<Image>().sprite = slotv.GetComponent<Slot>().itemInSlot.icon;

#

why

#

you already have the slot you're like working backwards

mortal flare
#

shouldn't the slot class just have a SetItem function that does all that?

frozen belfry
#

do i not need to change the slot objects image sprite to the item thats inside it?

mortal flare
#

i.e. rather than inventorySlots[i].itemInSlot = item; you should be doing inventorySlots[i].SetItem(item);

#

unless itemInSlot is a property

grave horizon
mortal flare
#

in which case the setter can handle things

#

That's true also^

#

the UI should listen to changes in the inventory and update things via the observer pattern

frozen belfry
mortal flare
#

then you shouldn't set it directly, you should use a setter function or a property

#

so you can do things like fire off an event or update the UI when it changes

frozen belfry
#

oh ive never done that before

grave horizon
#

they're very simple to do its just like calling a method

frozen belfry
#

so SetItem() is in the actual items scrip object?

#

or slot

grave horizon
#

SetItem is in the slot

frozen belfry
#

oh ok

grave horizon
#

inventory manager calls it

#

Slot deals with changing the item and invoke a possible UI change

#

if events is too complex for now just call .sprite = item.sprite from the Slot in the same method

#

but you should consider making UI separate script

frozen belfry
#
  
public void SetItem(ItemData item)
    {
        itemInSlot = item;
    }
grave horizon
#

ItemInSlot should probably have a public get somewhere

#

so you can check the slots

frozen belfry
#

get set

mortal flare
#

yes you can do it as a property as well

frozen belfry
mortal flare
#

e.g.

ItemData _itemInSlot;

public ItemData ItemInSlot {
  get => _itemInSlot;
  set {
    _itemInSlot = value;
    UpdateUIWithNewItem(value);
  }
}```
grave horizon
#

they're special methods that do something when you do anything with itemslot

frozen belfry
#

oh so its literally just

#

getting it

#

then setting it

mortal flare
#

it's just like running a function when you do
something = theProperty < calls the getter
or
theProperty = something < calls the setter

mortal flare
frozen belfry
#

set is inside of get

mortal flare
#

it is not

#

they're separate

grave horizon
#

=> is a special operator

mortal flare
#

get => _itemInSlot; is just shorthand for:

get {
  return _itemInSlot;
}```
frozen belfry
#

oh

#

so the method shouldnt be void

#

does it need to return the item in slot

mortal flare
#

this replaces the SetItem method entirely

frozen belfry
#

im unsure where to put the get and set

mortal flare
#

in the ItemSlot class

frozen belfry
#
    public ItemData SetItem
    {
        get {  return itemInSlot; }
        set { itemInSlot = value; }
    }
mortal flare
#

no

#

you wouldn't call it SetItem

#

that would be really weird

#

look at my example

frozen belfry
#

ItemInSlot

#

then you put the item in the slot if you ran the method like, ItemInSlot(itemAdded)

#
 public ItemData ItemInSlot
    {
        get {  return itemInSlot; }
        set { itemInSlot = value; }
    }
mortal flare
#

sure except remember the whole point of this was so we can update the UI cleanly when the item changes

frozen belfry
#

ye

mortal flare
#

so you'll want to add that to the set

frozen belfry
#

UpdateUIWithNewItem(value);

mortal flare
#

yeah that was just an example though - idk how you actually have things set up

frozen belfry
#
public void AddItem(ItemData item) //Adds item into Inventory
    {
        for (int i = 0; i < inventorySlots.Length; i++)
        {
            if (inventorySlots[i] == null)
            {
                inventorySlots[i].itemInSlot = item;
                break;
            }
        }
    }
grave horizon
#

Ideally you'd want some type of event or method

frozen belfry
frozen belfry
#

thought so

mortal flare
#

You need to set ItemInSlot (the property) not the field

#

which is why the field should be private

#

to prevent you from making a mistake like this

frozen belfry
#

ItemInSlot(item);

mortal flare
#

no

#

just change itemInSlot to ItemInSlot

#

also instead of if (inventorySlots[i] == null) it should be if (inventorySlots[i].ItemInSlot == null)

#

or perhaps make an IsEmpty property

frozen belfry
grave horizon
#

wat

frozen belfry
mortal flare
#

THE PROPERTY

#

not the field

#

the property is public

#

the property is called ItemInSlot

grave horizon
#

notice capitalization

mortal flare
#

the field should be private. the field is called itemInSlot

#

I'll also note in my example above I used _itemInSlot for the field name to avoid this confusion further

frozen belfry
#
    [Header("Item inside of the slot")]
    public ItemData _itemInSlot;

    public ItemData ItemInSlot
    {
        get {  return _itemInSlot; }
        set { _itemInSlot = value; /*UpdateUIWithNewItem(value);*/ }
    }
mortal flare
frozen belfry
#

oh i accidently changed it mb

#

its fixed on mine

mortal flare
#

if you want it in the inspector use [SerializeField]

frozen belfry
#

but not on what i pasted

frozen belfry
#

how would i make it Update with UI now

mortal flare
#

create and implement this function UpdateUIWithNewItem(value);

frozen belfry
#

wait but first

#
public void AddItem(ItemData item) //Adds item into Inventory
    {
        for (int i = 0; i < inventorySlots.Length; i++)
        {
            if (inventorySlots[i].ItemInSlot == null)
            {
                inventorySlots[i].ItemInSlot = item;
                break;
            }
        }
    }
#

is this correct then?

mortal flare
#

I would even put return there instead of break. Then you can put code outside the for loop to handle the case of no empty slot being found (however you want to handle that)

#

maybe you have this function return a bool to indicate whether the item was successfully added or not for example

frozen belfry
#

help with debugging as well

#

currently provides minimal for it

frozen belfry
frozen belfry
#

does return end any loop?

grave horizon
frozen belfry
frozen belfry
mortal flare
#

wdym

#

Put it in the class

frozen belfry
#

oh the slot

#

class

frozen belfry
#

how would i use value in the

#

UpdateUIWithNewItem(value)

grave horizon
#

thats ur ItemInSlot being passed

mortal flare
#

it goes into the function the same way parameters go into any function

#

e.g.

void UpdateUIWithNewItem(ItemData newItem) {
  // do whatever you want
}```
frozen belfry
#
public void UpdateUIWithNewItem(ItemData value)
    {

    }
mortal flare
#

I wouldn't make it public

frozen belfry
#

idk why whenever i make the code block is makes it tabbed weird

mortal flare
#

If your IDE is doing that then you probably have some incorrect indentation somewhere

frozen belfry
#

its not the IDE

#

its from

#

!code

blazing escarpBOT
frozen belfry
#

making this^

mortal flare
#

doesn't happen to me ¯_(ツ)_/¯

frozen belfry
#

idk but its whatever

grave horizon
frozen belfry
#

how is it being useful?

grave horizon
#

because everytime you access it , that function runs

frozen belfry
#

alright

grave horizon
#

its useful because it can trigger something to happen if slot has changed item

#

without explicitly writing a function and calling that function every time

#

the function runs itself when you make change or get the prop

frozen belfry
#

with item/s

grave horizon
#

so whatever you put it in runs

frozen belfry
#

oh cool

#

im gonna need to have more experience with this to get better but this is a start for some deeper coding

grave horizon
#

well yeah regardless of what you're doing you're gonna have to learn properties some time

#

they're essential really

frozen belfry
#

i spend my study halls reading c# coding

#

so ill eventually learn more

#

but also now

grave horizon
#

all you're doing is making your code modular and not a nesting mess

#

when you introduce events into that you'll start to see that

frozen belfry
#

code modular?

grave horizon
#

yeah where you can more or less add to it / remove to it without breaking everything

frozen belfry
#
public void UpdateUIWithNewItem(ItemData value)
    {
        // Slot.GetComponent<Image>().sprite = value.icon;
    }
#

so i need the slots actual sprite to change to its items now

#

how would i reference the actual image?

grave horizon
#

for example if your inventory cant find a sprite or something the whole system breaks instead of just a slot breaking

frozen belfry
#

easier to work with

grave horizon
#

although you should ideally have another script that only deals with UI elements

#

imgComponentReference.sprite = value.sprite

frozen belfry
#

yeah

#

and imgComponentReference is a variable

#

thats equal to the slot objects image

#

how do i reference another component from one component

#

like from the Slot component (script) to Image component

grave horizon
frozen belfry
#

and drag it in for each slot?

grave horizon
#

this is on the slot

#

yes drag Image that corrisponds to its slot

frozen belfry
#

ik

#

thats really the easiest way

#

suprising

grave horizon
#

you're breaking down responsibilities really for the most part

#

just like people

#

you wouldnt want to give 1 person doing 20 different things

#

it can be done, but its overwhelming confusing and prone to mistakes

frozen belfry
#

thats true

grave horizon
#

your code right now would say Hi I'm a Slot and I take care of this Slot right here #12 or w/e

#

so each slot does their own job

frozen belfry
#

how did you get so good at coding

#

im trying to learn but some things are complex

grave horizon
#

Trust me Im an infant in comparison to the big fish here

#

but I learned by doing trial and error basically lol

frozen belfry
#

how did the better coders here learn?

grave horizon
#

realizing that 500+ lines of script is NOT the way to go

frozen belfry
#

i assume c# isnt the only language they know tho

grave horizon
#

everyone has different backgrounds and experiences 🤷‍♂️

frozen belfry
#

do they go to college for it

#

often

grave horizon
#

some have formal school training sure

#

you don't need to go to school for it but it helps knowing the algorithms and such at your disposable

#

more importantly when to use what

frozen belfry
grave horizon
#

eh you're young off to a good start

#

by my age you will be coding 5D chess

frozen belfry
#

nobody at my school could help me at all cause i know 100 times more then them

#

and yet when doing it i feel so bad

#

i had to literally force myself through a super big learning curve to get where i am now

grave horizon
#

its usually a problem when you try to do too much at once

frozen belfry
#

not that online resources for learning are bad... but they teach as if i already know a decent amount

#

i mean i do know, but when i first began

#

im probably on month 3-4

#

(but to be fair i go to school like everyday)

grave horizon
#

practice / repetition makes you good

#

just keep doing it until your brain can just do it without thinking

frozen belfry
#

i mean im here everyday cause i code and work on my game daily

#

so i basically do that

grave horizon
#

just remember at the end of the day its not about so much the particular syntax

#

its the Logic you learn

#

learning to think differently literally

#

you can use any language really

#

all logic pretty much translates across

frozen belfry
#

i started with c++

#

then python

#

now im here

#

because the only resources i had were for those

grave horizon
#

I started with C++ but by accident

frozen belfry
#

c++ is similar

grave horizon
#

yeah but harder

#

I was making mod for Hl2 mods

#

and source engine runs on C++

#

so luckly it wasnt pure C++ but an engine but still ,

#

its poorly documented engine

#

so I gave up

frozen belfry
#

i plan to do more with c++

grave horizon
#

switched to unity

frozen belfry
#

but i heard it has a load of vis scripting

grave horizon
#

visual scripting sucks

frozen belfry
#

i dont like it personally

grave horizon
#

i trieddoing a raycast with nodes and it looked atrocious

#

so many nodes and stuff

#

I can do that in 1 line of code in unity

#

amazing stuff

frozen belfry
#

my friends keep telling me to use ue instead of unity

#

unreal engine

#

but unity feels way better

#

idk

grave horizon
#

ehh they're just tools, use whatever you can cook up more shit with

frozen belfry
#

mostly cause they want me to make super realistic battle royale whatever games

grave horizon
#

"Realistic battle royal"

grave horizon
#

whatever that means lol

#

what is realism ?

#

graphics?

#

functions

frozen belfry
#

basically

#

graphuics

#

my friends dont even know what a variable in coding is

#

so

grave horizon
#

I see so many games with amazing graphics (asset flips)

#

and the game itself sucks a fat dookie

frozen belfry
#

roblox nowadays has games with amazing graphics

grave horizon
#

some of the best games have the shittiest graphics usually

frozen belfry
#

yeah

#

its cause their iconic

#

minecraft is a prime example

grave horizon
#

but also fun

frozen belfry
#

my game im attempting to make is something ive dreamed of for years

grave horizon
#

you can say Tarkov looks really good for a unity game

grave horizon
#

many other

frozen belfry
#

but i also loved 2D top down games

#

such as

#

enter the gungeon

grave horizon
#

im old mane

#

i grew up on Newgrounds games

#

and flash

frozen belfry
#

i play those too

#

but during study hall at school lol

#

but anyways

#

ive wanted to make a bullet hell 2D mmorpg

grave horizon
#

thats pretty cool

#

multiplayer is diffcult af

frozen belfry
#

when i first began coding i attempted to make it right away and failed horribly

#

ALTHOUGH

grave horizon
#

even veterans around here will tell ya

#

multiplayer is really not a good first project lol

#

or second

#

or third

#

lol

frozen belfry
frozen belfry
#

after i failed i began the unity create with code

grave horizon
#

managining items, ingame economy 😵‍💫

frozen belfry
#

yeah fr

frozen belfry
#

i made

#

a cookie clicker game that had its own card packs with rarities you could buy with cookies to eventually make more cookies

#

^my first taste of the most simple inventory you could have

#

a 3D bean shooter, where you ran around and shot capsules

#

so this is probbaly my 4th or 5th projec

grave horizon
#

yeah inventory is tough but if you odo it clean it can easily extend

#

the goal is : you will make one so good that you will make your own asset you use in different projects

frozen belfry
#

AHH the inventory works!!!!!

#

but items cannot be moved currently

grave horizon
#

one step closer

#

moving it is with UI script

#

this gets tricky because of working with Unity canvas is annoying

#

but doable

frozen belfry
#

should i make it drag and drop right away, or just select slot then selected slot to move to

grave horizon
#

thats up to you

frozen belfry
#

well

#

ive never sucsesfully done either

#

and drag and drop sounds very complex to me

grave horizon
#

I can show an example of one of my card games and how I move them between the same slots

frozen belfry
#

ok

grave horizon
frozen belfry
#

each slot can hold many items

#

or cards in your case

grave horizon
#

yes

#

up to 5 in this case

frozen belfry
#

thats a VERY cool concept

#

mine will likely have the drag and drop and each slot will be able to hold up to like 160 of stackable items